We need to understand how to open, read, write and close text files. The following File Input/Output terms are explained:
Text File - A file consisting of characters from the ASCII character code set. Text files (also know an ASCII text files) contain character data. When we create a text file we usually think of it consisting of a series of lines. On each line are several characters (including spaces, punctuation, etc.) and we generally end the line with a return (this a character within the ASCII character code set). The return is also known as the new line character. You are most likely already familiar with the escape code of \n which is used within C++ to indicate a return character when used with in a literal string with the cout.
A typical text file consisting of lines can be created by text editors (Notepad) or word processing programs (Microsoft Word). When using a word processor you must usually specify the output file as text (.txt) when saving it. Most source code files are ASCII text files with a unique file extension; such as C++ using .cpp, Pascal using .pas, Cobol using .cob, etc. Thus, most compiler/Integrated Development Environment software packages (such as the Bloodshed Dev-C++ 5 compiler/IDE) can be used to create ASCII text files.
Filename - The name and its extension. Most operating systems have restrictions on which characters can be used in filenames. Example for MS-DOS and Windows: Lab 05.txt
Because some operating systems do not allow spaces, we suggest that you use the underscore where needed for spacing in a filename.
Filespec - The location of a file along with its filename. It is short for file specification. Most operating systems have a set of rules on how to specify the drive and directory (or path through several directory levels) along with the filename. Example for MS-DOS and Windows: C:\myfles\cosc 1436\Lab 05.txt
Because some operating systems do not allow spaces, we suggest that you use the underscore where needed when creating folders or sub-directories.
Open - Your program requesting the operating system to let it have access to an existing file or to open a new file. Within C++ this is accomplished by including the header file: <fstream> File Input/Output is handled in C++ by using a pre-defined class of data objects, similar to the way string data type is handled. This class of objects has both data type names and functions built to specifcally accomplish opening and closing a file.
Within your program you create a local storage variable with the data type of fstream like this:
This variable will be used to store the device token that the operating system assigns to the file being opened. Thus, opening a file uses a class member function call like this:
inData.open("C:\\myfiles\\cosc l436\\Lab 05.txt", ios::in);
The two parameters passed to the function are the flespec and the method that you want to use the file (in this example as input). The function provides a returning value of a device token from the operating system and it is stored in the variable named inData.
It is considered good programming practice to determine if the file was opened properly. The device token should be a non zero value. It the operating system gives you the value of zero it was not able to open the file. The reason it usually can't open a file is because the flespec is wrong (misspelled or not typed case consistent in some operating systems) or the file is not stored in the location specifed. We often test the device token by using an if then control structure with the action consisting of stopping the program if it is true that you got the zero. The first line of the if then control structure looks like this:
Don't be misled by the not operator. This reads "if it is true that the token stored in inData is zero". If inData is zero, noting zero is 1 or true.
Read - Moving data from a device that has been opened into a memory location defined in your program. When reading text files that have integer or floating-point constants, the operating systems converts the text symbols to a binary number. The operator used is the extraction or read operator. An example of reading is:
inData » next number
This expression is similar to reading from the standard input device (aka the keyboard):
cin » next number
The "cin" is a predefined device token associated with the Standard Input and Output devices. For our file reading example you might say, "Go to the device identifed by the token stored in the inData variable and read in the next value storing it in the next number variable within my program".
Write - Moving data from a memory location defined in your program to a device that has been opened. When writing integer or floating-point data types, the operating system converts the binary number into the proper text symbols. The operator used is the insertion or write operator. An example of writing is:
outData « "Total is: " « total « endl;
This expression is similar to writing to the standard output device (aka the monitor):
cout « "Total is: " « total « endl;
The "cout" is a predefined device token associated with the Standard Input and Output devices. For our file writing example you might say, "Go to the device identifed by the token stored in the outData variable and write the items listed (the string constant then the value stored in my program variable named total then the endl or new line or the return character)".
Close - Your program requesting the operating system to release a file that was previously opened. There are two reasons to close a file. First, it releases the file and frees up the associated operation system resources. Second, if closing a file that was opened for output; it will clear the out the operating system's bufer and insure that all of the data is physically stored in the output file. Some examples of closing files:
You need to study this module in conjunction with the demo file provided.