Using the ifstream member functions to read data from a file, process the data and print the data on the standard output C++ code 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: To read data from a file, process the data and print the data on the standard output using C++ ifstream member functions

To show: How to read data from a file, process the data and print the data on the standard output using C++ ifstream member functions in C++ programming

 

Create a text file named testfileio1.txt, type the following content and save it on C drive.

 

100.23 56.33 67.12 89.10 55.45

23.12 56.11 43.24 65.32 45.00

 

Then run the following program.

 

// a very simple processing data from external file. Read the data in sequential mode, do some

// calculation and display to the standard output. Create testfileio1.txt file on drive C, and type some float data as shown above

#include <iostream>

#include <fstream>

using namespace std;

 

void main(void)

{

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

ifstream inputfile;

 

// opening input file for reading

inputfile.open(filename, ios::in);

// test if fail to open the file error handling for file opening

if(inputfile.fail())

{

cout<<"Opening file "<<filename<<" for reading\n";

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

cout<<"The file could not be 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 error

}

// if successful, do the following

else

{

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

 

// declare some variables for simple calculation

float price, total = 0;

int count = 0;

 

cout<<"Reading data and do some calculation\n\n";

// read data from input stream

inputfile>>price;

 

// test, if end of file not found, do the following

while(!inputfile.eof())

{

// total = total + price

total += price;

count++;

cout<<"Item price # "<<count<<" is "<<price<<endl;

}

cout<<"The total price for "<<count<<" items is: "<<total<<endl;

cout<<"\n-------DONE-------\n"<<endl;

 

// close the input file

inputfile.close();

// test closing file, if fail to close the file, do the error handling for file closing

if(inputfile.fail())

{

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

// something wrong, just exit

exit(1);

}

// if successful closes the file, do

else

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

}

}

 

Output examples:

 

Opening file C:\testfileio1.txt for reading

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

The file could not be opened!

Possible errors:

1. The file does not exist.

2. The path was not found.

Press any key to continue . . .

 

 

 

The C:\testfileio1.txt file was opened successfully!

Reading data and do some calculation

Item price # 1 is 100.23

Item price # 2 is 56.33

Item price # 3 is 67.12

Item price # 4 is 89.1

Item price # 5 is 55.45

Item price # 6 is 23.12

Item price # 7 is 56.11

Item price # 8 is 43.24

Item price # 9 is 65.32

The total price for 9 items is: 556.02

-------DONE-------

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

Press any key to continue . . .

 

 

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