Computer Science Canada [Tutorial] How to read and write to a file in Turing |
Author: | Dan [ 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
Sending 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
Putting data
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
This was my 1st tutorial on this borad, plz tell me if you liked it. A continuation of this tutorial was writen by Blade and can be found ->http://www.compsci.ca/v2/viewtopic.php?t=427 |
Author: | Vicous [ 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!!! |
Author: | Vicous [ 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 |
Author: | Tony [ Fri Mar 07, 2003 11:51 am ] |
Post 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. |
Author: | azndragon [ Thu Mar 13, 2003 4:44 pm ] |
Post 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? |
Author: | Tony [ Thu Mar 13, 2003 5:07 pm ] |
Post 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? |
Author: | Vicous [ Mon Mar 17, 2003 2:12 pm ] |
Post subject: | |
try using an assert command eg. Quote: open: fileNo,"Filename",read
assert fileNo>0 |
Author: | TheZsterBunny [ Sun Apr 25, 2004 2:00 pm ] |
Post 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 |
Author: | Delta [ Mon Apr 26, 2004 7:50 pm ] |
Post 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. |
Author: | junkpro11 [ Thu May 13, 2004 6:16 pm ] | ||
Post 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
when i try to run it, error comes up saying "attempt to read past eof" |
Author: | Dan [ Thu May 13, 2004 8:54 pm ] |
Post 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. |
Author: | RedRogueXIII [ Sat Nov 05, 2005 1:15 pm ] | ||
Post 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
|
Author: | Cervantes [ Sat Nov 05, 2005 6:34 pm ] |
Post 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. |
Author: | RedRogueXIII [ Sat Nov 05, 2005 10:27 pm ] |
Post 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. |
Author: | MysticVegeta [ Mon Nov 07, 2005 2:52 pm ] | ||||
Post subject: | |||||
Say you have a data file: "I love programming" And here you have the code: note this is pseudocode
But You only want the "I" to be inputVar
Ah so now inputVar is only "I" |
Author: | unknowngiver [ Fri May 19, 2006 4:08 pm ] | ||
Post subject: | |||
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:
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?? |
Author: | Cervantes [ Fri May 19, 2006 5:29 pm ] | ||
Post subject: | |||
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:
|
Author: | seawad_ [ Wed Nov 05, 2014 4:16 pm ] |
Post subject: | 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. |
Author: | Tony [ Wed Nov 05, 2014 4:35 pm ] |
Post subject: | RE:[Tutorial] How to read and write to a file in Turing |
how are you initializing your file variable? |
Author: | ylcc23 [ Tue Jan 13, 2015 9:02 am ] | ||
Post subject: | 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.
|
Author: | Insectoid [ Tue Jan 13, 2015 10:15 am ] |
Post subject: | RE:[Tutorial] How to read and write to a file in Turing |
your get instruction is missing something. |
Author: | ylcc23 [ Wed Jan 14, 2015 2:19 pm ] | ||
Post subject: | 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.
|
Author: | Insectoid [ Wed Jan 14, 2015 2:57 pm ] |
Post subject: | 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. |
Author: | ylcc23 [ Thu Jan 15, 2015 8:59 am ] | ||
Post subject: | 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?
|
Author: | Insectoid [ Thu Jan 15, 2015 10:23 am ] |
Post subject: | 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. |
Author: | Raknarg [ Thu Jan 15, 2015 8:08 pm ] |
Post subject: | 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 |