Turing File IO
From Compsci.ca Wiki
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:
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.
var stream : int open : stream, "filename.txt", put
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
open : fileNumber,fileName, I/O operation
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:
close : fileNumber
So in our example above:
close : stream
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:
var stream : int open : stream, "myText.txt", put put : stream, "This is how you put something to file"
Also note that you can put integers, real numbers, and booleans to file as well, and the process is exactly the same:
var stream : int open : stream, "myText.txt", put put : stream, 42 put : stream, 2.112 put : stream, false
You can also put variables to file:
var stream : int var myPhrase : string := "This is how you put a variable to file" open : stream, "myText.txt", put put : stream, myPhrase
Simple as that and it works with any variable type Smile.
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:
get : fileNumber, information
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:
var stream : int var myText : string := "" open : stream, "myText.txt", get %notice the file has to be in the "get" mode get : stream, myText
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)
var stream : int var myText : string open : stream, "myText.txt", read read : stream, myText
And for write (similiar to put)
var stream : int var myText : string := "This is how to write to a file" open : stream, "myText.txt", write write : stream, myText
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?
Tell and Seek
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 : fileNumber, filePosition
seek : fileNumber, filePosition
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)
var stream : int var filePosition : int var myText : string := "An example of seek and tell" open : stream, "myText.txt", put, seek tell : stream, filePosition put : stream, myText seek : stream, filePosition
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! Very Happy 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.
var stream : int var myText : flexible array 1..1 of string open : stream, "myText.txt", get loop exit when eof(stream) get : stream, myText(upper(myText)) new myText, upper(myText) + 1 end loop
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.
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.
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. Very Happy
Added to Wiki by: TheFerret