1 / 12

Exercise 9 – File I/O

Exercise 9 – File I/O. 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.

Download Presentation

Exercise 9 – File I/O

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Exercise 9 –File I/O

  2. Agenda • Input and Output in C++ • Stream Objects • Formatted Output • Writing and Reading Files • Examples 2

  3. 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

  4. 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.

  5. 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.

  6. 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

  7. 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

  8. 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

  9. 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

  10. 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

  11. 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

  12. 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

More Related