reading a file with multiple integers on a line

17 views
Skip to first unread message

Tom Morrison

unread,
Dec 6, 2013, 9:56:52 AM12/6/13
to solde...@googlegroups.com
Just can't seem to find anything that tells me if I can do this and what the syntax would be.

I'd ideally like to be able to read a file into an array.  The file would consist of lines with 2 values per line:  two integers or possibly one or more real numbers and integers intermixed.

Is there a way to do this?

CoreBASIC Wizard

unread,
Dec 9, 2013, 8:32:04 AM12/9/13
to solde...@googlegroups.com
How are the integers separated?  By blanks?  If so, then just
 
INPUT #stream, I AS INT, J AS INT
 
This expects two integers on the stream followed by a new line.  (Well, it will disregard anything following up to the end of line, I think).
 
Alternatively, you can input the whole line and pick it apart.  Say they're separated by a comma.  This is just typed in as a thought experiment, but it should work OK
 
' Read from stream
INPUT #stream, S AS STR
 
' Separate into pieces
LET STRINGS = SPLIT(S, ",")
 
' Check syntax...
IF LEN STRINGS <> 2 THEN ' it's an error, didn't get two comma-separated values.
 
' Convert strings to floats/integers...
LET VALUES = VAL(STRINGS)
 
' If the syntax didn't conform, an error
IF NAN(VALUES(0)) OR NAN(VALUES(1)) THEN ' error
 
' Convert array to integers.
VALUES = INT VALUES
 
' Now have two integers in VALUES(0) and VALUES(1)
PRINT VALUES(0), VALUES(1)
 
 
 
You have the
Reply all
Reply to author
Forward
0 new messages