0% found this document useful (0 votes)
69 views

C++ Input Output

The document provides a quick reference for C++ file input/output (I/O). It discusses including header files for I/O, opening and closing files using ifstream and ofstream, reading and writing to files using extraction and insertion operators, and formatting output using setprecision, setw, and other functions. File streams must be passed by reference to functions rather than by value.

Uploaded by

kdudek01
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
69 views

C++ Input Output

The document provides a quick reference for C++ file input/output (I/O). It discusses including header files for I/O, opening and closing files using ifstream and ofstream, reading and writing to files using extraction and insertion operators, and formatting output using setprecision, setw, and other functions. File streams must be passed by reference to functions rather than by value.

Uploaded by

kdudek01
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

C++ Input/Output

http://www.cs.hmc.edu/~geoff/classes/hmc.cs070.200109/notes/io.html

C++ File Input/Output


Details of le I/O seem to be buried at the back, missing, or overly complicated in most C++ manuals. This page provides a quick reference for the most frequently used methods. This page only discusses basic options that seem to be common to all my C++ references. Apparently there is a lot of variation from one manual to another, and from one implementation to another. I believe the methods below to be safe and portable, at least for ASCII (human-readable text) les.

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.

Passing streams to functions


File streams must be passed to functions by reference, not by value.
void myfunction(ifstream &fp, ...) void myfunction(ifstream fp, ...) // use this // not this

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.

Item by item input and output


If each input item is surrounded by whitespace (blanks, tabs, newlines), the items can be read easily using the extraction operator >>.
int myinteger; float myfloat; char mychar; char *mystring; char mystring[64]; fp_in >> myinteger; cin >> myfloat; fp_in >> mychar; cin >> mystring; // declarations // two ways to declare a string // input from file pointer or standard input

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;

Buffering and ush


When you send output to a stream, it does not necessarily get printed immediately. Rather, it may wait in a buffer until some unspecied event, e.g. buffer full enough, reading from input, or exit from program. The details may vary. Buffering makes it faster to print a large amount of output, particularly if you are producing the output bit-by-bit (e.g. one character at a time). However, it is a nuisance for output used in debugging (e.g. a statement that informs you that step 3 of your algorithm has been nished). Forcing all buffered output to actually be printed is known as "ushing" the stream. A ush can be forced by calling the ush function associated with each output stream, inserting the magic variable ush into the stream, or inserting endl.
cout << flush; cout.flush(); cout << endl;

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

Other input operations


All of the following are illustrated using the standard input, but they work just the same on le streams. cin.get(char &ch) Puts the next input character in the variable ch. Returns an integer value, which is zero if it encountered a problem (e.g. end of le). cin.getline(char *buffer, int length) Reads characters into the string buffer, stopping when (a) it has read length-1 characters or (b) when it nds an end-of-line character ('\n') or the end of the le. Stores a null character ('\0') after the last character read. cin.read(char *buffer, int n) Reads n bytes (or until the end of the le) from the stream into the buffer. cin.gcount() Returns the number of characters read by a preceding get, getline, or read command. cin.ignore(int n) Remove the next n characters (or until end of le) from the input stream, throwing them away into the Great Bit Bucket. cin.putback(char ch) Puts character ch back onto the stream. Bad things will happen if this character is not the one most recently extracted from the stream. These operations all return zero if something goes wrong, e.g. they hit the end of the le. Therefore, they can be used as the condition in an if statement or while loop. In addition, there are two more input operations, get and peek. These functions return EOF (which is secretly -1) if they encounter the end of the le. The output of these functions should be put into an integer (not a char) variable. cin.get() Returns the next character in the stream. cin.peek() Returns the next character in the stream but does not remove it from the stream. The following functions can be used to test the status of a stream. They return an integer, which is either zero or non-zero. cin.good() Returns 0 if the stream has encountered problems such as reading the end of le, non-existent le. cin.bad() Returns non-zero value if the stream is totally unusable, e.g. the le cannot be opened (but not if the stream has merely hit the end of the le). cin.eof() Returns a non-zero value if the stream has hit the end of the le. Notice that the stream's status will change to bad (not good, eof, etc) after the rst read request which encounters a problem. So, to use one of these functions, you attempt to do what you wanted to do (e.g. open the le, read the next number from the le, ...). If the action can't succeed, the program won't crash, though some of your variables might not contain the values you intended. Next, use the status function to check whether the action succeeded.

Other output operations


Other options for writing data to an output stream are: cout.put(char ch) Puts ch onto the stream. cout.write(char *str, int n) Puts n characters onto the stream, reading them from the string str. The function setf can be used to change formatting parameters for an output stream. For example, the following causes numbers to be left justied.
cout.setf(ios::left); cout.unsetf(ios::left);
3 of 4

\\ set option \\ unset option


7/13/2009 3:07 PM

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

\\ make item occupy 8 characters \\ 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.

Repositioning and error states


In general, it is possible to move to any position in a le stream. This is a capability most programmers use rarely, if at all. For fairly obvious reasons, don't try to use repositioning operations on the standard input, output, or error streams. The most likely reason for using a repositioning command is to rewind a le to the start, so that you can read its contents again. This is done as follows:
fp_in.clear(); fp_in.seekg(0, ios::beg); // forget we hit the end of file // move to the start of the file

This page is maintained by Geoff Kuenning.

4 of 4

7/13/2009 3:07 PM

You might also like