
-----------------------------------
Dan
Tue Aug 20, 2002 8:55 pm

[Tutorial] How to read and write to a file in Turing
-----------------------------------
How to read and write to a file in Turing


The code:

	To open the connection to the file
                                open : fileNumberVar, fileName, ioCapability

	To send something to the file
		put: fileNumberVar, stufftosend

	To get something from a file
		get: fileNumberVar, stufftoget

	To find out if it is the end of the file
		eof (fileNumberVar) %true if end of file

	To close the connection to a file
		close: fileNumberVar


How to use the code:


1st you need to open a connection to the file. For this example let's say the file is called data.txt

Getting data in

var stremin :int %a var that is an int is needed to open a file

open: stremin, "data.txt", get %this will open a connection to get data


Sending data

var stremout :int %a var that is an int is needed to open a file

open: stremout, "data.txt", put %this will open a connection to send data



2nd you need to get the data you want or send the data you want. To get or put more than one line or number you should use a loop and an eof statement.


Getting data

loop
      exit when eof (stremin)
      get: stremin, nums
end loop


Putting data

loop
      exit when eof (stremout)
      put: stremout, nums
end loop


3rd you need to close the connection to the file.


Closing the file

         close: stremout

or

         close: stremin




Putting it all together

Program to get the average of marks in a file and then put them in a new one

var stremin, stremout :int
var avage, temp, numberofmarks :int
var total :int := 0

open: stremin, "marks.txt", get

loop
      exit when eof (stremin)
      get: stremin, temp
      total +=  temp
      numberofmarks += 1
end loop

close:stremin

open: stremout, "avg.txt", put
put: stremout, total div numberofmarks
close: stremout



This was my 1st tutorial on this borad, plz tell me if you liked it. :D

A continuation of this tutorial was writen by Blade and can be found ->

-----------------------------------
Vicous
Fri Mar 07, 2003 11:31 am

Warnings!!!!
-----------------------------------
bewarned! if you write something to a file, you delete everything already in that file!!!

-----------------------------------
Vicous
Fri Mar 07, 2003 11:36 am

also
-----------------------------------
use write and read, not put and get (same code, different words) write makes the variables stored in the file smaller, but you can't read the variables manually so yes, use put to start with and move on to write when you are more confident

I can't remember the dif. between read and get, but I know read is better

-----------------------------------
Tony
Fri Mar 07, 2003 11:51 am


-----------------------------------
read and write are used to work with a binary file.

Basically put/get is for sequencial access and
write/read is for random access

In most cases, sequencial is enough, but random access has its own advanages. If you know where to look for your variables, it provides a much quicker access to your data.

Also, I belive that random-access data is actually larger in size since information is stored in "blocks" that allow that random access... so a single character and a 255 letter word would take up same amount of space.

-----------------------------------
azndragon
Thu Mar 13, 2003 4:44 pm


-----------------------------------
Is it just me, or does Turing 3.0 crash when you attempt to use read commands? It gives the error: "Read attempted on incompatible stream number 0", but it works fine in Turing 4.0. Is it a syntax difference, or just my messed up Turing?

-----------------------------------
Tony
Thu Mar 13, 2003 5:07 pm


-----------------------------------
stream number 0 means file was not open :? probably not found (or it was already open by another program)

there shouldnt be syntax differences. but as long as it works in v4, its all good, right?

-----------------------------------
Vicous
Mon Mar 17, 2003 2:12 pm


-----------------------------------
try using an assert command eg.

open: fileNo,"Filename",read
assert fileNo>0

-----------------------------------
TheZsterBunny
Sun Apr 25, 2004 2:00 pm


-----------------------------------
Yeah, I was wondering. can someone please explain in greater detail how to use read and write statements?

sorry for the abrubtness of this message - kitchen just flooded.

-bunny

-----------------------------------
Delta
Mon Apr 26, 2004 7:50 pm


-----------------------------------
Ok guys... I'm sorry to have to say this... but do it yourselves... read the turing help files!... thats what they are there for or at least ask your teacher... and if your just doing simple input/output to files then just just put/get... they are pretty much all you need.... and you can make it so they don't overwrite everything in the file.

-----------------------------------
junkpro11
Thu May 13, 2004 6:16 pm


-----------------------------------
i agree that ppl should read the turing reference, but sumtimes turing files is confusing....for some commands turing reference helps...but most of the time i get confused....maybe im just dumb but because i dun understand sumthings tatz y i post stuff on the forum. 

i read the turing help on the eof, and i read this tutorial and this is part of my code 

 open : streamin, intstr (iguess) + ".t", get
    loop
        exit when eof (stremin)
        get : stremin, sfname, slname, iage, sgender
    end loop
    close : stremin


when i try to run it, error comes up saying "attempt to read past eof"

-----------------------------------
Dan
Thu May 13, 2004 8:54 pm


-----------------------------------
that is b/c you are inputing  4 things in one get line.

if you are geting eof that means there are more then one thing but less then 4 things left to read in the file. Ether your file is wrong or you are inputing to much on that one line.

-----------------------------------
RedRogueXIII
Sat Nov 05, 2005 1:15 pm


