- C uses printf ( fprintf, etc. )
- Need to specify type of value being output
%d for integer, %c for character, etc.- Type mismatch will compile but cause garbage output
%f for integers can print as 0.00000, etc.- Printing only supported for "fundamental" types
no format specifiers for user-defined structs, etc.- C++ avoids all these problems
C++ Output with cout , <<
- C uses stdout to denote the standard output stream ( usually screen )
fprintf ( stdout, "whatever" ); is like printf ( "whatever" );- C++ uses cout to denote the standard output stream
must #include < iostream.h > ( like C's <stdio.h> )- We can send any type of value to cout using the << ( insertion ) operator
int a; float f; char c;
cout << "The value of a is " << a; // Note chaining of output
cout << "The value of f is " << f; // Output "flows" in direction of << arrows
cout << "The value of c is " << c << endl; // endl denotes a newline- C++'s cout output is buffered just like C's stdout output
endl flushes the buffer
can flush without endl by writing cout << flush;- Compiler automatically detects the types of values, uses appropriate behavior
- Fundamental types have correct behavior already defined
- Programmers can also define <<'s behavior for user-defined types
MyType mt;
cout << "Printing mt: " << mt << endl;- Can send error messages to cerr ( unbuffered ) or clog ( buffered )
- C uses scanf ( fscanf, etc. )
- Need to specify type of value being input
%d for integer, %c for character, etc.- Type mismatch will compile but cause garbage output
%f for integers can input as 0.00000, etc.- Need to specify an address to store input data
type mismatch can lead to overflow errors
must worry about pointer validity / memory allocation- "All-in-one" inputting only supported for fundamental types
no format specifiers for user-defined structs, etc.- C++ avoids all these problems also
C++ Input with cin , >>
- C uses stdin to denote the standard input stream ( usually keyboard )
fscanf ( stdin, %d, &i ); is like scanf ( %d, &i );- C++ uses cin to denote the standard input stream
must #include < iostream.h > ( like C's <stdio.h> )- We can receive any type of value from cin using the >> ( extraction ) operator
int a; float f1, f2, f3; char c;
cout << "Enter an integer:" ;
cin >> a; // Input "flows" in direction of >> arrows
cout << "Enter three floating-points:";
cin >> f1 >> f2 >> f3; // Note chaining of input
cout << "Enter a character:";
cin >> c; // Note that no pointers are necessary- Compiler automatically detects the types of values, uses appropriate behavior
- Fundamental types have correct behavior already defined
- Programmers can also define >>'s behavior for user-defined types
MyType mt;
cout << "Enter a MyType object:";
cin >> mt;Lower-level I / O facilities also exist ( like get, put, etc.) for "raw data" manipulation.
- cin and cout are examples of C++ streams
- streams are also used to handle file I / O
- streams are typed by operations to be performed on them
ifstream denotes a file used for input
ofstream denotes a file used for output
fstream denotes a file used for both- must #include < iostream.h > and #include < fstream > to use these
C++ Input / Output with File Streams
- Must declare a variable for each stream ( like C FILE*'s )
ifstream InputFile;
ofstream OutputFile;- Must associate each with a file ( like fopen( ) )
InputFile . open ( "filename" ); // By default, opened for reading, due to type
OutputFile . open ( "otherfilename" ); // By default, opened for writing, due to type- Can check to see whether opening was successful
if ( !InputFile )
// InputFile couldn't be opened for writing; handle error somehow- Note that opening a file for output destroys existing contents of file ( if any )
File Stream Manipulation
- Can specify other modes upon opening
InputFile . open ( "filename", ios :: app ); // Append instead of destroying
OutputFile . open ( "otherfilename", ios :: binary ); // For non-text I / O- Can also declare and open file streams in one step
ifstream InputFile ( "inputfilename" ); // Opened for reading
ofstream OutputFile ( "outputfilename" ); // Opened for writing
Example of constructors
Can specify other modes, as above- Can use insertion / extraction operators just like with cin / cout
for ( i = 0; i < NumValues; i++ )
InputFile >> Array [ i ];
Array [ i ] >> OutputFile;- Can check for end-of-file during other operations
while ( InputFile >> variable_name )
// loop ends when there is no more data to be read
// file stream actually converted to a void*, compared against null pointer
Could also use while ( InputFile )- File streams are closed automatically when they go out of scope
Example of destructors
Can also close manually: InputFile . close ( );File Stream Utilities
- Many formatting utilities exist for file streams
Can set floating-point precision
Can set justification
Can set field widths, etc.- Can also "reposition" streams ( like rewind, etc.)