Math2220 Lecture 20

File

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;

void main() {
    ofstream fout;
    fout.open("lecture.txt");
    fout << "Lecture 4: Pointer" << endl
         << "Lecture 5: File I/O" << endl;

    fout.close();

    char line[100];
    cin.getline(line,100);
}



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;

void main() {
    ofstream fout;
    fout.open("lecture.txt", ios::app);

    fout << "Lecture 4: Pointer" << endl
         << "Lecture 5: File I/O" << endl;
    fout.close();

    char line[100];
    cin.getline(line,100);

}



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>

using namespace std;

void main() {
    ifstream fin;
    ofstream fout;

    fin.open("input.txt");
    if(fin.fail()) { // fail to open the file
        cout <<  "Fail to open the file in.txt.";
        char line[100];
        cin.getline(line,100);
        return;
    }

    fout.open("output.txt");
    if(fout.fail()) { // fail to open the file
        cout << "Fail to open the file output.txt.";
        char line[100];
        cin.getline(line,100);
        return;
    }

    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();

    char line[100];
    cin.getline(line,100);
}

Suppose there is no input.txt. The program can't open the file. Here is the output
Fail to open the file in.txt.

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>
using namespace std;


void main() {
    ifstream fin;

    fin.open("score.txt");
    if(fin.fail()) { // fail to open the file
        cout <<  "Fail to open the file score.txt.";
        char line[100];
        cin.getline(line,100);
        return;
    }

    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();

    char line[100];
    cin.getline(line,100);
}


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>
using namespace std;

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

    fin.open("score.txt");
    if(fin.fail()) { // fail to open the file
        cout <<  "Fail to open the file score.txt.";
        char line[100];
        cin.getline(line,100);
        return;
    }

    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();

    char line[100];
    cin.getline(line,100);
}

getline

#include<iostream>
#include<fstream>
using namespace std;

void main() {
    ifstream fin;
    ofstream fout;
    fin.open("in.txt");
    fout.open("out.txt");

    int lineNum = 1;
    char fileLine[100];

    while(!fin.eof()) { // if it is not the end of the file
        fin.getline(fileLine,100);
        fout << lineNum <<": " << fileLine << endl;
        lineNum++;
    }

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

    char line[100];
    cin.getline(line,100);
} 
in.txt
This is line one.
This is line two.
This is line three.
This is line four.
This is line five.
I don't know how to count anymore.
out.txt
1: This is line one.
2: This is line two.
3: This is line three.
4: This is line four.
5: This is line five.
6: I don't know how to count anymore.