-----------------------------------
say if you were to read the data in the file and then output it into another file, how would you do that? or write from the input onto the turing run screen?

by the way dont you just have to put 
open: location, "filename.txt", put , mod?

-----------------------------------
Cervantes
Sat Nov 05, 2005 6:34 pm


-----------------------------------
say if you were to read the data in the file and then output it into another file, how would you do that? or write from the input onto the turing run screen?
If you're copying the file exactly, just use File.Copy.  Else, get it line by line and write/display it line by line.  Or you could store things into an array if you wanted.

-----------------------------------
RedRogueXIII
Sat Nov 05, 2005 10:27 pm


-----------------------------------
Examples of how to grab data either from line to line or word by word would be nice in this tuorial. - Because the Turing reference is also pretty vague in about how to do this also.

-----------------------------------
MysticVegeta
Mon Nov 07, 2005 2:52 pm


-----------------------------------
Say you have a data file: 

"I love programming"

And here you have the code:
note this is pseudocode


get : streamno, inputVar :*
%So inputVar will be "I love programming"


But You only want the "I" to be inputVar
get : streamno, inputVar


Ah so now inputVar is only "I"

-----------------------------------
unknowngiver
Fri May 19, 2006 4:08 pm


-----------------------------------
hey
what if i want to write to a file without overwriting the current stuff...and also wt if iwant to overwrite SOME of the file but not all

for example

lets say on my program where users can change the OPTIONS of the game..

the OPTIONS are saved in a file called options.txt

so it may look like
options.txt:

skin = "redmonster"
language = "english"
background_music = "bg.mid"
bg_image = "bg.jpg"

and the user goes to the options and changes ONLY the skin to something else..let say "blue" 
how would i make it so thts the only thing tht changes??

-----------------------------------
Cervantes
Fri May 19, 2006 5:29 pm


-----------------------------------
You'd probably cycle through your data file until you reach the line that defines the skin colour, then overwrite it with the new line.

Note, however that that line has a certain number of bytes (one byte per character). You cannot change the number of bytes that line has (since that would screw up following lines). Thus, you must make sure the line you are inserting is less than or equal to the current line. If the replacement line is shorter, pad it with spaces.

You will need to use the mod option when opening the file:

open : file_stream, "filename.txt", put, mod


-----------------------------------
seawad_
Wed Nov 05, 2014 4:16 pm

Re: [Tutorial] How to read and write to a file in Turing
-----------------------------------
When I try to do:

put : file, "Hi, I like to eat hamburgers"

Turing says:

Put attempted on incompatible stream number 1.

Please help.
I would like to know how to fix this problem.

-----------------------------------
Tony
Wed Nov 05, 2014 4:35 pm

RE:[Tutorial] How to read and write to a file in Turing
-----------------------------------
how are you initializing your file variable?

-----------------------------------
ylcc23
Tue Jan 13, 2015 9:02 am

RE:[Tutorial] How to read and write to a file in Turing
-----------------------------------
When ever I try to open the .txt file I need for my program, I get syntax error at 'end'. Expected '.' I'm not sure what is causing the syntax error, as I used the code from this tutorial. Error occurs at end loop. I think it has something to do with the colon in my get, but I'm not entirely sure.


proc instructions
    var file : int
    open : file, "checkers instructions.txt", put, mod
    loop
        exit when eof (file)
        get : file
    end loop
end instructions


-----------------------------------
Insectoid
Tue Jan 13, 2015 10:15 am

RE:[Tutorial] How to read and write to a file in Turing
-----------------------------------
your get instruction is missing something.

-----------------------------------
ylcc23
Wed Jan 14, 2015 2:19 pm

RE:[Tutorial] How to read and write to a file in Turing
-----------------------------------
I've tried a different way of reading the file, but it returns a blank screen. Not sure why it won't work.


proc instructions
    open : file, "checkers instructions.txt", get
    assert file > 0
    for i : 1 .. 2
        exit when eof (file)
        get : file, list (i)
    end for
    close : file
end instructions


-----------------------------------
Insectoid
Wed Jan 14, 2015 2:57 pm

RE:[Tutorial] How to read and write to a file in Turing
-----------------------------------
Well that code doesn't output anything, so there's nothing to write to the screen.

-----------------------------------
ylcc23
Thu Jan 15, 2015 8:59 am

RE:[Tutorial] How to read and write to a file in Turing
-----------------------------------
I've added in a put statement but all that does is print 2 in the top left corner of my screen. Any advice?


proc instructions 
    open : file, "checkers instructions.txt", get 
    assert file > 0 
    for i : 1 .. 2 
        exit when eof (file) 
        get : file, list (i) 
    end for 
    put file
    close : file 
end instructions 


-----------------------------------
Insectoid
Thu Jan 15, 2015 10:23 am

RE:[Tutorial] How to read and write to a file in Turing
-----------------------------------
Putting a file will not output the contents of that file. You need to put the variables that you stored the contents of file in.

-----------------------------------
Raknarg
Thu Jan 15, 2015 8:08 pm

RE:[Tutorial] How to read and write to a file in Turing
-----------------------------------
As a side note, you are getting 2 because 'file' is an integer, and all it stores is a streamID
