Computer Science Canada

How do u put stuff into a text file!

Author:  nate [ Sun Jun 15, 2003 7:10 pm ]
Post subject:  How do u put stuff into a text file!

Like you get a value and then you put that value into the file!

Author:  hskhan [ Sun Jun 15, 2003 7:24 pm ]
Post subject: 

code:
var stream : int
var line : string
%filename goes in place of inst.txt
open : stream, "inst.txt", put

% Write the file
loop

line:= "what ever"
    exit when eof (stream)
    put : stream, line : *


end loop

Author:  nate [ Sun Jun 15, 2003 7:31 pm ]
Post subject: 

don't you have 2 close the file at the end!?

Author:  hskhan [ Sun Jun 15, 2003 7:36 pm ]
Post subject: 

nate wrote:
don't you have 2 close the file at the end!?

wopdie do. Razz
code:

close : stream

Author:  naoki [ Sun Jun 15, 2003 9:35 pm ]
Post subject: 

i'm too lazy to check if it errors, but i'm pretty sure you can't put to a file like that

going "put: stream, name : *" is wrong, you don't need the * when putting, only when you get a string

Author:  Martin [ Sun Jun 15, 2003 9:43 pm ]
Post subject: 

Yeah, the above isn't goign to work at all. Here's how you do it.

code:
var fin : int
open : fin, "inputfile.txt", get
var x : int
var y : string
var z : string
get : fin, x %will read one integer
get : fin, y:* %will read an entire line
get : fin, z %will read one word.
close : fin


That's file input
suppose your file looked like this:
code:
1 2 3
hello world


x would have a value of 1 (the first integer, y would have a value of "2 3", and z would have a value of 'hello'

for file output, you do the following
code:
var fout : int
open : fout, "outputfile.txt", put
put : fout, "THIS IS NOW IN THE FILE"
put : fout, 1 + 2 + 3
close : fout


outputfile.txt now looks like this:
code:
THIS IS NOW IN THE FILE
6


Also, if outputfile.txt doesn't exist it will be created. If it does exist, opening it for put will delete it and replace it with a new blank file.
Hope that clears it up.


: