
-----------------------------------
who cares?
Wed Oct 20, 2004 5:31 pm

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!

-----------------------------------
DanShadow
Wed Oct 20, 2004 7:11 pm


-----------------------------------
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:

%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) 
%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.

-----------------------------------
Delos
Wed Oct 20, 2004 7:11 pm


-----------------------------------
These are amazing, you know!

[url=http://www.compsci.ca/v2/viewtopic.php?t=5]File Manipulation by Dan
[url=http://www.compsci.ca/v2/viewtopic.php?t=427]More file manipulation by Delta
[url=http://www.compsci.ca/v2/viewtopic.php?t=5340]High Scores by Dan (good example of file reading/writing

[url=http://www.compsci.ca/v2/viewtopic.php?t=1117]Arrays by Dodge
[url=http://www.compsci.ca/v2/viewtopic.php?t=366]Arrays by tony

-----------------------------------
Andy
Thu Oct 21, 2004 3:36 pm


-----------------------------------
w00t for once, my tutorial is being recommended.. +10 bits
