Computer Science Canada

importing numbers from text files question...

Author:  who cares? [ Wed Oct 20, 2004 5:31 pm ]
Post subject:  importing numbers from text files question...

i did this last year, but i forgot the code...

basically, i have an array and a .txt file with a number on each line... what's the code i use to import each number into a different spot in the array?

obviously i need a loop, i know that, but i forgot the syntax of everything else (like the open statement)

thanks a lot!

Author:  DanShadow [ Wed Oct 20, 2004 7:11 pm ]
Post subject: 

Well, basically...you have to read each character in a for loop. So first, create a counter, and set it to 0. Then create a String named 'input' and set it to "" (empty). Then in a for loop (1..eof(datafile)), add this in:
code:

%Assuming you declared a string called 'temp' and
%that your file reader is named 'datafile'
for i:1..eof(datafile)
counter+=1
get:datafile,temp(i)
input+=temp
end for

After this, you should create an array of integers (or real, depending on what kind of numbers your reading.) that is (1.. counter) [because counter counts the amounts of numbers read.] Then do something like this:
code:

%Assuming array of int is under var name 'num'
for i:1..counter
if strintok(temp(i)) then
num(i):=temp(i)
end if
end for

I didnt test any of this out, because If I did, I probably would have just given the answer away. But basically by those steps, you can import numbers from a .txt file into an array. I hope this helps.

Author:  Delos [ Wed Oct 20, 2004 7:11 pm ]
Post subject: 

These are amazing, you know!

File Manipulation by Dan
More file manipulation by Delta
High Scores by Dan (good example of file reading/writing

Arrays by Dodge
Arrays by tony

Author:  Andy [ Thu Oct 21, 2004 3:36 pm ]
Post subject: 

w00t for once, my tutorial is being recommended.. +10 bits


: