+ All Categories
Home > Documents > IOStreams revisited

IOStreams revisited

Date post: 19-Jan-2016
Category:
Upload: ciqala
View: 31 times
Download: 0 times
Share this document with a friend
Description:
IOStreams revisited. Streams, strings, and files. 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. Buffering of cout. Data sent to cout is not always displayed right away - PowerPoint PPT Presentation
15
CS-1030 Dr. Mark L. Hornick 1 IOStreams revisited Streams, strings, and files
Transcript
Page 1: IOStreams revisited

CS-1030Dr. Mark L. Hornick

1

IOStreams revisited

Streams, strings, and files

Page 2: IOStreams revisited

CS-1030Dr. 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

Page 3: IOStreams revisited

CS-1030Dr. 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

Page 4: IOStreams revisited

CS-1030Dr. 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

Page 5: IOStreams revisited

CS-1030Dr. 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 <iomanip>using namespace ios;

Page 6: IOStreams revisited

CS-1030Dr. 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)

Page 7: IOStreams revisited

CS-1030Dr. 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

Page 8: IOStreams revisited

CS-1030Dr. 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

Page 9: IOStreams revisited

CS-1030Dr. Mark L. Hornick

9

Streams and Input/Output

<fstream> <ios> <iostream> // #includes istream <iosfwd> <iomanip> <istream> // #includes ostream <ostream> <sstream> <streambuf>

Page 10: IOStreams revisited

CS-1030Dr. 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');

Page 11: IOStreams revisited

CS-1030Dr. 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 <fstream>

Page 12: IOStreams revisited

CS-1030Dr. Mark L. Hornick

12

First associate a file with a file stream

Done during fstream declaration Input file:

ifstream Name(<filepath>); Output file:

ofstream Name(<filepath>); Example:

ifstream infile(“mydata.txt”); Use \\ or / for DOS/Windows paths

Page 13: IOStreams revisited

CS-1030Dr. Mark L. Hornick

13

Reading from a File to a string

Open a file and read some data:

string fileName = “test.txt”; // filenamestring 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}

Page 14: IOStreams revisited

CS-1030Dr. 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

}

Page 15: IOStreams revisited

CS-1030Dr. Mark L. Hornick

15

Handling File Errors

What if there is an error with the file? (i.e. reading 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 ”;


Recommended