[futurebasic] Re: [FB] Importing lots of text

Message: < previous - next > : Reply : Subscribe : Cleanse
Home   : October 1999 : Group Archive : Group : All Groups

From: Rick Brown <rbrown@...>
Date: Sun, 03 Oct 1999 11:19:54 -0500

James Tolchard wrote:

> I am trying to write a program in FB2 which imports a text file so I can
> work with it in memory and then write it to another text file.
>
> However, I am currently getting stuck at the 255 character limit. What I
> would ideally like to do is to have the text file imported into a string
> array, with each element holding 255 characters.
>
> However, using LINE INPUT (which is really the only way I know how to load
> in a text file), I can only get the *first* 255 characters of each line.
> What I end up with is the first 255 characters of the first line in element
> 1, the first 255 characters of the second line in element 2, etc etc.
>
> What I want is the first 255 characters of the text file in element 1, the
> next 255 characters of the text file in element 2, etc etc.

You can use READ# rather than LINE INPUT.  The following statement reads
exactly 255 characters into the variable myString$:

   READ#1, myString$;255

Note that the read operation might (depending on the content of your file)
cross an end-of-line boundary, and in that case your string variable will also
contain the ASCII carriage return(s) that separate the lines.

The only problem with this method is that, if the total size of your file is
not an exact multiple of 255 bytes, the very last READ instruction will reach
the end of the file before 255 bytes are read--this results in a file error.
To overcome this, you can read 255 characters for each READ instruction
_except_ the final one, which will read a smaller number of bytes.  Like this:

bytesLeft& = LOF(fileID,1)  'Total number of bytes in file
i = 0
DO
  IF bytesLeft& > 255 THEN bytesToRead = 255 ELSE bytesToRead = bytesLeft&
  READ#fileID, strArray$(i);bytesToRead
  bytesLeft& = bytesLeft& - bytesToRead
  INC(i)
UNTIL EOF(1)

Hope this helps.
- Rick