April 28, 2024, 09:39:59 PM

News:

IonicWind Snippit Manager 2.xx Released!  Install it on a memory stick and take it with you!  With or without IWBasic!


How can a read a "enter" in a sequential file

Started by splakidas, March 20, 2007, 04:54:40 PM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

splakidas

I have two files. Both have 2 lines, but one of them has an enter (next line)

How can i read this ?

REDEBOLT

Examine the following program:

OPENCONSOLE
LOCATE 1,1
PRINT "File Read demonstration #7. ",
PRINT "Press <ESC> to end program"
PRINT
LOCATE 4,1

DEF key$:STRING
DEF fFile:FILE
DEF filename:STRING
DECLARE ProcessRecord

INPUT "Enter <new6> or <new7>", filename
filename = GETSTARTPATH + filename + ".txt"
IF OPENFILE(fFile, filename, "R") <> 0
    PRINT "Cannot open ", filename
GOTO EOJ
ELSE
PRINT filename + " is opened."
ENDIF

IF READ(fFile, key$) <> 0 ' Read first record.
PRINT "Empty file."
CLOSEFILE fFile
GOTO EOJ
ENDIF

DO
GOSUB ProcessRecord ' Process the record.
READ(fFile, key$)
UNTIL EOF(fFile)

GOSUB ProcessRecord ' Process the last record.
CLOSEFILE fFile

LABEL EOJ
DO ' End of program.
    key$ = INKEY$()
UNTIL key$ = CHR$(27)
CLOSECONSOLE
END

SUB ProcessRecord ' Process the record.
PRINT key$
ENDSUB


Note that it will handle either case (new6 or new7).
Note that the first "READ" will detect an empty file and suppress further processing.
Note that a SUB is defined (ProcessRecord) since it has to be called in two places.

Regards,
Bob

acelli

Splakidas,

How are you opening the file  (FILE or BFILE)?   Also, I am not clear on the problem.  Which of the scenario's below best describes the issue.  The second scenario generally occurs when reading Windows vs. Linux files.

In either case, assuming you are opening as FILE, this is how I read to <eof>.  Work's because READ (as FILE) does not bring in the <cr+lf> or <cr> or <eof>.

Thanks,
Alan




  OPENFILE(Ifile,filepathname,"R")
   WHILE READ(Ifile,Ifield$)=0
     PRINT Ifield$
   WEND
  CLOSEFILE Ifile




File=new 7                              File=new 6

Line1<cr+lf>                            Line1<cr+lf>
Line2<cr+lf>                            Line2<eof>
<eof>


File=new 7                              File=new 6

Line1<cr+lf>                            Line1<cr>
Line2<cr+lf>                            Line2<eof>
<eof>