Opening files for read and write in C++

By: Kamini Viewed: 170 times  Printer Friendly Format    


To open the file myfile.cpp with an ofstream object, declare an instance of an ofstream object and pass in the filename as a parameter:

ofstream fout("myfile.cpp");

Opening this file for input works exactly the same way, except it uses an ifstream object:

ifstream fin("myfile.cpp");

Note that fout and fin are names you assign; here fout has been used to reflect its similarity to cout, and fin has been used to reflect its similarity to cin.

One important file stream function that you will need right away is close(). Every file stream object you create opens a file for either reading or writing (or both). It is important to close() the file after you finish reading or writing; this ensures that the file won't be corrupted and that the data you've written is flushed to the disk.

Once the stream objects are associated with files, they can be used like any other stream objects. Listing below illustrates this.

Opening files for read and write.

1:     #include <fstream.h>
2:     int main()
3:     {
4:        char fileName[80];
5:        char buffer[255];    // for user input
6:        cout << "File name: ";
7:        cin >> fileName;
8:
9:        ofstream fout(fileName);  // open for writing
10:       fout << "This line written directly to the file...\n";
11:       cout << "Enter text for the file: ";
12:       cin.ignore(1,'\n');  // eat the newline after the file name
13:       cin.getline(buffer,255);  // get the user's input
14:       fout << buffer << "\n";   // and write it to the file
15:       fout.close();             // close the file, ready for reopen
16:
17:       ifstream fin(fileName);    // reopen for reading
18:       cout << "Here's the contents of the file:\n";
19:       char ch;
20:       while (fin.get(ch))
21:          cout << ch;
22:
23:       cout << "\n***End of file contents.***\n";
24:
25:       fin.close();            // always pays to be tidy
26:     return 0;
27: }

Output: File name: test1
Enter text for the file: This text is written to the file!
Here's the contents of the file:
This line written directly to the file...
This text is written to the file!

***End of file contents.***

Analysis: On line 4, a buffer is set aside for the filename, and on line 5 another buffer is set aside for user input. The user is prompted to enter a filename on line 6, and this response is written to the fileName buffer. On line 9, an ofstream object is created, fout, which is associated with the new filename. This opens the file; if the file already exists, its contents are thrown away.

On line 10, a string of text is written directly to the file. On line 11, the user is prompted for input. The newline character left over from the user's input of the filename is eaten on line 12, and the user's input is stored into buffer on line 13. That input is written to the file along with a newline character on line 14, and then the file is closed on line 15.

On line 17, the file is reopened, this time in input mode, and the contents are read, one character at a time, on lines 20 and 21.



Most Viewed Articles (in C++ )

Latest Articles (in C++)

Comment on this tutorial