File Input and Output

Source Code from Notes


Example 1

Example using command line arguments to provide the filenames of the files to be opened.


#include <iostream.h>

main(int argc,char *argv[])
{
	istream	fin;
	ostream fout;

        if (argc != 3) {
                cerr  << "USAGE: program input_file output_file\n";
                exit(1);
        }
	
	fin.open(argv[1]);
	fout.open(argv[2]);

        if (!fin)
	{
                cerr << "Can't open file " << argv[1] << " for reading\n";
                exit(1);
        }
        if (!fout)
	{
                cerr << "Can't open file " << argv[2] << " for writing\n";
                exit(1);
        }

        // rest of program 

}


Example

For example to open a file called Myfile and write some data to it


#include <fstream.h>
main()
{
        // open text file Myfile for writing
        // now
        ofstream my;
        my.open("Myfile");

        if (!my)           // if open didn't work..
        {
                cerr << "Error opening file \n";
                exit();         // output error and quit
        }

        // write to the file
        my << "My name is Judith Bell \n";
}


Example - reading from file and detecting feof

This example opens a file called test. We will assume this file contains a list of floats, which could for example represent the temperature each day for several days. We will then read in these values andcalculate the average value stored in the file.

Note: This program will read the last item in the file twice, since it does not detect the end of file until it tries to read past it.



 // Program to read integers from  an input file
 // called test  and calculate average


#include<iostream.h>
#include<fstream.h>

main()
{

        ifstream fi;
        float input_value, sum = 0.0, average;
        int     no_values = 0;

        fi.open("test");

        if (!fi)
        {
                cout << "Can't open file test\n";
                exit(0);
        }

        while (!fi.eof())
        {
                fi >> input_value;
                sum += input_value;
                no_values ++;
        }

        average = sum / no_values;

        cout << "The average value in file test was "
             << average << "\n";

        fi.close();
}

Corrected Program


#include<iostream.h>
#include<fstream.h>

main()
{

        ifstream fi;
        float input_value, sum = 0.0, average;
        int     no_values = 0;

        fi.open("test");

        if (!fi)
        {
                cout << "Can't open file test\n";
                exit(0);
        }

        fi >> input_value;
        while (!fi.eof())
        {
                sum += input_value;
                no_values ++;
                fi >> input_value;
        }
	average = sum / no_values;

        cout << "The average value in file test was "
	<< average << "\en";

	fi.close();
}

Corrected Program - using arrays

Note: this program also reads file name in as specified by the user.


#include <iostream.h>
#include <fstream.h>

main()
{
        ifstream fi;
        const int max = 100;   // declare a larger array than required
        float input_value[max], sum = 0.0, average;
        int     i, no_values = 0;
        char    file_name[20];

        cout << "enter filename";
        cin >> file_name;

        fi.open(file_name);

        if (!fi)
        {
                cout << "Can't open file test\n";
                exit(0);
        }

        i =0;
        while (!fi.eof())  // read data into array
        {
                fi >> input_value[i];
                i++;
        }

        no_values = i-1;  // discard last element in array

        for (i=0; i< no_values;i++)   // sum together all values in array
                sum += input_value[i];

        average = sum / no_values;

        cout << "The average file in file test was "
             << average << "\n";

        fi.close();
}


Example

Here is a program that creates an output file, writes information to it, closes the file and opens it again as an input file and reads in the information.


#include <iostream.h>
#include <fstream.h>

main()
{
        ofstream        fo;

        fo.open("test_file");

        if (!fo)
        {
                cout << "Cannot open test_file\en";
                exit(0);
        }

        fo << "Hello\en";
        fo << 101.7;

        fo.close();     // close the file

        ifstream        fi;
        fi.open("test_file");

        if (!fi)
        {
                cout << "Cannot open test_file\en";
                exit(0);
        }

        char    str[80];
        float   x;

        fi >> str;
        fi >> x;

        cout << str << "\en" << x << "\en";

        fi.close();
}


Back to Main Page for Lecture 8