Author |
Message |
Pa|2a|)oX
|
Posted: Fri Feb 18, 2005 9:52 pm Post subject: File I/O to String |
|
|
Hello,
Just wondering if there is a way to make a variable equal to the values in a text file.
i.e. A text file has the value 100101001 made from seperate variables using the put : function. I need another string variable to be equal to that value.
Thanks for any help. |
|
|
|
|
|
Sponsor Sponsor
|
|
|
Cervantes
|
Posted: Sat Feb 19, 2005 8:02 am Post subject: (No subject) |
|
|
read up on get . |
|
|
|
|
|
Pa|2a|)oX
|
Posted: Sat Feb 19, 2005 10:30 am Post subject: (No subject) |
|
|
I already have, and unfortunately it does not answer my question.
The tutorial shows how to retrieve prestored variables with the same name. I need to make a variable equal to the entire line of the file.
Thanks |
|
|
|
|
|
Cervantes
|
Posted: Sat Feb 19, 2005 10:45 am Post subject: (No subject) |
|
|
Okay, so instead of assigning each line to your variable (:=) add each line to your variable (+=).
Also putting : * after your get might be useful (it gets the entire line, including spaces). |
|
|
|
|
|
Pa|2a|)oX
|
Posted: Sat Feb 19, 2005 11:13 am Post subject: (No subject) |
|
|
awesome, i just figured it out and came here to post results.. thanks for you help anyways
this is just an example:
code: |
var num1 : int := 10
var num2 : array 1 .. 5 of int
for i : 1 .. 5
num2 (i) := 1
end for
var total : string
var streamout : int %a var that is an int is needed to open a file
var streamin : int
open : streamout, "output.txt", put %this will open a connection to send data
put : streamout, num1 ..
for x : 1 .. 5
put : streamout, num2 (x) ..
end for
close : streamout
open : streamin, "output.txt", get
get : streamin, total : *
close : streamin
put total |
|
|
|
|
|
|
Cervantes
|
Posted: Sat Feb 19, 2005 11:38 am Post subject: (No subject) |
|
|
You are only getting one line though. I thought you wanted it all? Well, this works since you've only got one line in that file, but try adding a few lines after it. See what happens? total only equals the last line. Let's fix it:
Turing: |
var total := ""
var temp : string
var streamin : int
open : streamin, "output.txt", get
loop
get : streamin, temp : *
total + = temp
exit when eof (streamin )
end loop
close : streamin
put total
|
|
|
|
|
|
|
MysticVegeta
|
Posted: Sat Feb 19, 2005 8:43 pm Post subject: (No subject) |
|
|
Dont you think the "exit when eof(streamin)" line should be over the "get :" line incase there is no first line in the data file. |
|
|
|
|
|
Pa|2a|)oX
|
Posted: Sat Feb 19, 2005 9:44 pm Post subject: (No subject) |
|
|
Thanks, but i needed the output file for a very simple thing. One line to put, one line to get.
Its my multiplication porgram, you can check it out here:
http://www.compsci.ca/v2/viewtopic.php?t=7848 |
|
|
|
|
|
Sponsor Sponsor
|
|
|
MysticVegeta
|
Posted: Sat Feb 19, 2005 9:59 pm Post subject: (No subject) |
|
|
I think errortraping the "ask" thing will make it cool, cause if i put in "qwe" it will still exit lol |
|
|
|
|
|
Pa|2a|)oX
|
Posted: Sat Feb 19, 2005 10:35 pm Post subject: (No subject) |
|
|
Hey what happened to all my posts and submissions? |
|
|
|
|
|
Pa|2a|)oX
|
Posted: Sun Feb 20, 2005 9:32 am Post subject: (No subject) |
|
|
Wow that was weird, all of a sudden almost all my posts and programs dissapeared and my post count went down from lik 12 to 6, now everything is back to normal except my posts
I will try to error trap that exit question a bit better after I'm done the other two programs, I'm just about done subtraction. |
|
|
|
|
|
|