C++ Input Output
C++ Input Output
http://www.cs.hmc.edu/~geoff/classes/hmc.cs070.200109/notes/io.html
Header les
To do input and output, you will need to load the iostream header le. You may also need to load the fstream (le I/O) and/or iomanip (format manipulation) header les. Put some/all of the following lines at the top of your code le (or in the header le for your program, if you are using one).
#include <iostream.h> #include <fstream.h> #include <iomanip.h> // I/O // file I/O // format manipulation
Getting a stream
Three streams just exist: cout (terminal output), cin (terminal input), and cerr (error output, which also goes to the terminal). When writing error messages, use cerr rather than cout. In simple examples, the two appear to be the same. However, they behave differently when you use Unix redirection operators when you run your program. In particular, if you redirect output to a le, error messages printed to cerr will still appear on the user's terminal whereas error messages printed to cout will be mixed into the output le. File streams are of type ifstream (input) or ofstream (output).
ifstream fp_in; // declarations of streams fp_in and fp_out ofstream fp_out; fp_in.open("myfile.txt", ios::in); // open the streams fp_out.open("myfile.txt", ios::out); fp_in.close(); // close the streams fp_out.close();
A le should be closed if you are done with it, but the program will continue running for a while longer. This is particularly important when you intend to open a lot of les, as there may be a limit on how many you can have open at once. It is also a good idea if you intend to open a le for input, and then re-open the le for output. Declaring the pointer and opening the le can be combined:
ifstream fp_in("myfile.txt", ios::in); // declare and open
The parameters ios::in and ios::out specify the mode in which the le is to be opened. Depending on the implementation of C++, it may be possible to specify a variety of other mode options, such as appending to the end of an existing le, triggering an error rather than overwriting an existing le, or specifying that a le is binary for operating systems (e.g. MS-DOS) which distinguish binary and ASCII les.
1 of 4
7/13/2009 3:07 PM
C++ Input/Output
http://www.cs.hmc.edu/~geoff/classes/hmc.cs070.200109/notes/io.html
If you pass streams by value, the C++ compiler will not complain. However, mysterious bad things will start happening, often in parts of the code which don't appear to be related to the offending function.
The extraction operator works for numbers (ints, oats), characters (char), and strings (declared as arrays of type char or pointers to type char). The extraction operator returns a zero value if it encounters a problem (typically, the end of the le). Therefore, it can be used as the test in an if statement or a while loop. WARNING: when reading data into a character string, bad things will happen if the input word is longer than your string. To avoid problems, use the operator setw to force excessively long input to be broken up. (You must include the iomanip header le.) The input to setw should be the length of your string (including the null character '\0' at the end of the string).
cin >> setw(length) >> mystring;
Numbers, characters, and strings can be written to a le, standard output, or the standard error using the insertion operator <<.
cout << "Value of myinteger " << myinteger << endl; cout << "My string is " << mystring << " plus a null character\n" << flush;
To insert a line break, either insert the magic variable endl or write the end-of-line character ('\n') to the output. To make a pointer print out as a pointer, not as whatever type of data it points to, cast it to the type (void *). To make a character print as a number, cast it to type int. Similarly, you can use a cast to convince C++ to print an integer as the corresponding character.
cout << (void *)ptr; cout << (int)ch; cout << (char)ival;
2 of 4
7/13/2009 3:07 PM
C++ Input/Output
http://www.cs.hmc.edu/~geoff/classes/hmc.cs070.200109/notes/io.html
C++ Input/Output
http://www.cs.hmc.edu/~geoff/classes/hmc.cs070.200109/notes/io.html
The most obviously useful parameters are: ios::left Left justify output. ios::right Right justify output. ios::scientic Print oating point numbers using scientic notation. ios::xed Print oating point numbers using xed point notation. ios::showpoint Print a decimal point for all oating point numbers, even when it's not needed (e.g. the number is exactly an integer). The precision of numbers can be changed as follows. You can also set the width, i.e. the minimum number of spaces used to print the next. These featuers are used, for example, to make output items line up in columns when printed. Both of these features require that you include the iomanip header le.
cout << setprecision(2); cout.precision(2); cout << setw(8); cout.width(8); \\ print two digits after decimal point \\ an alternative syntax
Setting the width to k forces the item to occupy at least k characters. If its printed representation is shorter, blanks will be added so that it occupies k characters. If its printed representation is longer, it will occupy more than k characters. When you reset parameters such as the precision, it is not clear whether the new value lasts only for the next item printed or whether it persists for subsequent values. Apparently this varies with the parameter and the C++ compiler. Therefore, if you care about the value of some formatting parameter, explicitly set it to the right thing before you output each item. For examples of usage, and how the formatting options affect the printing of numbers, look at Bob Keller's format sampler.
4 of 4
7/13/2009 3:07 PM