Professional Documents
Culture Documents
COMP345: Advanced Program Design With C++ Input/Output
COMP345: Advanced Program Design With C++ Input/Output
COMP345: Advanced Program Design With C++ Input/Output
Lecture 3
Input/Output
Fall 2020
Department of Computer Science and Software Engineering
Concordia University
Contents
❑ streams and stream operators
▪ keyboard input – console output
▪ output formatting stream directives
❑ file input/output
❑ MFC serialization
Advanced Program Design with C++ 3
▪ Streams are objects that are defined to handle a device on which input or output operations are
performed.
▪ Streams allow C++ programs to interact with external entities controlled by the operating system,
such as:
▪ program execution console
▪ keyboard
▪ files
▪ Other external entities such as network sockets are not provided by basic C++ stream classes, but
are available in various libraries (e.g. Boost).
#include <iostream>
using namespace std;
▪ Tells C++ to use appropriate library so we can use the I/O objects cin, cout, cerr or
#include <iostream>
using std::cout;
▪ cout is an object that is an instance of the ostream class, defined in the iostream
library.
▪ cout is a stream, << is the stream insertion operator that is used to output to the
stream.
▪ endl is a stream manipulator, which is a directive sent to a stream that instructs the
stream’s behavior.
▪ There are many stream manipulators, which we shall not cover here in detail.
Stream manipulators
▪ Formatting numeric values for output
Stream manipulators
▪ Stream manipulators to force decimal sizes:
▪ These directives force all future cout’ed floating point values to have exactly two digits
after the decimal point:
▪ fixed: set the stream to manipulate floating point numbers to be represented
as fixed-point representation (as opposed to scientific notation, or integer
notation)
▪ showpoint: show trailing zeroes in after the point, even if there are none.
▪ setprecision(n): set the number of digits of precision used for floating point
numbers.
File input/output
▪ Similar to cin/cout streams, there are streams for input/output from/to files
▪ File input : ifstream
▪ File output : ofstream
▪ Part of library <fstream>
▪ May use the same stream operators and formatting directives
▪ They are more complex to use compared to cin/cout:
▪ Choice between different modes e.g text, binary, random-access
▪ Different operators to use for different modes
▪ May have to check for various stream states, which are set for example
when operating on a non-existing file, or reaching the end of a file.
2. ofstream outputfilestream("scores.txt");
…
outputfilestream.close();
▪ Input
1. ifstream inputfilestream;
infilestream.open("scores.txt");
2. ifstream inputfilestream("scores.txt");
…
inputfilestream.close();
fstream filestream;
filestream.open("scores.txt“, ios::out);
…//output operations
filestream.close();
…//do other things
filestream.open("scores.txt“, ios::in);
…//input operations
filestream.close();
…OR…
inout.open("temp.txt", ios::in);
string city;
inout >> city;
cout << "End of file (no errors)" << endl;
showState(inout);
inout.close();
return 0;
}
#ifndef JRectangle_H
#define JRectangle_H
#include "AbstractGeometricObject.h"
private:
double width;
double height;
protected:
DECLARE_SERIAL(JRectangle);
};
#endif
CFile theOtherFile;
//open a file in input mode
theOtherFile.Open(_T("CArchiveTest.txt"), CFile::modeRead);
//Create a CArchive and connect it to the file
CArchive otherArchive(&theOtherFile, CArchive::load);
return 0;
Joey Paquet, 2007-2020
References
▪ Y. Daniel Liang, Introduction to Programming with C++ (Chapter 1, 13), Pearson, 2014.
▪ Bjarne Stroustrup, The C++ Programming Language (Chapter 30, 38), Addison-Wesley,
2013.
▪ Microsoft Developer Network. Serialization: Making a Serializable Class.
http://msdn.microsoft.com/en-us/library/00hh13h0.aspx
▪ Microsoft Developer Network. Serialization in MFC. http://msdn.microsoft.com/en-
us/library/6bz744w8.aspx
▪ Microsoft Developer Network. Storing and Loading CObjects via an Archive.
http://msdn.microsoft.com/en-us/library/3bfsbt0t.aspx
▪ Cplusplus.com. Input/output.
▪ Barnette, McQuain. C++ Input/Output: Streams. Virginia Tech.
▪ Kurt McMahon. Output Formatting. Northern Illinois University.
▪ Joey Paquet COMP345 course Notes Concordia university.