Computer Science Canada [Tutorial] File I/O |
Author: | Clayton [ Wed Jun 21, 2006 3:12 pm ] | ||||||||||||||||||||||||||||||
Post subject: | [Tutorial] File I/O | ||||||||||||||||||||||||||||||
File Input/Output
Well i was looking through the Improving Tutorials thread and I saw that the File I/O was said to be a little rough around the edges so ive decided to try and fix it up a little bit . Whats Going to be Covered All commands associated with File I/O (except mod, and if anybody could PM me an explanation of what it does it would be much appreciated), which are: open close put get read write seek tell eof I will show you an example of each command in action as well as an explanation of how to use it. How do I use a File to Begin With? The answer to that question is really quite simple, and I will go over that right now. To use a file to input or take information from you must first open the file.
Alright, what I've done here is opened the text file "filename.txt" along the file path "stream". I've done this because you need to have an integer file path (which does not get initiated) so that the computer knows which file you are using in the event of more than one file being open at a time. Just ignore the "put" part of that open statement for now, we will come back to it later. Now the actual syntax for open is as such Syntax:
Also, whenever you are done using the file, you should close it, even if you aren't going to be using it again in the program. This is done automatically whenever the run window is closed, but it is good practice to close a file when done with it. The syntax is as follows:
So in our example above:
I've got the file open, now what? Another good question. Now we come back to the "put" part of the open statement. In the open statement that we made we can add several things on the end, each of which allows us to do different things with our file. Here is the list of commands that you can use along with a description of each: put: Well we all know that put outputs things to the screen, but did you know it outputs things to file as well? When putting things to file the syntax is slightly different however, it is as such:
Also note that you can put integers, real numbers, and booleans to file as well, and the process is exactly the same:
You can also put variables to file:
Simple as that and it works with any variable type . I can put stuff to the file, now I want to get it back This is a simple thing to do as well. Very similiar to put, however there is a couple of differences. Like input from the run window you get information from your source (in this case our text file) with the command get. The syntax for get is the same as put for the most part other than the fact the command is get. Here is the syntax:
Now when getting a value from a file you have to be careful that you are indeed getting a value that coincides with your variable type. For example, you can't give your int a value of "hello", it just doesn't work. Here is an example of how get works:
It's as easy as that. Just make sure that your variable type is the same as the data that you are getting. What if I don't want my file to be read? Well there are a couple of ways to do this. You can use the read and write commands, or you can learn to encrypt your files (note this is an advanced technique not for beginners), which we will not be going over in this tutorial. Now, you can semi-protect your information in a file by using the read and write commands. These commands are more or less identical to put and get with the exception that they write to a file in binary code, whereas put and get write to files in source, which means anyone with a text editor (like notepad) can read them. Read and Write Reading and writing to a file is very simple, it uses the same idea as put and get, but with different syntax, and it also uses different I/O modes: read and write Here is an example of how to use each: read (similiar to get)
And for write (similiar to put)
Notice the similiarities to put and get? Great, I know how to get my variables in and out of a file, what if a want to go to a certain line? With the tell and seek commands, thats how! tell and seek are two commands that have to do with positon in a file. tell can record a current position in the file for later use (like in a saved game file for re-loading), and seek can go to a given position in the file to start I/O operations. The syntax for both tell and seek are very similiar: tell:
seek:
As you can see, they both use the same variables: fileNumber (called stream in this tutorial), and filePosition (the binary position in the file). Using a combination of seek and tell you can keep track of positions in files and go back to them. Here is an example of both in action (Note that the file has to be in the "seek" mode to be able to use either seek or tell)
So all we've done there is found the initial location of data entry, recorded it, put myText to file, then gone back to where it was for future use. Great, but what if I have more than one thing in my file? Put it in a loop! If you haven't learned flexible arrays yet i suggest that you do. When dealing with file I/O flexible arrays will be your best friend. Now, when you are dealing with multiple (sometimes unknown) amounts of data coming in from a file, it is almost necessary to create an array. When you have done this, you can put the array in a loop getting one piece of data from the file one line at a time. Then when you have come to the end of the file you can exit the loop. Here is an example of how to get multiple lines of data from a file. Note I will be using flexible arrays here, if you don't understand them, don't worry, just concentrate on the idea of getting multiple lines of data from the file.
Now you notice how I used this little command "exit when eof (stream)" ? eof is a command (meaning end-of-file) that calls a function that returns a boolean. If it is true, that means we have reached the end of the file and we exit our loop, otherwise, we keep going until eof = true. Note that you have to tell eof which file number you are using so that it knows which file to check. This is especially important if you have more than one file open. For putting your multiple lines of data to a file the process is the same, but you should put all of your data into the file in a for loop with a dynamic upper bound. Problems
1.Create a file in a text editor called "marks.txt" and fill it with one mark per line for at least 15 lines. Then, create a program that will import these marks into an array in your program and then calculate the averages of the marks and output the average on the screen. 2.Create a file in a text editor filled with at least 15 names and save it as "names.txt". Create a program to import these names from the file and then sort them, then put them on the run window sorted alphabetically, and into that file sorted alphabetically. 3.Create a program that creates 100 random numbers from 1-500, puts them in a file called"numbers.txt". Then create another program to import those same numbers, sort them, and putt hem back in the file sorted. Closing That is my tutorial on file I/O, if anybody has any questions, plz post or PM me and I willbe glad to answer them, anything you see that needs fixing should be PM'ed to me as well. I hope that you guys learned something from this tutorial. |
Author: | aldreneo [ Wed Jun 21, 2006 3:17 pm ] |
Post subject: | |
Good job, I was having trouble with the old "File O/I Tutorial" +10 Bits |
Author: | Delos [ Wed Jun 21, 2006 4:42 pm ] | ||||||||
Post subject: | |||||||||
Great work. A couple things you should mention in there: -
- write can only accept vars as parameters. No raw text. I.e., this won't work:
- seek/tell: Files do not understand the concept of 'lines'. Everything in a file is a long strings of characters, and end-of-line characters are no exception. Although a file might be rendered by a text pad as having lines, those breaks are actually characters. Hence:
is roughly:
Where '/n' is a single character - the line break. - Additional parameters to read/write. I have found some of these insanely useful. Notice how if you were to create a file with nothing more than a single character in it, the file is still 32.0 KB? If I recall, using one of the additional paramters in 'write' ('actual size' methinks) can allow you to write files that are their actual size...so a single character file would be a few bytes, as oppoesd to 32.0 KB. I'm not positive whether the 'few bytes' registers as 'Actual Size' or 'Size on Disk'. Even if it doesn't, they're good to know about. You could add those to an 'advanced' section of the tut. - Speaking of which, I would advise you to break this down into 3 sections: Beginner, Intermediate, Advanced...use these as rough guidelines: +Beginner: put/get +Intermediate: read/write +Advanced: further read/write, seek/tell Questions at the end of each would be prime. Once they're done, you can post each in a seperate post (in this thread!) and either Cervantes or myself will then rearrange a few posts to ensure that all 3 parts follow each other (so don't worry about others posting in here). I'll let Cervantes handle the bits - seeing as he has so many to spare these days . |
Author: | Clayton [ Wed Jun 21, 2006 4:52 pm ] |
Post subject: | |
All right thanks for the ideas! I'll begin to work on a Beginner, Intermediate, Advanced level tutorial and delete this one eventually thanks for the notice on write only being able to accept variables, I never knew that, seeing as I only use variables when passing information to file anyways |
Author: | Cervantes [ Wed Jun 21, 2006 10:12 pm ] |
Post subject: | |
Looks nice. Beginner/Intermediate/Advanced is a good idea. File I/O is a big topic -- probably too much for the average Turing high school programmer to digest in one sitting. I think some deeper expainations are warranted. You really glossed over how get works. That is, it gets text until whitespace. get with the additional ": *" gets a whole line. It's useful to think of having an imaginary cursor in your file when you're getting data from it. The cursor starts at the beginning of the file (bytes = 0) and moves along. Emphasize that this imaginary cursor can't move backwards -- only forwards. More detail regarding read and write: One of the greatest things about read/write is that they can work with large data structures as easily as they can work with the primitive data types. That is, I define a custom data type that is really big and has lots of fields. Variables of that data type can be written to a data file just by writing the variable. The entire variable can then be read in using only one read line. Using put/get, I would have to manually put/get each field of the record. seek/tell: Emphasize that the integer parameter that seek accepts and the value tell stores in the variable you give it (oh gosh, what a horrible side-effect).... emphasize that this integer is the number of bytes since the beginning of the file. Delos' explanation of file structure is useful here. Your examples and sample problems are all variations on the same thing. File I/O can be more complex than simply inputting and outputting line after line linearly. Consider a data file that represents a maze. A "#" character might represent a wall while a "." character might represent an empty space. The data file would have lots of these characters on a single line. You'd want to read this information into a 2D array. And of course, mod. mod allows us to alter the contents of a file without totally erasing it. This is most useful for appending data onto the end of a file. It is also possible to overwrite data in the middle of a file (you might seek to that spot in the middle of the file). However, it must be understood that overwriting data in the middle of a file is just changing the bits in those specific positions in the file. It is not inserting new bits into the middle of the file.It's rather like using the 'replace' style of cursor in a text editor rather than the 'insert' style. Edit: Oh, and there's the matter of bits, of course. I'm going to only give 100 bits now, but there's much more to come if you add these details Delos and I have mentioned. |
Author: | War_Caymore [ Fri Sep 22, 2006 11:21 am ] |
Post subject: | |
hey, this is war_caymore saything taht i read this and i msut say, i can actualy open a program into turing now. i was confused by the Turing Refrence help and this all made it clear to me. you can all hope to see some good things from me within the next couple of months. war_caymore |
Author: | BenLi [ Thu Sep 28, 2006 4:10 pm ] |
Post subject: | |
thank you this has been helpful in re-learning file io however, you stated that write and read writes files in binary. Why is it, after i used write to create a txt file that i can still open it and it looks like normal text? |
Author: | Cervantes [ Thu Sep 28, 2006 6:06 pm ] |
Post subject: | |
BenLi wrote: however, you stated that write and read writes files in binary. Why is it, after i used write to create a txt file that i can still open it and it looks like normal text?
All files are binary files. Everything the computer does is in binary. The only reason some files are called binary files whereas some are called ASCII files (or some other name) is because binary files are intended to be read assuming each group of 0's and 1's is a number. So we might be working with 2-byte integers, meaning reading a binary file involves reading 2 bytes at a time and interpreting them as a number. With ASCII files, however, the binary data is intended to be read as 1-byte integer, then converted to a character based on the ASCII table. So it's entirely possible that a binary file that contains some numbers could be perfectly coherent text in English. However, chances are more likely that you sent the english text to the file using write, with the secret intention that it would be english text. One final note about binary files: they are often organized into blocks. Each block might be 256 integers. So if you're reading 2-byte integers, each block will be 512 bytes. This kind of organization makes it easy to seek to the place you want (you have to know exactly where you want to go, though, or nothing will make sense), but it often means sections of the file will be empty (contain only 0's). |
Author: | lord_pwnerer [ Tue Oct 17, 2006 7:48 pm ] | ||
Post subject: | |||
I've read all the turing tutorials on File I/O and still am uncertain about how to read a certain line, and then another certain line. And so on and so forth. I know that
gets the the first line and I've heard of seek, but don't quite understand it. If someone could please explain this to me I would be gratefull. Thanks |
Author: | Clayton [ Tue Oct 17, 2006 8:29 pm ] |
Post subject: | |
ok, do you understand how each character is stored in memory? If your file looked like this (physically): File wrote: Hello World ! So if your file looked like this physically, in memory, it would look something like this (with newline characters (\n) being one character) Memory wrote: Hello\nWorld\n! Now, when this is stored in memory, each character takes up one byte of memory. When you seek to a place in a file, you go to the byte position given. IE. If you had this file and you wanted to seek to "World", you would need to seek to byte 7 (Hello\n is seven bytes of memory), from there you can do what you want with the file (eg read from it or mod it) however, you cannot change the arrangement of the bytes in the file, when you mod something, you change the exact same amount of bytes as you took out. Sorry if I haven't explained this very well, if someone else wants to take a stab at it be my guest |
Author: | lord_pwnerer [ Wed Oct 18, 2006 4:24 pm ] | ||
Post subject: | |||
my bad, I had it working, that's what I had, lol, is there a code to tell it when to stop? like read from seven to 10? |
Author: | Cervantes [ Wed Oct 18, 2006 7:40 pm ] |
Post subject: | |
lord_pwnerer wrote: my bad, I had it working, that's what I had, lol, is there a code to tell it when to stop? like read from seven to 10?
Nope. You would do that yourself with for loops or recursion. I don't remember any way to just get one character (byte) from a file. That would be useful here. |
Author: | Silent Avenger [ Wed Oct 18, 2006 8:08 pm ] |
Post subject: | |
Wow nice tutorial Freakman now if I ever have time to program with turing at school I'll know what I'm doing. |
Author: | darkangel [ Mon Dec 10, 2007 8:40 pm ] | ||
Post subject: | Re: [Tutorial] File I/O | ||
to add...
will find the end of said file so you can add things unto the file without erasing the old. This (to me at least) is a lot easier than making flexible arrays....which are annoying. |
Author: | Mackie [ Sun Feb 03, 2008 2:01 pm ] |
Post subject: | RE:[Tutorial] File I/O |
How would I create a file in Turing? I'm making an application and it requires saving you work. |
Author: | Tony [ Sun Feb 03, 2008 2:05 pm ] |
Post subject: | RE:[Tutorial] File I/O |
If you write to the file that doesn't exist, the system will create one for you. |
Author: | anna12345 [ Thu Apr 24, 2008 10:25 am ] |
Post subject: | Re: [Tutorial] File I/O |
what is filenumber exactly? |
Author: | riveryu [ Sat May 31, 2008 10:54 pm ] |
Post subject: | RE:[Tutorial] File I/O |
I think filenumberer is the ID given to the file. You have more than one file open and you refer to a specific by its ID. Just as using pictures, you have a pic ID. |
Author: | PesticideUse [ Sun Aug 24, 2008 10:21 pm ] |
Post subject: | Re: [Tutorial] File I/O |
Hey, hopefully someone can help. Im trying to save 14 variables, i know how to, just something is wrong. when i go to save an error occurs, "I/O attempted on unopened stream number -10. Opened with fail message 'File "C:\File.txt" is already open (may not be opened twice)' but when i open the text document it has saved the first four lines i need i have a section of code above that loads the info that could be the problem but i put close(streamNumber) and close : streamNumber underneath so that should close the file right? I'm probably doing something wrong i don't know... if anyone has a clue please help, and if you need the code ill post the game |
Author: | riveryu [ Mon Aug 25, 2008 11:26 am ] |
Post subject: | RE:[Tutorial] File I/O |
That's suppose to be in the help section. Either some mod move or it or you start another topic in Help. Anyways. Whats the four lines that you need? Are they the first 4 variables? If you get an error like that its usually something wrong with your loop. Make sure your open statement is not in the loop you use to write, but I doubt you made this mistake. Post some code with your topic in Help. |
Author: | PesticideUse [ Mon Aug 25, 2008 6:12 pm ] |
Post subject: | Re: [Tutorial] File I/O |
Omg im dumb, wrong forum and the simplest answer ever, sorry about that everyone |
Author: | TheFerret [ Sat Oct 11, 2008 3:04 pm ] |
Post subject: | Re: [Tutorial] File I/O |
Added to wiki: Turing_File_IO |
Author: | FreshAir [ Wed Jan 21, 2009 7:52 pm ] | ||||
Post subject: | Re: [Tutorial] File I/O | ||||
You should also add how to write to a file in another directory. In turing this is done by doing a double backslash (//) instead of a single backslash (/) ex.
Mod Edit: Remember to use syntax tags! Thanks
|
Author: | uknowhoiam [ Tue May 12, 2009 6:49 pm ] | ||
Post subject: | RE:[Tutorial] File I/O | ||
thank you for the tutorial, i know how to input but im having trouble with output... here is my game and i have saved the score to a text file, now i want it to show up on the screen
|
Author: | Chaoskiller [ Sun Oct 25, 2009 9:09 pm ] |
Post subject: | RE:[Tutorial] File I/O |
nice. I wish i could do that but right now im currently working on a pacman game.. It wasnt going well until i saw that you made the happy face a whole instead of little parts which makes it easier cause i can tell it to run that procedure whenever |
Author: | ScaryRat [ Sun Apr 18, 2010 9:05 pm ] |
Post subject: | RE:[Tutorial] File I/O |
You should add "assert" to check for validity. |
Author: | Velocity [ Mon Nov 28, 2011 8:43 am ] |
Post subject: | RE:[Tutorial] File I/O |
I used the example for a flexible array and i want to know how to output a random string from the text file to my turing screen, just to test it. Cause im making hangman, and i already have the word list created, i did the exact thing for the word list. My question is, how do i output a random word from the word list to the screen? |
Author: | Insectoid [ Mon Nov 28, 2011 9:42 am ] |
Post subject: | RE:[Tutorial] File I/O |
Pick a random number between one and the size of your array. Output the word at that spot. var foo : int foo = Rand.Int (1, 10) put array_of_words(foo) |
Author: | Velocity [ Mon Nov 28, 2011 10:16 am ] |
Post subject: | RE:[Tutorial] File I/O |
is array_of_words the array name? |
Author: | evildaddy911 [ Fri Dec 09, 2011 6:32 pm ] | ||
Post subject: | Re: [Tutorial] File I/O | ||
Quote: get : file , *
this looks like string manipulation... if I needed a specific spot in a file, could i use
to get a specific character and store it as an int/string? because Im making a game where you may possibly create your own levels stored as text files |
Author: | mirhagk [ Sat Dec 10, 2011 3:52 pm ] |
Post subject: | RE:[Tutorial] File I/O |
No I believe what that would do would be the exact same as when you use get with the screen, meaning that would be how many characters or whatever it would get, not what character it would start getting from. A better option would be to simply store it as numbers or something, with spaces in between. Then you can get it number by number. |