File Input Output using arrays and loops
Author |
Message |
AsianNinja
|
Posted: Wed Dec 25, 2013 8:57 am Post subject: File Input Output using arrays and loops |
|
|
What is it you are trying to achieve?
To "put" different int into a seperate file using loops and arrays, so i can reset the array every time, in the end i want a file with several lines, each containing a series of integers.
What is the problem you are having?
But every time i try entering a second line, i either replace the previous lines, or i continue on the previous line, making it impossible for me to differentiate each line.
Describe what you have tried to solve this problem
so far i tried closing the file, then opening it again for the next line, but it just overwrites... im kinda stuck and google doesnt turn in any uuseful answers
Post any relevant code (You may choose to attach the file instead of posting the code if it is too long)
Turing: |
var test : array 1 .. 10 of int
for i : 1 .. 10
test (i) := i
end for
var fileno : int
var filename := "scores.txt"
put filename
open : fileno, filename, put
for decreasing i : 10 .. 1
put : fileno, test (i), " " ..
end for
close : fileno
open : fileno, filename, put
for i : 1 .. 10
put : fileno, test (i), " " ..
end for
for decreasing i: 10 .. 1
put : fileno, test (i), " " ..
end for
close : fileno
open : fileno, filename, get
for i : 1 .. 10
get : fileno, test (i)
end for
close : fileno
for i : 1 .. 10
put test (i)
end for
|
Please specify what version of Turing you are using
turing 4.1.1 |
|
|
|
|
|
Sponsor Sponsor
|
|
|
Raknarg
|
Posted: Wed Dec 25, 2013 1:21 pm Post subject: RE:File Input Output using arrays and loops |
|
|
The idea is that you would put the file all in at once, or find some way to store what was already in it. If you put something into a file, it erases what was already there and fills it with whatever you put in. Find some way so that you put all the information in at the same time. |
|
|
|
|
|
AsianNinja
|
Posted: Sat Dec 28, 2013 9:53 am Post subject: Re: File Input Output using arrays and loops |
|
|
ok alright, i found a way to store every into an array first. Thanks a lot |
|
|
|
|
|
np_123
|
Posted: Sun Dec 29, 2013 1:57 pm Post subject: Re: File Input Output using arrays and loops |
|
|
Outputting all the data into the file at once is usually the best solution, but adding data to the end of a file may prove helpful in certain situations
From Turing Documentation
Quote: To append to a file, the file must be opened with the mod and seek capability and then there must be a seek to the end of file
Turing: |
var fileno : int
open : fileno, "scores.txt", put, mod, seek /* mod (modify) allows you to change the contents of the file */
seek : fileno, * /* This searches for the end of the file ex. the last character */
put : fileno, "This appears at the end of the file"
close : fileno
|
|
|
|
|
|
|
AsianNinja
|
Posted: Mon Dec 30, 2013 9:14 pm Post subject: Re: File Input Output using arrays and loops |
|
|
Cool, i didnt know about mod and seek, ill keep those in mind. But just to clarify, mod allows you to continue on the previous file, without over writing it, and seek allows you to locate where to put/get in the file, in this case, the end of the file. right? |
|
|
|
|
|
np_123
|
Posted: Mon Dec 30, 2013 9:58 pm Post subject: RE:File Input Output using arrays and loops |
|
|
Correct, mod allows you to change the contents of the file.
Using seek correctly you can avoid overwriting the contents. |
|
|
|
|
|
|
|