
What is File handling in C++?
- File handling in C++ refers to the ability of a C++ program to read data and write data to files.
- File handling allows programs to interact with external files, such as text files, binary files, and more.
C++ Streams
In C++, streams are used for input and output operations. There are two basic stream classes:
- istream: Used for input (e.g., cin for keyboard input).
- ostream: Used for output (e.g., cout for console output).
C++ Predefined Streams
C++ provides three predefined streams:
- cin: Standard input stream, used for reading from the keyboard.
- cout: Standard output stream, used for writing to the console.
- cerr: Standard error stream, used for writing error messages to the console.
I/O Operations
I/O operations involve reading and writing data. For example:
Loading…
In this code, we read an integer from the user using cin and output the result using cout.
Unformatted Console I/O Operations
Unformatted I/O includes functions like get() and put() for character-based input and output.
Loading…
Manipulators
- Manipulators are functions or objects that allow you to control the formatting of data when it's being displayed.
- For example, std::setw() sets the width of the output field, ensuring that the output is neatly aligned.
Loading…
In this code, setw(10) sets the width of the output to 10 characters, ensuring the number is right-aligned.
File Modes in C++
Input Mode (ios::in)
- When you open a file with ios::in mode, you are specifying that you intend to read data from the file.
- If the file does not exist or cannot be opened for reading, an error will occur.
Syntax:
Loading…
Input Mode in C++ Example
Loading…
Output Mode (ios::out)
- This mode is used to open a file for writing.
- If the file exists, its content will be truncated; if it doesn't exist, a new file will be created.
- Syntax:
Loading…
Output Mode in C++
Loading…
Append Mode (
This mode is used to open a file for writing, appending data to the end if it exists, or creating a new file if it doesn't.
Syntax:
Loading…
Append Mode in C++
Loading…
Binary Mode (
This mode is used when working with binary files.
Syntax:
Loading…
Binary Mode in C++
Loading…
Opening and Closing a File in C++
- In C++, you can work with files by using ifstream (for reading) and ofstream (for writing) classes.
- To open a file, you typically provide its name and specify the mode (ios::in for input, ios::out for output).
Loading…
Opening and closing files is crucial to ensure that data is properly saved and resources are released.
Error Handling during File Operations in C++
- When working with files, it's essential to check whether file operations were successful.
- For example, when opening a file, you should verify if it opened successfully using the is_open() function.
Sequential Access to File
- Sequential access to a file means reading or writing data in the order it appears in the file, from the beginning to the end.
- Here's an example of reading and writing sequentially to a file:
Loading…
In this code, we first write three lines of text sequentially to a file and then read them back sequentially.
Random Input and Output Operations
- Random input and output operations involve moving the file pointer to a specific location in the file and reading or writing data from that point.
- Here's an example of random access:
Loading…
- In this code, we open a file for both reading and writing. We use seekp to set the file pointer to position 10 and write "Hello" there.
- Then, we use seekg to move the file pointer to position 5 and read data from that position.
Persistent Objects
Persistent objects are objects that can be saved to a file and loaded back later. Here's a simple example using serialization:
Loading…
- In this code, we have a Person class with Save and Load methods to serialize and deserialize the object's data to/from a file.
- We save a Person object to a file, and then we load and display the saved object.
Command Line Arguments
- Command line arguments are values passed to a program when it is executed from the command line.
- Here's a simple example of how to access and use command line arguments:
Loading…
- In this code, argc represents the number of command line arguments, and argv is an array of C-style strings (char arrays) containing the actual argument values.
- We print the number of arguments and then loop through and print each argument's value.
Conclusion
File handling in C++ is a crucial aspect of programming, allowing us to interact with external files for reading, writing, and manipulating data.