Reading from a text file, seperated by commas..

BlitzMax Forums/BlitzMax Beginners Area/Reading from a text file, seperated by commas..

BachFire(Posted 2007) [#1]
This is a GUI app, which should be simple - but not for me. I am trying to read from a text file, which only has words seperated by commas. Each word should be listed in a listbox. The way I have done it (see code) sort of works, but.. The very last word is not listed in the box - I guess I can work that one out myself somehow. But another problem is, if there is more than one line in the text file, the first word is also not listed. This puzzles me. I'm not very good at this yet, but I keep trying..

I am kind of hoping that there is a way more efficient way of doing this, and that someone can help me with improving this method.

I have been sitting with this most of the day, so I hope someone can help me out.. :(

Local style = WINDOW_TITLEBAR
Local MainWindow:TGadget=CreateWindow("no title yet", 200,200,400,400,Null,style)
	Local Menu:TGadget=CreateMenu("Test Menu",0,WindowMenu(MainWindow))
		CreateMenu "Preferences",100,Menu
		CreateMenu "Exit",101,Menu
	UpdateWindowMenu MainWindow
	
	Local List:TGadget=CreateListBox(20,40,150,300,MainWindow)	
		Local label1:TGadget=CreateLabel("ListBox:",20,20,100,20,MainWindow)

'read from database file START

temp$=""
file:TStream=ReadFile("test.txt")
While Not Eof(file)

b=ReadByte(file)

If Chr(b)="," Then
 AddGadgetItem List,temp$
 temp$=""
Else
 temp$:+Chr(b)
EndIf

Wend

CloseFile(file)

'read from database file END

Repeat
	WaitEvent()
Until EventID()=EVENT_WINDOWCLOSE
End



tonyg(Posted 2007) [#2]
Unless you're doing something different with the text file this works for me.
If I add 'AddGadgetItem list,temp$' immediately after the wend I get the last word and I don't have an issue with the first word not being listed in a multi-line text file.

If you're on Bmax 1.24 fully synced then I can only guess the problem is with the text file.
If you post the text file contents we could try.


Ziltch(Posted 2007) [#3]
if you change
If Chr(b)="," Then
with
if b = 44 or b = 13 or b = 10 then ' chr(13) = CR, Chr(10) = LF

and after the wend add a line
if temp$ then AddGadgetItem List,temp$

that should help


klepto2(Posted 2007) [#4]
I would do it this way:




BachFire(Posted 2007) [#5]
@ tonyg

Thanks,

Yes, got the last word now. But I still loose the first word in the second line. This is the text file I use now:

these,entries,should,now,be,in,the,list,box <enter>
but,the,first,word,here,is,gone


excluding the "<enter>", of course.. When I run it, the word "but" is simply not there.. This is hopefully just not because it's on a mac.. :)


BachFire(Posted 2007) [#6]
@klepto2

Oh yes, that is very nice! I will work off of that. I'm guessing the line "If P = -1 And Then" was a typo? I removed "And Then", and all was fine.

Thanks a bunch! And of course thanks to everybody else who helped me here. ;)


tonyg(Posted 2007) [#7]
For your original program you need a comma after 'box'.


BachFire(Posted 2007) [#8]
Yes, that has crossed my mind too. However, ind the end, the program should deal with certain .CSV files. They originated from an .mdb database file (MS Access) exported to .CSV files, and there are thousands of lines. And each line don't end with commas.. :)

I use .CSV, because I thought it would be too complex for me to try to read from the .MDB file directly. I am a beginner. ;)


BachFire(Posted 2007) [#9]
@klepto2 (or someone else who might know)

In your example above, there are lines like these:

AddGadgetItem(List , Line[..P])
Line = Line[P+1..]


I'm sorry, but what I don't get, is what the stuff in the square brackets do? I have failed to find anything about it. I have even tried to watch the program, and print out before & after values of 'P', but they seem to me the same. What is it, and how does it work?


klepto2(Posted 2007) [#10]
Take a look in the manual under the topic 'slicing'.
In fact:
Everytime the program find a ',' it copies the text from line until position P to a new string and pass it to AddGadgetItem( Line[..P]). Then it shorten the original line string to get the next item with the same method.

eg:
Line originally written: Hello,World,!
P = 5, Line[..P] = Hello --> To Item
Line = Line[p+1..] = World,! 'the +1 is needed to get rid of the previous ','
P = 5, Line[..P] = World --> To Item
Line = Line[P+1..] = !
P = -1 --> No ',' found
Line is added as an Item
Jump out of the repeat loop

I hope this helps.


BachFire(Posted 2007) [#11]
Aaah, I see. That's a nice feature! Thanks a lot for your explanation. ;)