Chapter 8 Examples

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 5

// Program 8.

1 Writing output to an external file

#include <iostream>
#include <fstream> // step (a)
#include <stdlib.h> // used by exit( )
#include <conio.h>

using namespace std;

void main ()
{
// mandatory when declaring a file for output
ofstream outFile; // step (b)
outFile.open ("f:\\file.txt"); // step (c)

// mandatory when checking the status of the file


if (outFile.fail()) // step (d)
{
cerr << "Output file could not be opened" << endl;
exit(-1);
}

// statements
int a=1, b=2, c=3;
outFile << a << " " << b << " " << c << endl; // step (e)

// mandatory to close a file which was opened


outFile.close(); // step (f)

cout << endl << endl;;


cout << "File written to drive F: \n";
cout << "press any key to exit..." << endl;
getch();
}

/* Output:

A file named file.txt is created in F: drive.


The file details are
1 2 3

*/
// Program 8.2 Writing output to an external file
// Example to write a text file by declaring logical file as fstream.
// Note the change in file opening statement.

#include <iostream>
#include <fstream> // required for file processing functions
#include <stdlib.h>
#include <conio.h>

using namespace std;

void main()
{
const char filename[] = "f:\\Out8_2.txt";
int i, value1, value2;
fstream result; // logical filename declaration

cout << "Type in the start of range: ";


cin >> value1;
cout << "Type in the end of range: ";
cin >> value2;

// associate logical and physical file, and open file for output
result.open(filename, ios::out);

// check for file open status


if (result.fail()) {
cerr << "Output file could not be opened" << endl;
exit(-1);
}

for (i = value1; i <= value2; i++)


result << i << endl; // writing output to the file

result.close(); // close the file


cout << endl << endl;;
cout << "File written to drive F:, ";
cout << "press any key to exit..." << endl;
getch();
}
/* Output
Execution window display:
Type in the start of range: 25
Type in the end of range: 31

File written to drive F:, press any key to exit...

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

A file named Out8_2.txt is created in F: drive.


The file details are
25
26
27
28
29
30
31

*/
// Program 8.3 Reading input from an external file

#include <iostream>
#include <fstream> // step (a)
#include <stdlib.h> // used by function exit( )
#include <conio.h>

using namespace std;

void main ()
{
// mandatory when declaring a file for input
ifstream inFile; // step (b)
inFile.open ("f:\\file.txt"); // step (c)

// mandatory when checking the status of the file


if (inFile.fail()) // step (d)
{
cerr << "Input file could not be opened" << endl;
exit(1);
}

int a, b, c;
inFile >> a >> b >> c; // step (e)

cout << "\nData read from input file is ";


cout << a << ", " << b << " and " << c;

// mandatory to close a file which was opened


inFile.close(); // step (f)

cout << endl << endl;;


cout << "Press any key to exit..." << endl;
getch();
}

/*
If a file named file.txt is created in F: drive.
The file details are
4 5 6
----------------------------------------------------------
Execution Window:
Data read from input file is 4, 5 and 6

Press any key to exit...


*/
// Program 8.4 Reading input from external file using eof flag

#include <iostream>
#include <fstream> // required by file processing functions
#include <stdlib.h>
#include <conio.h>

using namespace std;

void main()
{
int num = 0, value;
fstream data_input; // declaration of input file variable

// association of logical and physical filename and open the file


data_input.open("f:\\file.txt", ios::in);

// check file open status


if (data_input.fail()) {
cerr << "Input file could not be opened" << endl;
exit(-1);
}

// read data from file and store to variable value


data_input >> value;

while (!data_input.eof())// check for end-of-file indicator

{
if (value > 10) num++;
// continue reading data from file and store to variable value
data_input >> value;
}

cout << "The number of values bigger than 10 is "


<< num << endl;

// close the file


data_input.close();

cout << endl << endl;;


cout << "Press any key to exit..." << endl;
_getch();
}

You might also like