Breaking News
yeni film izle hd film seyret film izle

Opening & Closing a File In C++

Opening & Closing a File In C++

We think you’ll find this interesting. You’ll learn how to access disk files in the following sections.

First of all, you have to make use of another header file: fstream.h This header file has a number of classes already defined within it. Some of them are ofstream, ifstream, fstream etc…
To access a file you have to have a stream. Hence first we declare a stream. The stream will be an object of one of the classes in fstream.
ifstream in; // stream in created for input
ofstream out; // stream out created for output
fstream inout; // stream inout created for input and output
Once you’ve created a stream, you can use the open ( ) function to associate it to a disk file. The open ( )function is a member of all the three classes. It can be used as follows:“text.txt”) ; // Opens a file called text.txt for output. The file is opened using the stream out which was created earlier.
Note: When you say that a file is opened for output, it actually means that now you can write data to the file. When a file is opened for input (using ifstream), the data in the file can be displayed on the screen.
Suppose there was a file text.txt already. When using output stream (ofstream), the stream will create a new file text.txt. Whatever content was there in the original text.txt gets deleted and you can write new data to text.txt.
The 3 classes (ofstream, ifstream, fstream) have a constructor function that makes it easier to open a file. Example:
ofstream out(“text.txt”); // This creates an object out for output and opens the file text.txt.
This statement makes use of the constructor in the ofstream class. Ifstream and fstream also have the constructor.
Closing a File
The member function for closing is close ( ). Since you do all I/O through the stream, you have to close the stream as follows:
out.close( ); // To close a stream-type the name of the stream followed by a dot and then close ( ).
Actually you can link this to the object and classes concept. Out is an object and close is a member function of the ofstream class. Hence by saying out.close( ); you’re actually calling the member function.
Can Errors occur while opening a file?
We’ll repeat a point which We made in the previous section:
When you open a file using ofstream (which means output), you can write data to the file. You could say it’s an input to the file. Hence open for output means actually for input.
When a file is opened for reading, you will make use of the ifstream as follows:
ifstream. instream(“test.txt”); // The file test.txt is opened for reading
When you want to read a file, it means that the file is already present in your directory. Hence if you try to open a file (that isn’t present) for reading, what happens?
The answer is, it’s an error. When you open a file for writing data, the stream will create the file even if it doesn’t exist. But if you attempt to open a file for reading and it isn’t present, then it is an error.
Hence you can check whether an error has resulted in the open operation as follows:
if ( ! instream )
cout<< “The file cannot be opened”;
if ( ! instream ) stands for: if not instream (that means if instream not open) then do what is said in the body of the if statement.

Check Also

Virtual Functions In C++

  Virtual function is a member function of a base class and redefined by a …