ENGI 1020 Introduction to Computer Programming R E Z A S H A H I D I J U L Y 2 6, 2 0 1 0
Streams and files We have already talked about the standard input stream (cin), and the standard output stream (cout). A stream is a sequence of data, one piece of data after the other. cin is actually an object of type istream (input stream). cout is an object of type ostream (output stream).
Streams and files The input operator >> is smart! The standard input stream cin will input a variable of the given type, e.g. cin >> x, will read in a double precision number if x is declared as double x; If, for example, there are characters in the input stream that are input from the console, then these can t be part of x, since x is a number (except possible for the letter e, which stands for an exponent, e.g. 9.34e+2).
File streams File streams are streams that reside on disk rather than input from the keyboard or output to the screen. Files should be opened before they are used, and closed after they are used. Otherwise, file operations will not behave as expected.
File streams Use the same operators << for outputting to a file (as with outputting to standard output), and >> for inputting from a file (as with inputting from the keyboard). E.g. ifstream inputfile; char nextcharacter; inputfile >> nextcharacter; Always put the filename first, then the operator, and then the variable we are reading into. >> will exclude whitespace when it reads from a file.
Reading into input streams (either files or regular streams) Reading in a double precision x. The arrow is the current position in the input stream. This arrow moves along as we read in more info from the file. ifstream inputfile; double x; inputfile >> x;
File streams The input file stream class is ifstream (for reading from files on disk). The output file stream class is ofstream (for writing to files on disk). Can input from an ifstream using >>, get or getline. Can output to an ofstream with << or put.
Input and output redirection After we compile, a program we get an executable file, often ending in the suffix.exe, e.g. countwords.exe If we wish to run this program from the command line, but instead of accepting standard input from the keyboard, we want to get standard input from a file, we may use input redirection, and run the program as follows: countwords.exe < inputfile.txt
Input and output redirection If instead of writing the results to the screen, we wish to write the results to another file, e.g. if there are many lines of output and we do not wish to overwhelm the user, then we use output redirection with > Can use both < and > together. E.g. countwords.exe < inputfile.txt > output.txt Difference between this and ifstream/ofstream is that the input and output files are specified on the command line and not in the program itself.
File streams non member getline function getline is a non-member function of ifstream, which allows the programmer to input a whole line up to the next newline character to the second argument: string aline; getline(infile, aline); If instead you wish to input all characters up to another character (not the newline \n character), then this would be the third argument of the getline function: This reads all characters up to the next occurrence of * in the file, and so can read multiple lines. string aline; getline(infile, aline, '*');
File stream methods As with strings, there are six file stream methods that operate on file stream (ifstream and ofstream) objects. The first two are open() and close(). Suppose we have a file with the entire St. John s and area phone book, which is called phonebook.txt Before we can read from the file, we must open it. To do this, we have to create an object of type ifstream, and then use the open method on it: ifstream phonebookfile; phonebookfile.open( phonebook.txt );
File stream methods After performing some operations on the phone book, e.g. extracting all the phone numbers of a group of friends, we may want to write the phone numbers to another file, so that we can easily access them at any time. Then we can write our friends phone numbers to a file named friends.txt. We must create an object of type ofstream, and then open that file for writing: ofstream friendsfile; friendsfile.open( friends.txt );
File stream methods After we are done reading from a file or writing to a file, and have done whatever we want with the contents of the file, we must close the file. This is accomplished with the close method. Works for both ifstream and ofstream. So, e.g. phonebookfile.close(); friendsfile.close();
File stream methods For ifstreams, we need a method to input from the file that we have opened. This is the get() method, and returns the next character in the ifstream. Method prototype: char get() In a similar fashion, we need a way to output a character to a file we have opened for writing this writes the character stored in the variable c to the ofstream. Method prototype: void put(char c)
File stream methods To actually call these methods on certain objects is easy: char c; phonebookfile.get(c); string friend = Joe ; for (int i = 0; i < friend.length(); i++) { friendfile.put(friend.at(i)); } friendfile.put( \n );
File stream methods Two other methods are eof() and fail(). fail() is a boolean function (returns true or false), which only returns true if some error occurred when reading from or writing to an ifstream/ofstream. eof() is a boolean function which returns true only if the end of the input file stream (ifstream) that we are reading has been reached. E.g. could have: while (!inputfile.eof()) { inputfile.get(c); }