Work with Files in C++
Submitted by pavel7_7_7 on Thursday, February 6, 2014 - 07:26.
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
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:
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++.
"\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:
- create an instance of ofstrem
- Open file with function
open
- Write data to file
- close file using
close()
- it's very simple to create an ofstream object.
You just need to write
ofstream output;//stream to write output.txt
- 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: - When you opened a file, you can write data to the file. Let's look on an example:
As you can see, it's very easy to write data to a file. You can work with the ofstream object as with the
- #include <iostream>
- #include <fstream>
- using namespace std;
- int main()
- {
- ofstream output;//define a stream used to write data to file
- output.open ("output.txt");//open file output txt to write data
- int age = 21;
- string name = "Bob";
- float salary = 2100.12;
- output << "Hi, my name is " << name << endl;
- output << "I'm " << age << " years old \n";
- output << "My salary is " << salary << " $" << endl;
- output.close();
- return 0;
- }
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 $ - The last and the simple step is to close file.It can be done in a simple call of close function:
output.close();
- create an instance of ifstrem
- Open file with function
open
- Read data from file
- close file using
close()
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:
- int arr[3][3];
- ifstream input;
- input.open("input.txt");
- for(int i = 0; i != 3; ++i)
- for(int j = 0; j != 3; ++j)
- input >> arr[i][j];
- input.close();
- int sum = 0;
- for(int i = 0; i != 3; ++i)
- for(int j = 0; j != 3; ++j)
- sum += arr[i][j];
- //reopen input.txt
- output.open("input.txt",ios_base::app);
- output << endl << "The sum of array elements is " << sum;
- output.close();
Add new comment
- 164 views