150 likes | 278 Views
Explore the fundamentals of C++ streams in this comprehensive guide. Learn how to effectively use input streams like `cin` for keyboard input and output streams like `cout` for displaying data on the screen. Understand buffering, manipulators for formatting output (like precision and alignment), and the differences between standard output and error streams. Additionally, discover file handling by utilizing `fstream`, `ifstream`, and `ofstream` for reading and writing to files. This resource is ideal for beginners looking to enhance their C++ programming skills.
E N D
IOStreams revisited Streams, strings, and files CS-1030 Dr. Mark L. Hornick
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-1030 Dr. Mark L. Hornick
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-1030 Dr. Mark L. Hornick
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-1030 Dr. Mark L. Hornick
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-1030 Dr. Mark L. Hornick
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-1030 Dr. Mark L. Hornick
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-1030 Dr. Mark L. Hornick
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-1030 Dr. Mark L. Hornick
Streams and Input/Output • <fstream> • <ios> • <iostream> // #includes istream • <iosfwd> • <iomanip> • <istream> // #includes ostream • <ostream> • <sstream> • <streambuf> CS-1030 Dr. Mark L. Hornick
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-1030 Dr. Mark L. Hornick
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-1030 Dr. Mark L. Hornick
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-1030 Dr. Mark L. Hornick
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 } CS-1030 Dr. Mark L. Hornick
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-1030 Dr. Mark L. Hornick
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 ”; CS-1030 Dr. Mark L. Hornick