TIP CORNER - INPUTTO$

Home

Color Controls

Colored Textboxes

INPUTTO$

Spotlight

Development4Life?

console program

console API

Volunteer

Newsletter Help

Index

INPUTTO$(#handle, delim$))

This function reads from the file #handle up to the character specified in delim$ or to the end of the line, whichever comes first. This is handy for reading files which contain comma, tab, pipe, or other delimited items.

  'display each comma delimited item in a file on its own line
  open "inputto test.txt" for input as #test

  while eof(#test) = 0
      print inputto$(#test, ",")
  wend
  close #test

Remember, when reading input from files in a loop, always check for the End Of File "EOF" marker to avoid an error that could crash your program. EOF(#file) will be 0 if the end of file has not yet been reached. When it is no longer equal to 0, then stop reading input and close the file.

Here is a simple demo that reads data into a file, and delimits it with the pipes character. This is found on the keyboard as the uppercase version of the backslash "|". Don't forget... you must end every print statement with a semicolon if you do not want to print a carriage return [chr$(13)] to the file!

'create a simple, tab delimited file:
open "atest.dat" for output as #f

print #f, "Gundel, Carl" + "|";
print #f, "Bush, George W." + "|";
print #f, "Bond, James Bond" + "|";
print #f, "Liberty BASIC" + "|";
print #f, "Wow!" + "|";
print #f, "Hello, World?" + "|"

close #f

'now open the file:
open "atest.dat" for input as #g

'read the file directly into an array:
while EOF(#g)=0
    a$(total)=inputto$(#g, "|")
    total=total+1
wend

close #g

'print contents of array:
for i = 0 to total
    print a$(i)
next


'read in a single line from the file:
open "atest.dat" for input as #h
    line input #h, item$
    print item$
close #h

end

Note that using the pipes to delimit items in the file allows you a lot of flexibility when managing the data that is read from the file. It is very easy to fill an array directly with items from the pipes delimited file.

Look at the last routine in the program. It reads in an entire line from the file. You can see what it looks like as a single line with a pipes character between each item.


Home

Color Controls

Colored Textboxes

INPUTTO$

Spotlight

Development4Life?

console program

console API

Volunteer

Newsletter Help

Index