Reading multiple variables from file
Author |
Message |
xHoly-Divinity
|
Posted: Mon Feb 12, 2007 4:15 pm Post subject: Reading multiple variables from file |
|
|
Suppose you had a .txt file that looked something like
312
23552
234
62
You are only told what the variables are going to be so for instance, it'll just indicate that there will be four 'int' variables saved in the doc. How would you access these? Is there an ezier way than using 'seek' because seek does it based on bytes. The reason I'm asking is for the CCC, your inputs come from the file. I tried to experiment with the seek and get but couldn't get it quite to work 100%. Thanks |
|
|
|
|
![](images/spacer.gif) |
Sponsor Sponsor
![Sponsor Sponsor](templates/subSilver/images/ranks/stars_rank5.gif)
|
|
![](images/spacer.gif) |
Cervantes
![](http://compsci.ca/v3/uploads/user_avatars/1023105758475ab2e040bde.jpg)
|
Posted: Mon Feb 12, 2007 4:45 pm Post subject: RE:Reading multiple variables from file |
|
|
I'm not sure I understand your question, but why not just run a for loop from 1 to, in this case, 4, and get your data into an array?
I've done the CCC this works. They tell you exactly what you can expect, and they separate data by line breaks, (usually). get also works based on line breaks, so you're good. Every other programming competition I've done also works this way. |
|
|
|
|
![](images/spacer.gif) |
xHoly-Divinity
|
Posted: Mon Feb 12, 2007 5:31 pm Post subject: Re: Reading multiple variables from file |
|
|
code: |
const txtFile : string := "myfile.txt"
var docGet : int
var doc, x : int
open : doc, txtFile, get
get : doc, docGet
x := docGet
close : doc
|
So if the txt contained lets say the numbers:
18
2939
235
and I wanted to save the third value (in this case 235) to the variable 'x,' what kind of command would I need? |
|
|
|
|
![](images/spacer.gif) |
syntax_error
![](http://compsci.ca/v3/uploads/user_avatars/196798963948cf16794b6e5.jpg)
|
Posted: Mon Feb 12, 2007 6:33 pm Post subject: Re: Reading multiple variables from file |
|
|
sorry i also had a question related to this...............
ummmm when you are reading in text files and your given that there are words serpreted by spaces and on different lines.
Then how would one go by tell the computer to read data in and read it in different lines......
like for example you want to read in only the the second word in a file at each new line how would one go and do something of this sort?? ![Question Question](http://compsci.ca/v3/images/smiles/icon_question.gif) |
|
|
|
|
![](images/spacer.gif) |
Cervantes
![](http://compsci.ca/v3/uploads/user_avatars/1023105758475ab2e040bde.jpg)
|
Posted: Mon Feb 12, 2007 9:47 pm Post subject: RE:Reading multiple variables from file |
|
|
xHoly-Divinity:
code: |
% open file
var x : int
get : fileStream, x
get : fileStream, x
get : fileStream, x
|
or in general, to get the n'th item into variable x:
code: |
% open file
var x : int
for i : 1 .. n
get : fileStream, x
end for
|
Of course chances are you need all the data in the file, so I don't know why you'd be skipping past all the good stuff at the beginning.
syntax_error: What is your question? Are you trying to get the second word in each line?
code: |
% open file
% assume file has n lines
var arr : array 1 .. n of string
for i : 1 .. n
get : filestream, arr (i)
get : filestream, arr (i)
end for
|
Or to get the k'th word:
code: |
% open file
% assume file has n lines
var arr : array 1 .. n of string
for i : 1 .. n
for rep : 1 .. k
get : filestream, arr (i)
end for
end for
|
But again, I don't know why you would do this, as you're ignoring a whole lot of good data that you almost certainly need. You usually need all the data they give you in CCC or related problems.
This works because get gets data until a space or newline is encountered. If you want to get data until a new line is encountered (ie. get an entire line at once), use code: | get : fileStream, x :* | (I hope I got that syntax right. It's been a while.) |
|
|
|
|
![](images/spacer.gif) |
Clayton
![](http://compsci.ca/v3/uploads/user_avatars/1718239683472e5c8d7e617.jpg)
|
Posted: Mon Feb 12, 2007 10:13 pm Post subject: Re: Reading multiple variables from file |
|
|
yes you got the syntax right, be be aware that that only works for strings. If you have numerous integers on the same line seperated by whitespace, (if memory serves), you can use a record to get all of those integers in one get. ie:
code: |
var test :
record
x, y, z, q, p : int
end record
var stream : int
open : stream, "test.txt", get
get : stream, test
|
supposing that we have a text file like so:
test *should* hold the values of [1 45 7380 23 234 2] (each corresponding to a variable within the record). I'm not entirely sure this works, as I'm on Linux currently, so good luck on figuring this out ![Very Happy Very Happy](http://compsci.ca/v3/images/smiles/icon_biggrin.gif) |
|
|
|
|
![](images/spacer.gif) |
|
|