Lecture 25

File

Reading

p500-506, 506-516

Appending to a file

File: lecture.txt
Lecture 1: Introduction to C++
Lecture 2: Flow of Control
Lecture 3: Functions

Program: write to the file.
#include<iostream>
#include<fstream>
using namespace std;

int main() {
ofstream fout;
fout.open("lecture.txt");
fout << "Lecture 4: Pointer" << endl
<< "Lecture 5: File I/O" << endl;
fout.close(); return 0; }

File lecture.txt after you execute the program. The original data are gone.
Lecture 4: Pointer
Lecture 5: File I/O

Here is the new program. The new data are appended to the original file.
#include<iostream>
#include<fstream>
using namespace std;

int main() {
ofstream fout;
fout.open("lecture.txt", ios::app); fout << "Lecture 4: Pointer" << endl << "Lecture 5: File I/O" << endl; fout.close(); return 0; }

File lecture.txt after you execute the program. The new data are appended to the original file.
Lecture 1: Introduction to C++
Lecture 2: Flow of Control
Lecture 3: Functions
Lecture 4: Pointer
Lecture 5: File I/O

Check that a file was open successfully

#include<iostream>
#include<fstream>
#include<cstdlib> // for exit using namespace std; int main() { ifstream fin; ofstream fout; fin.open("input.txt"); if(fin.fail()) { // fail to open the file
cout << "Input file opening failed.\n";
exit(1); // exit the program
}

fout.open("output.txt");
if(fout.fail()) { // fail to open the file
cout << "Output file opening failed.\n";
exit(1); // exit program
}

int first, second, third;
fin >> first >> second >> third;
fout << "The sum of the first 3 numbers are "
<< (first + second + third) << endl;

fin.close();
fout.close();

return 0;
}

Suppose there is no input.txt. The program can't open the file. Here is the output
Input file opening failed.

After you create the file input.txt
25 35 45 55 65

After you run the program, output.txt will be created
The sum of the first 3 numbers are 105

Checking for the end of file

#include<iostream>
#include<fstream>
#include<cstdlib> // for exit
using namespace std;


int main() {
ifstream fin;

fin.open("score.txt");
if(fin.fail()) { // fail to open the file
cout << "Input file opening failed.\n";
exit(1); // exit the program
}

int num=0; // number of scores
double sum=0, score=0;

while(!fin.eof()) { // if it is not the end of the file
fin >> score;
sum+=score;
num++;
}

cout.setf(ios::fixed);
cout.setf(ios::showpoint);
cout.precision(2);

cout << "There are " << num << " scores.\n";
cout << "The average is " << sum/num << ".\n";

fin.close();

return 0;
}


File : score.txt
11.5 33.6 99 93.2 42.5 64.1 88.35 99.25 33.54 
52 53.3 99 88 88.8 74.25

Here is the output on the screen
There are 15 scores.
The average is 68.03.

Formatting output

You can rewrite the above program:
#include<iostream>
#include<fstream>
#include<cstdlib> // for exit
using namespace std;

int main() {
ifstream fin;
ofstream fout;
fout.open("output.txt");

fin.open("score.txt");
if(fin.fail()) { // fail to open the file
cout << "Input file opening failed.\n";
exit(1); // exit the program
}

int num=0; // number of scores
double sum=0, score=0;

while(!fin.eof()) { // if it is not the end of the file
fin >> score;
sum+=score;
num++;
}

fout.setf(ios::fixed);
fout.setf(ios::showpoint);
fout.precision(2);
fout << "There are " << num << " scores.\n"; fout << "The average is " << sum/num << ".\n"; fin.close(); fout.close(); return 0; }