Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 importing numbers from text files question...
Index -> Programming, Turing -> Turing Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
who cares?




PostPosted: 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!
Sponsor
Sponsor
Sponsor
sponsor
DanShadow




PostPosted: Wed Oct 20, 2004 7:11 pm   Post subject: (No 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.
Delos




PostPosted: Wed Oct 20, 2004 7:11 pm   Post subject: (No 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
Andy




PostPosted: Thu Oct 21, 2004 3:36 pm   Post subject: (No subject)

w00t for once, my tutorial is being recommended.. +10 bits
Display posts from previous:   
   Index -> Programming, Turing -> Turing Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 4 Posts ]
Jump to:   


Style:  
Search: