Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 [Tutorial] How to read and write to a file in Turing
Index -> Programming, Turing -> Turing Tutorials
Goto page 1, 2  Next
View previous topic Printable versionDownload TopicRate TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
Dan




PostPosted: 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. Very Happy

A continuation of this tutorial was writen by Blade and can be found ->http://www.compsci.ca/v2/viewtopic.php?t=427
Computer Science Canada Help with programming in C, C++, Java, PHP, Ruby, Turing, VB and more!
Sponsor
Sponsor
Sponsor
sponsor
Vicous




PostPosted: 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




PostPosted: 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




PostPosted: 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.
Latest from compsci.ca/blog: Tony's programming blog. DWITE - a programming contest.
azndragon




PostPosted: 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




PostPosted: Thu Mar 13, 2003 5:07 pm   Post subject: (No subject)

stream number 0 means file was not open Confused 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?
Latest from compsci.ca/blog: Tony's programming blog. DWITE - a programming contest.
Vicous




PostPosted: 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




PostPosted: 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
Sponsor
sponsor
Delta




PostPosted: 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




PostPosted: 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




PostPosted: 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 Canada Help with programming in C, C++, Java, PHP, Ruby, Turing, VB and more!
RedRogueXIII




PostPosted: 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




PostPosted: 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




PostPosted: 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




PostPosted: 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"


But You only want the "I" to be inputVar
code:
get : streamno, inputVar


Ah so now inputVar is only "I"
Display posts from previous:   
   Index -> Programming, Turing -> Turing Tutorials
View previous topic Tell A FriendPrintable versionDownload TopicRate TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 2  [ 26 Posts ]
Goto page 1, 2  Next
Jump to:   


Style:  
Search: