Computer Science Canada

File I/O to String

Author:  Pa|2a|)oX [ 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.

Author:  Cervantes [ Sat Feb 19, 2005 8:02 am ]
Post subject: 

read up on get .

Author:  Pa|2a|)oX [ Sat Feb 19, 2005 10:30 am ]
Post 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

Author:  Cervantes [ Sat Feb 19, 2005 10:45 am ]
Post 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).

Author:  Pa|2a|)oX [ Sat Feb 19, 2005 11:13 am ]
Post subject: 

awesome, i just figured it out and came here to post results.. thanks for you help anyways Smile


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

Author:  Cervantes [ Sat Feb 19, 2005 11:38 am ]
Post 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

Author:  MysticVegeta [ Sat Feb 19, 2005 8:43 pm ]
Post 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. Smile

Author:  Pa|2a|)oX [ Sat Feb 19, 2005 9:44 pm ]
Post 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

Author:  MysticVegeta [ Sat Feb 19, 2005 9:59 pm ]
Post subject: 

I think errortraping the "ask" thing will make it cool, cause if i put in "qwe" it will still exit lol Very Happy

Author:  Pa|2a|)oX [ Sat Feb 19, 2005 10:35 pm ]
Post subject: 

Hey what happened to all my posts and submissions?

Author:  Pa|2a|)oX [ Sun Feb 20, 2005 9:32 am ]
Post 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 Smile

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.


: