lecture.txt
Lecture 1: Introduction to C++ Lecture 2: Flow of Control Lecture 3: Functions |
#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);
}
|
lecture.txt after you execute the program. The
original data are gone.
Lecture 4: Pointer Lecture 5: File I/O |
#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);
}
|
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 |
#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);
}
|
fin.fail() returns true if the program can't
open the file. example, when the file doesn't exist.input.txt. The program can't
open the file. Here is the output
Fail to open the file in.txt. |
input.txt
25 35 45 55 65 |
output.txt will be
created
The sum of the first 3 numbers are 105 |
#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);
}
|
fin.eof() returns true if the program reaches
the end of the 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 |
There are 15 scores. The average is 68.03. |
#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);
}
|
cout by fout in the magic
formula.
#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. |