May 09, 2024, 10:35:00 AM

News:

Own IWBasic 2.x ? -----> Get your free upgrade to 3.x now.........


File reading problem

Started by Rock Ridge Farm (Larry), May 31, 2008, 07:15:53 AM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

Rock Ridge Farm (Larry)

I use openfile to open a text file.
    IF OPENFILE(myfile,GETSTARTPATH+"pwtmp.txt", "R") = 0
It enters the IF statement.
I then read data.
    WHILE READ(myfile,tmptxt)
Problem is that there is never anything in tmptxt (string)

What am I doing wrong - I just want to read in a txt file line at a time.

aurelCB

Do you try with this:
CONTROLCMD ( window | dialog, ID, @EDGETLINE, linenum, line$ {,cchLine} )

Rock Ridge Farm (Larry)


billhsln

This is my standard open and read file code:

if (openfile(iFile,iFileName,"R") <> 0)                             
' Open failed for input file, dislay error message               
PrintW1("File: " + iFileName + " Not able to Open Input")         
else                                                               
' all the opens have worked now process the file                 
' read input file until end of file                               
while (read(iFile,ln) = 0)                                       
' extract the value in the input record starting               
' in column 62 for a length of 1                               
cntr = cntr + 1                                                 
v = mid$(ln,194,1)                                             
' process data.....                                             
wend                                                             
' close all files, usually done in reverse order of opening       
closefile iFile                                                   
endif                                                               
When all else fails, get a bigger hammer.

REDEBOLT

Regards,
Bob

LarryMc

The READ command returns a 0 when successful
you should have
WHILE READ(myfile,tmptxt) = 0

Larry
LarryMc
Larry McCaughn :)
Author of IWB+, Custom Button Designer library, Custom Chart Designer library, Snippet Manager, IWGrid control library, LM_Image control library

hugh

I use openfile to open a text file.

Try this

WHILE READ(myfile,tmptxt) = 0    OPENFILE(myfile,GETSTARTPATH+"pwtmp.txt", "R")


It enters the IF statement.
I then read data.
    WHILE READ(myfile,tmptxt)
Problem is that there is never anything in tmptxt (string)

What am I doing wrong - I just want to read in a txt file line at a time.

hope this helps,

i do not pertain to be an expert!

READ will overwrite string memory if the data is longer han the dimensioned string, thus a NULL string, empty

LarryMc

Hugh
I understand how to do it but I don't understand your post above. ;)

Larry
LarryMc
Larry McCaughn :)
Author of IWB+, Custom Button Designer library, Custom Chart Designer library, Snippet Manager, IWGrid control library, LM_Image control library

pistol350

Quote from: Larry McCaughn on June 01, 2008, 08:54:53 PM
Hugh
I understand how to do it but I don't understand your post above. ;)

Larry

Hi Hugh!

I assume Larry means that you should use quotes when quoting people (just as above), which helps others to see what you are saying. :)
Regards,

Peter B.

billhsln

Does your program abort?  Which would mean that you are trying to read more data than string will hold (256 chars).  Or maybe, this is a BASIC problem, take a look at your data and check to see if it has binary 00 in it.   Basics seem to have a problem reading data with binary 00.

Bill
When all else fails, get a bigger hammer.

LarryMc

If you declared myfile as FILE and not BFILE and your file is truely a text file a READ will read one line at a time up to the CRLF.

pistol350  is absolutely correct about corrupted text files (00) and long lines

You need to define a ISTRING variable that is bigger than the longest line in the file you're trying to read.
You need to insure you are trying to read a true txt file.

Attached is a sample program that is about the minimum to read a text file.
Also is a sample text file that contains a line longer than 255 characters and a blank line.

Compile as a console program and run as is to make sure it reads my file.
Then uncomment your the line that has your file name and comment mine out.
Recompile and see if it reads your file.

If it doesn't work this time the problem is with your txt file.

Hope this helps find your problem.

Larry
LarryMc
Larry McCaughn :)
Author of IWB+, Custom Button Designer library, Custom Chart Designer library, Snippet Manager, IWGrid control library, LM_Image control library

Rock Ridge Farm (Larry)

Well Duh - I am an idiot.
After reading your post I went back and checked the myfile definition.
Sure as krap - I had bfile - changed to file and it works.
I sometimes get so involved in looking at an area in the code I forget the big picture.

Thanks

LarryMc

You're not the only one that does that sort of thing.

Happens to me all the time.

Larry
LarryMc
Larry McCaughn :)
Author of IWB+, Custom Button Designer library, Custom Chart Designer library, Snippet Manager, IWGrid control library, LM_Image control library

hugh

sorry, I got it wrong, this is my litle file from the sub bingodone() to Write totals data and display it on screen as text file



sub bingodone()
'
DEF myfile:FILE
IF(OPENFILE(myfile,"C:\\computerised bingo\\totals.TXT","W") = 0)
   WRITE myfile,"Date = "+mid$(date$,4,3)+left$(date$,3)+right$(date$,2)+"  "+"  end Time  = "+time$  ' U.K. date
   write myfile,"Total games played = "+a$
   write myfile,"Initial book startup = "+zz$+"  Initial book end# = " +ben$
   write myfile,chr$(13)
   write myfile,"..................session statistics................."
   write myfile,"Book start ="+bs$         
   write myfile,"Book end   = "+be$
   write myfile,"Books Sold = "+bss$
   write myfile,chr$(13)
   write myfile,bd1$
   write myfile,bd2$
   write myfile,bd3$
   write myfile,chr$(13)
   write myfile,"Total books sold this session = "+str$(booksoldtot)
   write myfile,"This is a Cash check on Total books sold, it should = Takings"
   write myfile,chr$(13)
   write myfile,"total books sold "+str$(booksoldtot)+"  multiplied by 00.20  =  "+"ÂÃ,£"+str$(cashcheck)
   CLOSEFILE myfile
   SYSTEM "notepad.exe" , "c:\\computerised bingo\\totals.txt"

RETURN
ENDSUB

We are not all perfect, but once again, Sorry.

Regards, Hugh

LarryMc

Hugh
QuoteWe are not all perfect.....
That implies that some are perfect. ???
Only ever heard tell of one person who was perfect ;)

For future ref:IF(OPENFILE(myfile,"C:\\computerised bingo\\totals.TXT","W") = 0)
could be changed toIF(OPENFILE(myfile,getstartpath+"totals.TXT","W") = 0)
That will make it look for the txt file in whatever directory the bingo.exe is in.
That's if you gave someone a copy of your program.  That would allow them to put it in whatever directory they wanted to.
Might come in handy for you in the future,


Larry
LarryMc
Larry McCaughn :)
Author of IWB+, Custom Button Designer library, Custom Chart Designer library, Snippet Manager, IWGrid control library, LM_Image control library

hugh

//javascript:void(0);
Roll Eyes

Thanks Larry, using that piece of code now.
and as us Irish say, be jepers, ive forgotten more than you all probably know!

And i still cant remember it.

regards

Hugh