Chapter 18 – File Input and Output

18.1  File Output

 

So far, all programs in this book only output to the screen (console).  This section shows how to output to a file.  This adds a lot of flexibility with your programming – the ability to output data into a file that could be used to import the data into a different application.  Outputting to a file is done by creating a file output file stream using the fstream library.  A file output stream works very similar to the cout statement, but the results will be sent to a file instead of the console.  The following program demonstrates output to a file named "test.txt".

 

Program 18.1 – Output to a File

#include <iostream>

#include <fstream>

using namespace std;

int main()
{
  ofstream fout("test.txt");

  cout << "This sentence is output to the console.\n";

  fout << "This sentence is output to the file.\n";

  return 0;

}

Console Output
This sentence is output to the console.

 

File Output

This sentence is output to the file.

 

In the preceding program, the iostream library is included for console output using the cout statement.  The fstream library is included for file output.  The ofstream (output file stream) command is used to create an output stream called fout which is directed to a file named “test”.  Note that an output file stream can be named anything (not just fout), but fout is a common name for the file output stream since it works similar to the cout statement.

 

If you run this program on a Linux system, then do a directory listing by entering ls at the $ prompt.  You will notice that a file named "test.txt" was created in the same folder as your source code file.

18.2  File Input

 

Whereas the cin statement gets data from the keyboard and places into variables, the fin statement gets data from a file and places into variables.  The fin statement is located in the fstream library.  Reading data from a file is useful when you are working with large amounts of data or working with data that was exported from another program.  Before you can input data from a file, you must first open it with the ifstream fin command.

 

The next program uses an input file named "data.txt" which contains two numbers.  If you are using Windows, this file can be created using notepad and placed in the same folder as your source code file.  The numbers can be separated by spaces, tabs, or carriage returns.  The program reads the data into the variables A and B.

 

Program 18.2a – Input Data from a File

#include <fstream>

#include <iostream>

using namespace std;

int main()
{
  ifstream fin("data.txt");

  float A, B;

  fin >> A;

  fin >> B;

  cout << "The numbers are " << A << " and " << B << endl;

  return 0;
}

Input File – "data.txt"

25.2 17.6

Output
The numbers are 25.2 and 17.6

 

In the next program, we add eight more numbers to the input file.  The file "data.txt" now has ten numbers in it.  Instead of declaring ten variables and using ten fin statements, we declare an array and use a for loop for inputting the data.  We also declare a variable named Total and add each number to it.

 

Program 18.2b – Input Data from a File into an Array

#include <fstream>

#include <iostream>
using namespace std;

int main()
{
  ifstream fin("data.txt");

  float MyData[10];

  float Total = 0;

  for (int X=1; X<=10; X++)

  {

    fin >> MyData[X];

    Total = Total + MyData[X];

  }

  cout << "The total of the numbers is " << Total << endl;

  return 0;
}

Input File – "data.txt"

25.2 17.6 14.7 8.9 10.5 3.1 5.3 19.4 21.4 25.8

Output
The total of the numbers is 151.9