Posted: Tue Aug 20, 2002 8:55 pm Post subject: [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
code:
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
code:
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
code:
loop
exit when eof (stremin)
get: stremin, nums
end loop
Putting data
code:
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
code:
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.
Computer Science CanadaHelp with programming in C, C++, Java, PHP, Ruby, Turing, VB and more!
Sponsor Sponsor
Vicous
Posted: Fri Mar 07, 2003 11:31 am Post subject: Warnings!!!!
bewarned! if you write something to a file, you delete everything already in that file!!!
Vicous
Posted: Fri Mar 07, 2003 11:36 am Post subject: 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
Posted: Fri Mar 07, 2003 11:51 am Post subject: (No subject)
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.
Posted: Thu Mar 13, 2003 4:44 pm Post subject: (No subject)
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
Posted: Thu Mar 13, 2003 5:07 pm Post subject: (No subject)
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?
Posted: Mon Mar 17, 2003 2:12 pm Post subject: (No subject)
try using an assert command eg.
Quote:
open: fileNo,"Filename",read
assert fileNo>0
TheZsterBunny
Posted: Sun Apr 25, 2004 2:00 pm Post subject: (No subject)
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
Sponsor Sponsor
Delta
Posted: Mon Apr 26, 2004 7:50 pm Post subject: (No subject)
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
Posted: Thu May 13, 2004 6:16 pm Post subject: (No subject)
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
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
Posted: Thu May 13, 2004 8:54 pm Post subject: (No subject)
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.
Computer Science CanadaHelp with programming in C, C++, Java, PHP, Ruby, Turing, VB and more!
RedRogueXIII
Posted: Sat Nov 05, 2005 1:15 pm Post subject: (No subject)
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
code:
open: location, "filename.txt", put , mod
?
Cervantes
Posted: Sat Nov 05, 2005 6:34 pm Post subject: (No subject)
RedRogueXIII wrote:
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
Posted: Sat Nov 05, 2005 10:27 pm Post subject: (No subject)
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
Posted: Mon Nov 07, 2005 2:52 pm Post subject: (No subject)
Say you have a data file:
"I love programming"
And here you have the code:
note this is pseudocode
code:
get : streamno, inputVar :*
%So inputVar will be "I love programming"