====== File processing examples ======
===== File writing =====
==== Simple example ====
// This program writes data to a file.
#include
#include
using namespace std;
int main()
{
ofstream outfile; // instantiate file pointer object
outfile.open("public_radio.txt"); // associate file with file pointer object
cout << "Writing to the file ... ";
outfile << "KCMP\t" << 89.3 << endl;
outfile << "KNOW\t" << 91.1 << endl;
outfile << "KSJN\t" << 99.5 << endl;
outfile.close(); // close the file!
cout << "Done!" << endl;
return 0;
}
==== Checking for opening errors ====
// This program writes data to a file.
// Shows error checking.
#include
#include
using namespace std;
int main()
{
ofstream outfile; // instantiate file pointer object
outfile.open("public_radio.txt"); // associate file with file pointer object
if (!outfile)
{
cout << "Error opening file." << endl;
return 1; // exit program with an error code
}
cout << "Writing to the file ... ";
outfile << "KCMP\t" << 89.3 << endl;
outfile << "KNOW\t" << 91.1 << endl;
outfile << "KSJN\t" << 99.5 << endl;
outfile.close(); // close the file!
cout << "Done!" << endl;
return 0;
}
==== Using filenames ====
// This program writes data to a file.
// Converting string to c_str.
#include
#include
#include
using namespace std;
int main()
{
ofstream outfile; // instantiate file pointer object
string filename; // used to store filename to save file
// get filename from user
cout << "Enter the filename to store the file in\n"
<< "> ";
cin >> filename;
outfile.open(filename); // associate file with file pointer object
//outfile.open(filename.c_str()); // convert filename to C-string in pre-C++11
if (!outfile)
{
cout << "Error opening file." << endl;
return 1; // exit program with an error code
}
cout << "Writing to the file ... ";
outfile << "KCMP\t" << 89.3 << endl;
outfile << "KNOW\t" << 91.1 << endl;
outfile << "KSJN\t" << 99.5 << endl;
outfile.close(); // close the file!
cout << "Done!" << endl;
return 0;
}
===== File reading =====
==== Simple example ====
// This program reads a number from a file.
#include
#include
#include
using namespace std;
int main()
{
string filename = "some_numbers.txt";
int num1;
ifstream infile; // instantiate file pointer object
infile.open(filename); // associate file with file pointer object
if (!infile)
{
cout << "Error opening file." << endl;
return 1; // exit program with error code
}
cout << "Reading from file ... ";
infile >> num1;
infile.close(); // close the file!
cout << "Done!" << endl;
cout << "The first number in the file is " << num1 << "." << endl;
return 0;
}
==== Reading multiple tokens ====
// This program reads two numbers from a file.
#include
#include
#include
using namespace std;
int main()
{
string filename = "some_numbers.txt";
int num1, num2;
ifstream infile; // instantiate file pointer object
infile.open(filename); // associate file with file pointer object
if (!infile)
{
cout << "Error opening file." << endl;
return 1; // exit program with error code
}
cout << "Reading from file ... ";
infile >> num1 >> num2;
infile.close(); // close the file!
cout << "Done!" << endl;
cout << "The first two numbers in the file are:" << endl
<< num1 << endl
<< num2 << endl
<< endl;
return 0;
}
==== Read all tokens in a file ====
// This program reads all the numbers from a file.
#include
#include
#include
using namespace std;
int main()
{
const string filename = "some_numbers.txt";
int num1;
ifstream infile; // instantiate file pointer object
infile.open(filename); // associate file with file pointer object
if (!infile)
{
cout << "Error opening file." << endl;
return 1; // exit program with error code
}
cout << "Reading from file ... " << endl;
while (infile >> num1)
{
cout << num1 << endl;
}
infile.close(); // close the file!
cout << "Done!" << endl;
return 0;
}