Work with Files in C++

In this article you'll read about how to work with files in c++. Files allow users to read a huge amount of data directly from the disc without entering the data from keyboard. There are two main types of files : text files and binary files. Text files are files that consist of the sequences of any symbols. This sequence is organized in rows that are separated by the newline character "\n". The file end with the end-of-file character EOF. In the binary files information is written in blocks of a special size, where information of any type and structure can be stored. The process of reading or writing data from/to file is closely connected to the stream. To work with files two many types of streams are used: ifstream to read data from file and ofstream to write data to file. To be able to work with files you must include iostream and fstream libraries to your program. The basic steps to write data to file are:
  1. create an instance of ofstrem
  2. Open file with function open
  3. Write data to file
  4. close file using close()
Now, let's speak more about these steps. Write information to text file
  1. it's very simple to create an ofstream object. You just need to write ofstream output;//stream to write output.txt
  2. Now, you have to open the file. The file can be opened using open method. void open (const char* filename,  ios_base::openmode mode = ios_base::out); This function takes twwo arguments: first is name of file to be opened and the second is the mode used to open file. The "out" mode is default mode. The list of the other modes is described here: mode
  3. When you opened a file, you can write data to the file. Let's look on an example:
    1. #include <iostream>
    2. #include <fstream>
    3. using namespace std;
    4. int main()
    5. {
    6. ofstream output;//define a stream used to write data to file
    7. output.open ("output.txt");//open file output txt to write data
    8. int age = 21;
    9. string name = "Bob";
    10. float salary = 2100.12;
    11. output << "Hi, my name is " << name << endl;
    12. output << "I'm " << age << " years old \n";
    13. output << "My salary is " << salary << " $" << endl;
    14. output.close();
    15. return 0;
    16. }
    As you can see, it's very easy to write data to a file. You can work with the ofstream object as with the  cout that is a stream to. When you execute the program, in the current directory an "output.txt" file will appear with the next data: Hi, my name is Bob I'm 21 years old My salary is 2100.12 $
  4. The last and the simple step is to close file.It can be done in a simple call of close function: output.close();
Reading data from a file The process of reading data it's similar to the process to writing data to a file. The basic steps are:
  1. create an instance of ifstrem
  2. Open file with function open
  3. Read data from file
  4. close file using close()
The main difference consists in the mode we open the file. Now we need to read data from file so we open it in in mode. void open (const char* filename,  ios_base::openmode mode = ios_base::in); Now, let's look at an example. We will read data from input.txt. In input.txt will be stored a two dimensional array of integers. "input.txt": 1 5 7 0 2 4 3 7 8 And the code to read this data is:
  1. int arr[3][3];
  2. ifstream input;
  3. input.open("input.txt");
  4. for(int i = 0; i != 3; ++i)
  5. for(int j = 0; j != 3; ++j)
  6. input >> arr[i][j];
  7. input.close();
We read the data, but now we will check, if the data is read correct. For this check, let's calculate the sum of the elements of the array. The result will be appended to the end of the input.txt file:
  1. int sum = 0;
  2. for(int i = 0; i != 3; ++i)
  3. for(int j = 0; j != 3; ++j)
  4. sum += arr[i][j];
  5. //reopen input.txt
  6. output.open("input.txt",ios_base::app);
  7. output << endl << "The sum of array elements is " << sum;
  8. output.close();
And the data in input.txt is 1 5 7 0 2 4 3 7 8 The sum of array elements is 37 Of course, this is a simple example of reading data from a file and writing data to a file, but thess are the basic steps to learn, how to use files in c++.

Add new comment