Jump to content



Photo

Reading File


  • Please log in to reply
3 replies to this topic

#1 tofu

tofu

    Newbie

  • Members
  • Pip
  • 2 posts

  • Calculators:
    Classpad 300+
    Casio Graph 35+

Posted 15 November 2008 - 01:35 PM

Hello.
I'm not gifted to speak English, please be indulgent with me.

I'm a new programmer with CPLua. This Add-In is very powerfull but I have a problem.
i've created a "hanged" and now I would like to use a dictionary.
It is composed of list of words as:

File : dico
Fleur
Chocolat
voiture
test

In my main program I want display the first line :
require "io"
---
...
---
f = io.file("dico") -- opening file in folder CPLUA/
f:open("r") --read only
p = f:read() --read first line

print(p)

Now I run the program, it gives me :
Fleur
Chocolat

-- Why two words appear ???

and if I want to list them :
require "io"
---
...
---
f = io.file("dico") -- opening file in folder CPLUA/
f:open("r") --read only
p = f:read() --read first line

while(p ~= nil and p~= eof) -- i show you, the different things i tested
do
  print(p)
  p = f:read()
end

print(p)

The program gives :
Fleur
Chocolat
voiture
test
main:73: Couldn't read (null) in file

-- I can not avoid the error...

Can you help me please, i'm newbie in CPLua I don't understand.

Thanks a lot ! (and still sorry for my low level of English)

#2 Kilburn

Kilburn

    Casio Technician

  • Members
  • PipPipPipPipPipPip
  • 491 posts
  • Gender:Male
  • Location:France
  • Interests:Blah

  • Calculators:
    FX-7500 G
    ClassPad 300

Posted 16 November 2008 - 04:07 PM

Français, hein ? :)

Anyway, I'll be replying to you in English, so it may help other people as well. I haven't used CPLua 0.9 very much, but I would suggest using a table instead of a file for this job. It might not be the best way to do it, but it's the most simple to understand.

For your example, write this in your file "dico":
export{
dico={
"fleur",
"chocolat",
"voiture",
"test",
}
}

This will create a list named "dico" containing all your words, and "export" it in the main program when you include this file using require().

Then, in your main program:
require("dico")

for i=1,#dico do
 print(dico[i])
end

#dico equals to the number of elements in the list "dico".


Not sure if this works, I haven't tested it yet. Don't hesitate to repost if you need more help or misunderstood something. ;)

#3 Orwell

Orwell

    Casio Overlord

  • Members
  • PipPipPipPipPipPipPip
  • 777 posts
  • Gender:Male
  • Location:Paris - France

  • Calculators:
    Casio AFX 1.02 / Casio ClassPad 300

Posted 17 November 2008 - 02:10 PM

Actually, while Kilburn solution is certainly efficient, using text files might be a better idea in this case ;) If his "dictionary" gets large enough, the exported table could become rather large and consume a big part of the memory. Having the data in the CP's file storage is for me a better solution.

Anyway I must say that tofu's code should work (except for the 'eof' detection; see below) :unsure: I suppose that some 'newline' characters in his file are not encoded correctly, hence the display of the two first lines (it seems that CPLua simply didn't detect the newline in the file)... How did you create your file? :huh:

Maybe you should test your code on a file written by CPLua :)
Try something like:

-- write the file
f = io.file("dico2")
f:create("text")
f:open("w")
f:write("Fleur\nChocolat\nvoiture\ntest")
f:close()

-- read it
f = io.file("dico2")
f:open("r")
count = 0
while not f:eof() do  -- check if end of file not reached
  print(count, f:read()) -- display each line
  count = count+1
end
f:close()  -- don't forget to close the file after reading
If this code displays the 4 lines of the file "dico2" correctly (and not those from the file "dico", then there must be a problem with your file :)
Btw, notice the call to f:eof(), this function returns true when the end of file was reached ;)

#4 tofu

tofu

    Newbie

  • Members
  • Pip
  • 2 posts

  • Calculators:
    Classpad 300+
    Casio Graph 35+

Posted 18 November 2008 - 10:43 PM

Oui français :).
Orwell, c'est toi Julien (sur classpad.fr) ? Si c'est le cas, toutes mes félicitations, je peux plus me passer de CPLua ! (à la poubelle le basic :P).

Par contre je me choppe des fatals ERRORS lors de l'ouverture de mon pendu (peut être trop gros fichier ?)
Plus problèmatique, c'est l'utilisation de <ui>, il doit y avoir une fuite de memoire (peut être le fait que je n'ai pas placé l'instruction ui.stop() ) mais je peux plus rien faire dans CPLua pendant 1 heure minimum dès que l'écran de la mort apparait (le temps que la caltoche se mette vraiment en veille), les fatals errors se produisent en permanence.

Sinon je vais le redire mais, encore bravo à toi !

----

Thanks for answers.
I will describe how i managed to resolve my problem.

First time, the file dico seemed have an invisible char in the first line (probably with copy/paste !)
Else I created the file with "New" in Lua. The other words have been written by hand (In my opinion Lua take car "newline" ( ex: "\n" in C))

I say this because when I converted my string in array, one or two characters invisible are in this array.

In second part, I used f:eof with my file. it works !

Do you want, i show you my code ? (you will see the invisibles chars)

Edited by tofu, 18 November 2008 - 10:57 PM.



1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users