cs-1030 dr. mark l. hornick 1 iostreams revisited streams, strings, and files
TRANSCRIPT
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
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
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
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;
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)
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
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
CS-1030Dr. Mark L. Hornick
9
Streams and Input/Output
<fstream> <ios> <iostream> // #includes istream <iosfwd> <iomanip> <istream> // #includes ostream <ostream> <sstream> <streambuf>
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');
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>
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
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}
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
}
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 ”;