Download presentation
Presentation is loading. Please wait.
1
Standard Input/Output Streams
A stream is a sequence of characters Streams convert internal representations to character streams or vice versa >> input (extraction) operator << output (insertion) operator Any character (printable or not) A stream has no fixed size
2
cin and cout Defined in the iostream library cin cout
#include <iostream> cin Standard input stream Accepts typed input from the keyboard Uses the extraction operator >> cout Standard output stream Outputs to the terminal window Uses the insertion operator << Extraction operator Load input into a variable cin >> x; Insertion operator Output to a device (or file) cout << x; Both operators “point” in the direction of the data flow!
3
Input Stream, >> Operator
Last character read is tracked with an input stream buffer pointer Each new input attempt begins at current pointer position Leading white space (blanks, tab, nwln) skipped until first non-white- space character is located, then… For strings, characters are read into string variable until white space is encountered For numeric value, all characters that are part of the numeric value are processed until a character that’s not legally part of a C++ number is read.
4
Output Stream - Manipulators
setw Set the width of the next output value. After output the value resets to zero. Zero width defaults to the object width (i.e. no padding) boolalpha noboolalpha Switches between textual and numeric representation of booleans showpoint noshowpoint Controls whether decimal point is always included in floating-point representation skipws noskipws Controls whether leading whitespace is skipped on input left right Sets the placement of fill characters (i.e. right or left justification) fixed scientific Changes formatting used for floating-point I/O endl Outputs '\n' and flushes the output stream setprecision Changes floating-point precision Manipulators require the inclusion of the iomanip library - (except for setw) #include <iomanip>
5
Attaching to an External file
Read or write data from/to a file rather than standard in or out Must include fstream library #include <fstream> Declare an input or output stream variable ifstream ins ofstream outs Open the file, using the file name (and path, if needed) ins.open(“input.txt”) outs.open(“output.txt”) Or: String inFile= “file.txt”; ins.open(inFile.c_str()); // Note: must convert to a “c-style” string cin and cout are stream variables that attach to the terminal instead of a file! Otherwise, they behave the same way
6
Example: Reading a File Name
string fileName; cout << “Enter the input file name: “; cin >> fileName; ins.open(fileName.c_str( )); Note: Need to use “C-style” string in the open() method c_str() method does the conversion from C++ string
7
Some stream methods fs.open(fname) fs.close() fs.eof() fs.fail()
Open a file for input or output (attach to a file) fs.close() Disconnect a stream from an open file fs.eof() Tests for end-of-file condition. Returns true when the end of a file is reached (when the program attempts to read the <eof> character that marks the end of a file). fs.fail() Returns true is an operation fails to execute properly (ex: when a file open or an attempt to read input fails) fs.clear() Clears the error flag set when a failure occurs Note: fs is a stream – cin/cout or a user specified file
8
More stream methods fs.get(ch) fs.put(ch)
Extract the next character from the (input) stream and put it in the char variable ch fs.put(ch) Inserts (writes) the character ch into the (output) stream. getline(stream, string_variable, delimiter) Read up to a delimiter (see next slide) fs.ignore(num, delimiter) Ignore input up to delimiter (or num, whichever comes first) Note: fs is a stream - cin or a user specified file
9
getline() method Usage: getline(stream, string_variable)
Ex: getline(ins, name) // ins is an input stream, name is a string getline() reads up to the delimiter character Default delimiter is newline (c/r) Can specify delimiter as a third argument Ex: getline(ins, name, ‘#’) // ‘#’ is delimiter instead of newline getline() does not skip over white space (blanks, tab, nwln) If next character to read is delimiter, nothing gets read into the variable! swallow delimiter with stream.ignore()…
10
Using ignore getline does not skip leading white space, so if a newline character is encountered at the beginning of the characters to be extracted, getline will stop immediately and won’t perform the expected task Use stream.ignore(num, delimiter) to swallow delimiter Ex: ins.ignore(100,’\n’) or cin.ignore(1000,’\n’); See example fileGetline.cpp
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.