Simple File Input and Output in C++

You can save data to a file and read data from the file later.

You used the cin to read input from the keyboard and the cout to write output to the console. You can also read/write data from/to a file. This section introduces simple file input and out­put. Detailed coverage of file input and output will be presented in Chapter 13.

1. Writing to a File

To write data to a file, first declare a variable of the ofstream type:

ofstream output;

To specify a file, invoke the open function from output object as follows:

output.open(“numbers.txt”);

This statement creates a file named numbers.txt. If this file already exists, the contents are destroyed and a new file is created. Invoking the open function is to associate a file with the stream. In Chapter 13, you will learn how to check whether a file exists before creating the file.

Optionally, you can create a file output object and open the file in one statement like this:

ofstream output(“numbers.txt”);

To write data, use the stream insertion operator (<<) in the same way that you send data to the cout object. For example,

output << 95 << ” ” << 56 << ” ” << 34 << endl;

This statement writes numbers 95, 56, and 34 to the file. Numbers are separated spaces, as shown in Figure 4.3.

Figure 4.3 The output stream sends data to the file.

After you are done with the file, invoke the close function from output as follows:

output.close();

Invoking the close function is necessary to ensure the data is written to the file before the program exits.

Listing 4.9 gives the complete program for writing data to a file.

Listing 4.9 SimpleFileOutput.cpp

1 #include <iostream>

2 #include <fstream>

3 using namespace std;

4

5 int main()

6 {

7    ofstream output;

8

9    // Create a file

10    output.open(“numbers.txt”);

11

12    // Write numbers

13    output << 95 << ” ” << 56 << ” ” << 34;

14

15    // close file

16    output.close();

17

18    cout << “Done” << endl;

19

20    return 0;

21 }

Since ofstream is defined in the fstream header file, line 2 includes this header file.

2. Reading from a File

To read data from a file, first declare a variable of the ifstream type:

ifstream input;

To specify a file, invoke the open function from input as follows:

input.open(“numbers.txt”);

This statement opens a file named numbers.txt for input. If a file you attempt to open does not exist, unexpected error may arise. In Chapter 13, you will learn how to check whether a file exists when opening a file for input.

Optionally, you can create a file input object and open the file in one statement like this:

ifstream input(“numbers.txt”);

To read data, use the stream extraction operator (>>) in the same way that you read data from the cin object. For example,

input << score1;

input << score2;

input << score3;

or

input << score1 << score2 << score3;

These statements read three numbers from the file into variables scorel, score2, and score3, as shown in Figure 4.4.

.

After you have done with the file, invoke the close function from input as follows:

input.close();

Listing 4.10 gives the complete program for writing data to a file:

Listing 4.10 SimpleFileInput.cpp

1 #include <iostream>

2 #include <fstream>

3 using namespace std;

4

5 int main()

6 {

7    ifstream input;

8

9    // Open a file

10    input.open(“numbers.txt”);

11

12    int score1, score2, score3;

13

14    // Read data

15    input >> score1;

16    input >> score2;

17    input >> score3;

18

19    cout << “Total score is ” << score1 + score2 + score3 << endl;

20

21    // Close file

22    input.close();

23

24    cout << “Done” << endl;

25

26    return 0;

27 }

Since ifstream is defined in the fstream header file, line 2 includes this header file. You including <fstream> header can simplify the statements in lines 15-17 using the following one statement:

input >> scorel >> score2 >> score3;

Source: Liang Y. Daniel (2013), Introduction to programming with C++, Pearson; 3rd edition.

Leave a Reply

Your email address will not be published. Required fields are marked *