Presentation is loading. Please wait.

Presentation is loading. Please wait.

CS-1030 Dr. Mark L. Hornick 1 IOStreams revisited Streams, strings, and files.

Similar presentations


Presentation on theme: "CS-1030 Dr. Mark L. Hornick 1 IOStreams revisited Streams, strings, and files."— Presentation transcript:

1 CS-1030 Dr. Mark L. Hornick 1 IOStreams revisited Streams, strings, and files

2 CS-1030 Dr. Mark L. Hornick 2 We’ve already seen these cin – input from keyboard First keypress is read in first cout – output to screen First data out is displayed first

3 CS-1030 Dr. Mark L. Hornick 3 Buffering of cout Data sent to cout is not always displayed right away It is buffered until flushed Three options cout << endl; // Send newline & flush cout << flush; // Flush only cout << ‘\n’; // Send newline only cout is automatically flushed when cin is accessed

4 CS-1030 Dr. Mark L. Hornick 4 output streams cout The “normal” output, sometimes redirected to a file You may wish to handle certain types of output differently… There are two extra ostreams for doing this cerr Sent to user, automatically flushed clog Sent to user, uses normal buffering

5 CS-1030 Dr. Mark L. Hornick 5 Manipulating an ostream The appearance of output can be controlled with manipulators Iostream manipulators don’t print Instead, they change the behavior of the output stream Found in iomanip library #include using namespace ios;

6 CS-1030 Dr. Mark L. Hornick 6 Manipulating an ostream cout << 20; Outputs “20” in decimal (default) cout << dec << 20 Also outputs “20” in decimal cout << oct << 20; outputs “24” (octal representation of 20) cout << hex << 20; outputs “14” (hexadecimal rep. of 20)

7 CS-1030 Dr. Mark L. Hornick 7 More Manipulators Output streams default to right-aligned text. To left-align the output: cout << setiosflags( left ) What about other defaults? setw(int w) Control width of display field Non-persistent (next item only) fixed, scientific Style of output

8 CS-1030 Dr. Mark L. Hornick 8 More Manipulators setprecision(int d) Number of digits of precision setfill(char c) Padding character (only with setw) showbase, noshowbase Displays/hides leading 0 or 0x for octal and hex boolalpha, noboolalpha Display true/false or 1/0

9 CS-1030 Dr. Mark L. Hornick 9 Streams and Input/Output // #includes istream // #includes ostream

10 CS-1030 Dr. Mark L. Hornick 10 Reading and Writing to and from strings Insertion (writing) to string: << Display string, subject to setw(int) cout << myString; Extraction (reading) from string: >> Reads only to first whitespace; not to end of line! cin >> myString; Extract entire line Reads to specified delimiter (‘\n’ is newline) getline(cin, myString, ‘\n');

11 CS-1030 Dr. Mark L. Hornick 11 Accessing Files Input can be read from files Output can be sent to files Use a special stream – fstream Or ifstream – for input only Or ofstream – for output only Behavior is similar to cin and cout Described by the fstream library #include

12 CS-1030 Dr. Mark L. Hornick 12 First associate a file with a file stream Done during fstream declaration Input file: ifstream Name( ); Output file: ofstream Name( ); Example: ifstream infile(“mydata.txt”); Use \\ or / for DOS/Windows paths

13 CS-1030 Dr. Mark L. Hornick 13 Reading from a File to a string Open a file and read some data: string fileName = “test.txt”; // filename string strText;// holds text from file ifstream infile(fileName.c_str()); if( infile ) { // write only if open // read from file to string getline( infile, strText ); infile.close(); // close the file }

14 CS-1030 Dr. Mark L. Hornick 14 Writing to a File Open a file and output some data: string fileName = “test.txt”; // a file ofstream outfile(fileName.c_str()); if( outfile ) { // write only if open outfile << “Outputting to file” << endl; outfile << fileName; // write filename outfile.close(); // close the file }

15 CS-1030 Dr. Mark L. Hornick 15 Handling File Errors What if there is an error with the file? (i.e. r eading a non-existent file) Simply ask the file stream: ifstream myfile(“baadname.txt”); if (!myfile) cout << “Error accessing file ”; Or use the is_open() method: if (!myfile.is_open()) cout << “Error accessing file ”;


Download ppt "CS-1030 Dr. Mark L. Hornick 1 IOStreams revisited Streams, strings, and files."

Similar presentations


Ads by Google