Download presentation
Presentation is loading. Please wait.
Published byJunior Townsend Modified over 9 years ago
1
1 I/O C++ has no built-in support for input/output input/output is a library (iostream) C++ program views input and output as a stream of bytes Input: program extracts bytes from an "input" stream Output: program inserts bytes into an "output" stream Error: program outputs bytes into an "error" stream
2
2 Approximate I/O Inheritance Hierarchy Will learn what inheritance is later Streambuf (base class, actual op on devices) ios (formatting I/O operations, stream state, err) istream (input stream) ifstream (input file) ostream ofstream
3
3 How It Works Standard objects cin, cout, cerr Bytes in either stream can go to standard device OR file E.g. output to cout (monitor), OR a file C++ program does not care where bytes come/go It just works on bytes of data Automatically converts data type
4
4 Extractor Operator (>>) istream & operator>>(istream & is, T &t) { is >>... return is; } reason for returning “is” : to concatenate input (cin >> x) >> y cin >> x >> y
5
5 Inserter Operator (<<) ostream & operator<<(ostream & os, T t) { os <<... return os; } reason for returning “os” : to concatenate input (cout << x) << y cout << x << y
6
6 Friend operators for classes class Coord { private: int x, y; public: Coord() {x = 0; y = 0;} friend ostream &operator << (ostream &s, Coord point); friend istream &operator >> (istream &s, Coord &point); }; Cannot be part of class methods (cin >> obj, cout << obj)
7
7 Parsing When input data is read using cin, it is broken into tokens each token is separated from the next by whitespace. "cin >>..." reads one token at a time What if we want to read spaces too ? int get() - returns a char or EOF (-1) istream& getline(char *buffer, int num, char end = ‘\n’)
8
8 Status int x; cin >> x; Q: What happens if you type in &%* when int expected ? A: x is not changed, can check failure of operation, using “!” if (! (cin >> x)) cerr << “Error in cin stream” << endl;
9
9 How to test for errors: Member functions good() : ios::goodbit - everything is fine eof() : ios::eofbit - end-of-file (also sets failbit) fail() : ios::failbit - failure: error, but stream is usable bad() : ios::badbit - fatal err: stream is no longer usable clear() : clears all or some flags rdstate() : read the combination of all
10
10 How to test for errors while (cin >> x) { // cin stream is good cout << x << endl; } Another way: cin >> x; if (!cin.fail()) { cout << x << endl; }
11
11 Operations on Files ofstream out; out.open (file_name, mode, access); e.g. r/o access Mode - ios::app ios::trunc, etc. Access - r/o access E.g. out.open (“test”, ios::out, 0); or out.open(“test”) or ofstream out(“test”); //constructor autom open Use through the inserter operator: out << 5; out.close()
12
12 Stream Manipulators (e.g. std::endl) #include std::setw() int x; cout << std::setw(9) << x; outputs x, right adjusted, with spaces on the left side
13
13 Alignment std::left, std::right cout << std::left; cout << std::setw(9) << x; outputs x, left adjusted, with spaces on the right side left and right remain valid until another command
14
14 Fill manipulator std::setfill cout << std::right; cout << std::setfill('0'); cout << std::setw(9) << x << endl; outputs x, right adjusted, with zeros on the left side
15
15 Numeric base cout << std::hex << x << endl; cout << std::oct << x << endl; cout << std::dec << x << endl; remains valid until changed
16
16 Booleans bool a; cout << std::boolalpha; cout << a << endl; remains valid until changed
17
17 Floating Point std::fixed, std::scientific and std::setprecision manipulators cout << std::fixed << std::setprecision(2) << 432.343; will print 432.34 cout << std::scientific << std::setprecision(2) << 432.3434; will print 4.32e+02
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.