0% found this document useful (0 votes)
15 views

Standard Input/Output Streams

The document discusses input and output in C++, including standard input/output streams, writing to files using ofstream, reading from files using ifstream, and member functions like getline, get, and put. It also covers the fstream class, which can be used for both input and output, and file open modes like ios::in and ios::out that specify how a file will be used.

Uploaded by

Germán Méndez
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views

Standard Input/Output Streams

The document discusses input and output in C++, including standard input/output streams, writing to files using ofstream, reading from files using ifstream, and member functions like getline, get, and put. It also covers the fstream class, which can be used for both input and output, and file open modes like ios::in and ios::out that specify how a file will be used.

Uploaded by

Germán Méndez
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

Input and Output

Standard Input/Output Streams Writing Data to a File Reading Data from a File Member Functions: getline, get and put fstream and File Open Modes

Standard Input/Output Streams


A stream is a sequence of characters Standard input stream (cin) and standard output stream (cout) of the iostream library >> input (extraction) operator << output (insertion) operator Any character (printable or not) A stream has no fixed size
C++ for Java Programmers Lecture 8
2

C++ for Java Programmers Lecture 8

Writing Data to a File


ofstream class
used to write primitive data type values, arrays, string, and objects to a text file need to include fstream header file open, close
if a file already exists, the contents of the file will be destroyed without warning

Reading Data from a File


ifstream class
used to read data from a text file fail: test whether the file exists or not eof: test the end of file

C++ for Java Programmers Lecture 8

C++ for Java Programmers Lecture 8

Member Functions: getline, get and put


the stream extraction operator >> works for data delimited by whitespace use getline to read strings with whitespace
getline(char array[], int size, char delimitChar)

fstream and File Open Modes


we could use fstream class to create an input or output stream convenient if the program needs to use the same stream object for both input and output specify a file mode to tell C++ how the file will be used

5

use get on an input object to read a character


char get() // Returns a char istream * get(char &ch) // Read a character to ch char get(char array[], int size, char delimitChar) // Read into array

use put on an output object to write a character


void put(char ch)
C++ for Java Programmers Lecture 8

ios::in // Opens a file for input ios::out // Opens a file for output ios::app // Appends all output to the end of the file ios::ate // Opens a file for output. If the file already exists, move to the end of the file.
C++ for Java Programmers Lecture 8
6

You might also like