Writing to a file and appending data using ofstream member functions such as open(), fail() and close() C++ program example

 

Compiler: Visual C++ Express Edition 2005

Compiled on Platform: Windows XP Pro SP2

Header file: Standard

Additional library: none/default

Additional project setting: Set project to be compiled as C++

Project -> your_project_name Properties -> Configuration Properties -> C/C++ -> Advanced -> Compiled As: Compiled as C++ Code (/TP)

Other info: none

To do: Writing data to a file and appending data using ofstream member functions such as open(), fail() and close() in C++ programming

To show: How to write data to a file and then appending more data using ofstream member functions such as open(), fail() and close() in C++ programming

 

 

 

Create an empty text file named testfileio2.txt and save it at C drive. Then run the following program.

 

// a simple processing data from external file. Creating, opening and writing some data to a file

// and then appending data at the end of the file

#include <iostream>

#include <fstream>

using namespace std;

 

void main(void)

{

char filename[ ] = "C:\\testfileio2.txt";

ofstream outputfile;

int sampledata;

 

// creating, opening and writing/appending data to a file

outputfile.open(filename, ios::out|ios::app);

 

// simple error handling for file creating/opening for writing, test if fail to open the file

if(outputfile.fail())

{

cout<<"Creating and opening file "<<filename<<" for writing\n";

cout<<"------------------------------------------\n";

cout<<"The "<<filename<<" file could not be created/opened!\n";

cout<<"Possible errors:\n";

cout<<"1. The file does not exist.\n";

cout<<"2. The path was not found.\n";

exit(1); // just exit

// 0-normal, non zero - some errors

}

// else, if the file can be opened

else

{

cout<<"The "<<filename<<" file was created and opened successfully!\n";

cout<<"\nDo some file (re)writing....\n\n";

 

outputfile<<"Writing some data to this file\n";

outputfile<<"------------------------------\n";

cout<<"Check the "<<filename<<" file contents :-)"<<endl;

cout<<"If the file already have had data, the new data will be appended\n";

 

// write some integers to the file

for(sampledata=0; sampledata<=10; sampledata++)

outputfile<<sampledata<<" ";

outputfile<<endl;

 

// close the output file

outputfile.close();

 

// test if fail to close the file, do the following...

if(outputfile.fail())

{

cout<<"The "<<filename<<" file could not be closed!\n";

exit(1);

}

// test if successful to close the file, do the following...

else

cout<<"\nThe "<<filename<<" file was closed successfully!\n";

}

}

 

Output examples:

 

The C:\testfileio2.txt file was created and opened successfully!

Do some file (re)writing....

Check the C:\testfileio2.txt file contents :-)

If the file already have had data, the new data will be appended

The C:\testfileio2.txt file was closed successfully!

Press any key to continue . . .

 

The testfileio2.txt content when the program run twice:

 

 

Writing some data to this file

------------------------------

0 1 2 3 4 5 6 7 8 9 10

Writing some data to this file

------------------------------

0 1 2 3 4 5 6 7 8 9 10

 

 

C and C++ Programming Resources | C & C++ Code Example Index