Skip to main content
Contents
Dark Mode Prev Up Next Scratch ActiveCode Profile
\(
\newcommand{\lt}{<}
\newcommand{\gt}{>}
\newcommand{\amp}{&}
\definecolor{fillinmathshade}{gray}{0.9}
\newcommand{\fillinmath}[1]{\mathchoice{\colorbox{fillinmathshade}{$\displaystyle \phantom{\,#1\,}$}}{\colorbox{fillinmathshade}{$\textstyle \phantom{\,#1\,}$}}{\colorbox{fillinmathshade}{$\scriptstyle \phantom{\,#1\,}$}}{\colorbox{fillinmathshade}{$\scriptscriptstyle\phantom{\,#1\,}$}}}
\)
Section 6.10 Summary & Reading Questions
File handling in C++ uses
stream
similar to cout and cin in
<iosteam>
library but is
<fstream>
for file stream.
ifstream in_stream
creates an input stream object, in_stream, that can be used to input text from a file to C++.
ofstream out_stream
creates an output stream object,out_steam, that can be used to write text from C++ to a file.
End-of-File or
.eof()
is a method for the instance variables of fstream, input and output stream objects, and can be used to carry out a task until a file has ended or do some task after a file has ended.
Reading Questions Reading Questions
1.
Which of the following are libraries for C++ input and output? (Choose all that are true.)
Yes, fstream is library for handling file input and output.
No, ifstream is an object type for handling input.
No, ofstream is an object type for handling output.
Yes, iostream is a library for handling console input and output.
2.
3.
#include <fstream>
#include <cstdlib>
#include <iostream>
using namespace std;
main(){
ifstream in_stream;
ofstream out_stream;
int inputn;
out_stream.open("anotherFile.txt");
out_stream << 25;
out_stream << 15 << endl;
out_stream << 101 << endl;
in_stream.open("anotherFile.txt");
in_stream >> inputn;
cout << inputn;
in_stream >> inputn;
}
4.
You have attempted
of
activities on this page.