Presentation is loading. Please wait.

Presentation is loading. Please wait.

Standard Input/Output Streams

Similar presentations


Presentation on theme: "Standard Input/Output Streams"— Presentation transcript:

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 << Note insertion and extraction operators “point” in the direction of 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 External files Streams can be used to read and write from files
Instead of cin and cout, file streams are defined cin/cout are automatically “attached” to the keyboard/terminal Using file streams First declare the stream variable Two types ifstream to read from a file ofstream to write to a file Use the stream variable as you would cin and cout i.e. with the insertion and extraction operators

6 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

7 Example: Reading a File Name
string fileName; ifstream ins; 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

8 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 (like ins/outs from the previous slides) – or cin/cout

9 More stream methods (for input)
fs.get(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 an input stream (like ins from the previous slides) – or cin

10 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 Note: delimiter is extracted (removed) from the stream, but not stored getline() does not skip over leading white space (blanks, tab, nwln) If next character to read is delimiter, nothing gets read into the variable! But, can swallow delimiter with stream.ignore()…

11 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


Download ppt "Standard Input/Output Streams"

Similar presentations


Ads by Google