Javatpoint Logo
Javatpoint Logo

Opening and Closing a File in C in C++ Pdf

File handling operation is a very important part of programming in C++. In most programs, we need to read from or write to files. In C++, we can use the file handling library to perform file operations. This library provides several functions that allow us to open, read, write, and close files. In this article, we will see how to open and close a file in C++.

File handling refers to the process of reading from or writing to files. In C++, we can use the file handling library to perform file operations. This library provides several functions that allow us to open, read, write, and close files. We can use file handling to perform tasks such as storing data, retrieving data, and modifying data in files.

File modes in C++:

In C++, files can be opened in various modes depending on the operations we want to perform on them. Here are the commonly used file modes in C++:

  • std::ios::in:

This mode is considered when we want to open a file for reading. When this mode opens a file, we can only read from it but not write to it.

C Code:

  • std::ios::out:

This mode is considered when we want to open a file for writing. When this mode opens a file, we can only write to it but not read from it. If the file doesn't exist, it will be created.

C Code:

  • std::ios::app:

This mode is considered when we want to open a file and append data to the end of it. If the file doesn't exist, it will be created.

C Code:

  • std::ios::ate:

This mode is considered when we want to open a file and seek to the end of it immediately. This is useful when we want to append data to the end of a file or read the entire file at once.

C Code:

  • std::ios::binary:

This mode is considered when we want to open a file in binary mode. In binary mode, no newline translation is performed, and the file is treated as a sequence of bytes. This mode is typically used when working with non-text files such as images or executables.

C Code:

Note: We can combine these modes using the bitwise OR operator (|). For example, to open a file for both reading and writing, we can use the following:

C Code:

It's important to note that when a file is opened in write mode, the contents of the file will be overwritten. To avoid this, we can use the std::ios::app mode to append data to the end of the file instead.

Opening a file in C++:

To open a file in C++, we can use the ofstream and ifstream classes. The ofstream class is used to write to a file, while the ifstream class is used to read from a file. Both classes are derived from the fstream class, which can be used for reading and writing files.

Below is an example of how to open a file for writing using the ofstream class:

C++ Code:

In this example, we create an object of the ofstream class named "outfile". We then use the open() function to open a file named "example.txt" in output mode (ios::out). We can then write to the file using the various output functions provided by the ofstream class. Once we are done writing to the file, we use the close() function to close the file.

Similarly, here's an example of how to open a file for reading using the ifstream class:

C++ Code:

In this example, we create an object of the ifstream class named "infile". We then use the open() function to open a file named "example.txt" in input mode (ios::in). We can then read from the file using the various input functions provided by the ifstream class. Once we are done reading from the file, we use the close() function to close the file.







Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA