120 likes | 140 Views
Learn Input and Output operations, file handling, formatted outputs in C++ with Stream Objects. Examples and step-by-step guidance provided.
E N D
Agenda • Input and Output in C++ • Stream Objects • Formatted Output • Writing and Reading Files • Examples 2
General Remarks • I/O operations are essential for computer programs. • Input operations let us communicate with the program. • Output operations let the program communicate with us. • Typical input/output devices: • Keyboard, Mouse, Screen and Files. • C++ provides set of streams: • Classes that make I/O communication easy and OS independent. 3
Stream Objects and Classes • A stream is a transfer of data elements between a program and I/O devices. • Every stream has a direction (I/O) and a device connected to it: • E.g. - Input (direction) stream from file (device) • E.g. - Output (direction) stream to console (device) • Previously seen C++ classes and stream objects are: • istreamcin: input variable hooked up to the keyboard. • ostreamcout: output variable hooked up to the screen. • Stream objects operating on files are instances of ifstream and ofstream classes.
C++ Standard I/O #include <iostream> #include <string> int main() { std::cout << "What’s your name:" << std::endl; std::string fname; std::cin >> fname; std::cout << "Hello World" << fname << "!" << std::endl; return 0; } • C++ library includes the header file <iostream> defininingstdin and stdout stream objects. • stdin of a program is the keyboard, accessed through cin. • stdoutof a program is the screen, accessed through cout.
Formatted Output • Manipulate output data most of the time to make it easier to read or post-process. (e.g. 0.013 more readable than 1.299e-3) • C++ header <iomanip> provides a set of parametric manipulators to format output: • Fill character • cout.fill(´*´); • ... << setfill(´*´) << ... • …**************… • Decimal precision • cout.precision(3); • ... << setprecision(3) << ... • 3.14159 3.14 • set*** requires • #include <iomanip> Additional Resources: (1)http://www.cplusplus.com/reference/iomanip (2) C++ Primer, Ch. 17 6
C++ <iostream> != C <cstdio> • C++-style Output • cout << "C++ printing of: " << setprecision(3) << number << endl; • C-style Output • printf("%s: %.3f\n","C printing of",number) • C++ Pros1 : • Extensible to user defined objects. • Type safe, i.e. the compiler knows the type of the object being I/O'd and detect errors at compile time. • Less error prone, e.g. no redundant “%”. • C++ Cons: • Requires more typing, in particular for complex output. 1see http://www.parashift.com/c++-faq/iostream-vs-stdio.html
Streams and Files <fstream> • Streaming data from/to files (i.e. read/write) is almost identical to console I/O. • Except file streams have to be instantiated and opened by the user-program. // Instantiate ofstream object "fout" ofstream fout; fout.open("myfile.dat"); fout << var; // writing to file cout << var; // writing to screen
fstream • Important I/O functions • Open/Close Files: voidopen(constchar* filename); voidclose(); • End offile booleof() const; • Extractformatteddata istream& operator>>(...) or >> • Insert datawithformat ostream& operator<<(...) or << • Fast extractionofunformatted, binarydata istream& read(char* ptr, size_t size) • Fast writeoperationofunformattedbinarydata ostream& write(char * ptr, size_tsize) 9
Read/write character by character • Useful functions for scanning files char-by-char: • ostream& put(charc); • intget(); <fstream> offers many other functions http://www.cplusplus.com/reference/fstream 10
Step-by-Step Streaming to Files • Include header: #include <fstream> • Create instance of fstream object: ofstream fout; • Open file: fout.open("myDat"); • Transfer data from/to file:fout << "Hello World!\n"; • Close file: fout.close(); 11
Example 1: Simple writeandread #include <iostream> #include <fstream> usingnamespace std; int main() { ofstream fout; fout.open("mydat"); if(!fout.is_open()) { cerr << "IO error\n"; return 1; } int a = 12345; fout << a; //write to file fout.close(); return 0; } #include <iostream> #include <fstream> using namespace std; int main() { ifstream fin; fin.open("mydat"); if(!fin.is_open()) { cerr << "IO error\n"; return 1; } int a; fin >> a; // read from file fin.close(); return 0; } 12