Problems reading a txt file..
Blitz3D Forums/Blitz3D Beginners Area/Problems reading a txt file..
| ||
I'm trying to read info off a text file, what am i doing wrong here? i get a mav, what i'm trying to do in this case, is read off a text file, and everytime it sees the word "New list" it adds it to a WinBlitz3d combo box..Function Readmoddata(file$) If FileType(file$)=1 opened=ReadFile(file$) strng$=ReadString(opened) Repeat If strng$="New list" WB3D_AddGadgetItem(catagory,strng$,0,0) Until Eof(opened) CloseFile(file$) End If End Function |
| ||
strng$=ReadString(opened) needs to go inside your Repeat/Until, for a start. As it stands, you're going to get stuck inside the loop indefinitely since you're not reading any more data from the file. Also, turn Debug mode on and you'll get a more specific error. |
| ||
Also, are you sure the file you're reading contains the strings in the correct (4-byte header) format? If it really is a true txt file, you may be wanting to use ReadLine instead. |
| ||
can you give me an example on how i'm suppost to go about doing this? i'm new to the whole reading data stuff |
| ||
If your data file contains string variables (created with WriteString):Function Readmoddata(file$) If FileType(file$)=1 opened=ReadFile(file$) Repeat strng$=ReadString(opened) If strng$="New list" WB3D_AddGadgetItem(catagory,strng$,0,0) Until Eof(opened) CloseFile opened End If End Function If your data file is a true txt file (i.e. viewable in NotePad): Function Readmoddata(file$) If FileType(file$)=1 opened=ReadFile(file$) Repeat strng$=ReadLine$(opened) If strng$="New list" WB3D_AddGadgetItem(catagory,strng$,0,0) Until Eof(opened) CloseFile opened End If End Function Edit: just realized you were using CloseFile wrongly, too. :) |
| ||
Hmm, seems like it working, but the "New list" is not showing up in the combo box.. heres how i call it: Readmoddata("test.txt") heres the function.. Function Readmoddata(file$) If FileType(file$)=1 opened=ReadFile(file$) Repeat strng$=ReadLine(opened) If strng$="New list" WB3D_AddGadgetItem(catagory,strng$,0,0) Until Eof(opened) CloseFile(opened) End If End Function and heres the test.txt file. blah blah blah blah New list everything looks ok to me. |
| ||
That should work, assuming test.txt is located in the same folder as the prog source. You may want to change the If line to the following, for added integrity, though:If Lower$(Trim$(strng$))="new list" WB3D_AddGadgetItem(catagory,strng$,0,0) Also, try putting a Stop in that if statement and run in Debug to make sure "new list" is being found. If it is, your problem's with the call to WB3D_AddGadgetItem, something I know nothing about. |
| ||
i don't think its a problem with the WB3D function, run my source above in a basic example, change Wb3D to somthing like "Print Found" it still doesnt work here, i'll try that replacement function |
| ||
Hmm, somthing must be wrong, look at this following example:SetBuffer BackBuffer() Print "Testing file..." If FileType("test.txt")=1 Print "File Found!" Print "Trying to Extract information" file = ReadFile("test.txt") Repeat RL = ReadLine(file) Print RL Until Eof(file) CloseFile(file) Else Print "No file Found!" End If Delay 5000 gives me: Testing file... File Found! Trying to extract infomation 0 0 0 0 |
| ||
when calling WB3D_AddGadgetItem() are you sure catagory is a valid gadget? kev |
| ||
yup, read my above example, WITHOUT any wb3d stuff, theres a problem with the way i'm doing it. |
| ||
In that example, you're reading the line into an integer variable. Change RL to RL$ and it works fine. |
| ||
i think i found my problem: my text file consists of the followingTlist.list = New list i wanted to look for the New list part, but it seems Readline only reads the whole line or nothing, thus, if a line is more then "New list" it won't pick it up.. any work arounds or ideas? |
| ||
use Instr() to search for 'New list' in the string returned by Readline(). kev |
| ||
can you give me an example on how this would be used? from reading the doc i'm not sure how it will help. |
| ||
;tmp$ = ReadLine(file) tmp$ = "testing line" If(Instr(tmp$,"New list")) = 0 Then Print "New List not in string" Else Print "New list found in string" EndIf add 'New list' to the string tmp$ and see its located, the same is if ReadLine(file) is used. one extra note Instr() returns the position its found within the string. kev |
| ||
hmm, i don't think i get it, whats wrong with the following?SetBuffer BackBuffer() Print "Testing file..." If FileType("test.txt")=1 Print "File Found!" Print "Trying to Extract information" file = ReadFile("test.txt") Repeat RL$ = ReadLine(file) lookfor$="New list" If Instr(lookfor$,RL$)=0 Print "Not found" Else Print "Found" Print lookfor$ End If Until Eof(file) CloseFile(file) Else Print "No file Found!" End If Delay 5000 |
| ||
change Instr(lookfor$,RL$) to Instr(RL$,lookfor$), the first param is the string to be searched the second param is the string to be found, so param 2 string is searched for in param 1 string. does that make sence? kev |
| ||
Thanks! works good now :D |
| ||
Heres a good question: how would i look back when i get to a point.. like this example: test.txt test.tester = new test test2.tester = new test what i want to do is look for the "New test" everytime it sees that, go to the start of that line, and read off everything till you get to the period. then repeat(go to the next lines untill you find that new test again) we already found out how to find the "New test" but what about once you find it, go back to the start of that line, and read off till the period. any ideas? |
| ||
i tried the following:If Not Instr(strng$,"New test")=0 dot$=Instr(strng$,".") name$=Instr(strng$,Left(strng,dot)) WB3D_AddGadgetItem(catagory,name$,0,0) End If but Instr() returns found or not found, how do i get a string out of it? in the above example, it looks like it finds the right thing alright i just need to figure out how to grab it. |
| ||
maybe like this ? name$=left$(strng$, dot - 1) |
| ||
. |
| ||
. |
| ||
. |
| ||
. |
| ||
. |
| ||
Sorry for spamming your topic .. last night, the Post button was acting all weird, but it seems like it was working well after all .. |
| ||
Sorry to dig this up, but it seems i need to do somthing like this again, can anyone give me an example on how to extract the value out of this text file: test.txt blah blah blah myvalue=1000 morestuff morestuff i need to extract the '1000' value out of the myvalue line... this is a .txt document. my guess it to use the Instr() like before somehow, also the number will only be 9 digits and shorter if that means anything.. i'v been going at it for 2 hours now, is this possible or am i wasting my time? |
| ||
Don't worry, its possible.file=OpenFile("test.txt") If file=0 End EndIf Repeat txt$=ReadLine(file) If Left$(txt$,8)="myvalue=" number=Mid$(txt$,9,Len(txt$)) Notify number Exit EndIf Forever CloseFile file End |
| ||
.... man i'm stupid :D. Thank you! |