Comment faire une recherche par mot sur un fichier ?

C++ File Handling: Read and Write Made Easy

10/07/2008

Rating: 4.74 (13871 votes)

Working with files is a fundamental skill for any programmer, allowing your applications to store and retrieve data persistently. In C++, the Standard Library provides a robust and flexible way to manage file input and output operations through the `fstream` library. This comprehensive guide will walk you through the essential concepts and practical implementation of reading from and writing to files in C++.

Comment écrire dans un fichier c++ ?
La fonction prend cette syntaxe : Vous pouvez écrire dans un fichier directement depuis votre C++ programme. Pour cela, vous utilisez l'opérateur d'insertion de flux (<<). Le texte à écrire dans le fichier doit être placé entre guillemets.[/caption]
Table

Understanding File Handling in C++

At its core, file handling in C++ involves interacting with files stored on a storage device. This allows your programs to go beyond temporary memory and save information that can be accessed later, even after the program has finished executing. Think of files as permanent storage for your data. The `fstream` library abstracts the complexities of direct hardware interaction, providing you with a stream-based interface. A stream can be thought of as a sequence of bytes, acting either as a source from which to read data or a destination to which data is written.

The `fstream` Library: Your Toolkit for File Operations

The C++ Standard Library equips you with three key classes within the `fstream` header for file manipulation:

  • `ofstream` (Output File Stream): This class is designed for writing data to files. It allows you to create new files or overwrite existing ones, effectively sending data from your program to a file on disk.
  • `ifstream` (Input File Stream): Conversely, `ifstream` is used for reading data from files. It enables your program to retrieve information stored within a file.
  • `fstream` (File Stream): This versatile class combines the functionalities of both `ofstream` and `ifstream`. It can be used for both reading from and writing to files, offering greater flexibility when you need to perform both operations on the same file.

To utilise these classes, you must include the `` header in your C++ source file using the preprocessor directive:

#include <fstream> #include <iostream> // Often needed for console output using namespace std; 

Opening Files: Setting the Stage for Interaction

Before you can read from or write to a file, you must first open it. This establishes a connection between your program and the file on the storage device. Each of the stream classes (`ofstream`, `ifstream`, `fstream`) provides an `open()` member function for this purpose. The `open()` function typically takes two arguments:

void open(const char* filename, std::ios_base::openmode mode = std::ios_base::out);

Where:

  • filename: A string representing the name of the file you wish to open (e.g., "mydata.txt").
  • mode: An optional argument specifying how the file should be opened. This is crucial for determining the type of operations you intend to perform.

File Opening Modes

The `mode` parameter is a bitmask that allows you to specify various flags controlling the file opening behaviour. Here are the most common modes:

ModeDescription
ios::inOpen for reading. This is the default for `ifstream`.
ios::outOpen for writing. If the file exists, its contents are discarded. This is the default for `ofstream`.
ios::appAppend mode. All output operations write data to the end of the file.
ios::binaryOpen in binary mode. This is important for non-text files (like images or executables).
ios::truncTruncate mode. If the file exists, its contents are deleted before opening.
ios::ateSeek to the end of the file upon opening. Useful for determining file size.

You can combine multiple modes using the bitwise OR operator (`|`). For instance, to open a file for both reading and writing, you would use:

myFile.open("data.txt", ios::in | ios::out); 

Example: Creating and Opening a File for Writing

Let's start with a simple example of creating a file and preparing it for writing:

#include <iostream> #include <fstream> int main() { std::ofstream outputFile("my_output_file.txt"); // Opens for writing, creates if not exists if (!outputFile.is_open()) { std::cerr << "Error: Could not open file for writing!\n"; return 1; // Indicate an error } std::cout << "File 'my_output_file.txt' opened successfully for writing.\n"; // Further operations (writing) would go here outputFile.close(); // Close the file when done return 0; } 

In this example, `std::ofstream outputFile("my_output_file.txt");` attempts to open the file named "my_output_file.txt" for writing. If the file doesn't exist, it will be created. The `is_open()` method is a crucial check to ensure the operation was successful.

Writing to Files: Sending Data to Disk

Once a file is open for writing (using `ofstream` or `fstream` with `ios::out` or `ios::app`), you can use the stream insertion operator (`<<`), just like you would with `std::cout`, to write data to the file. The data is sent to the file stream, which then writes it to the actual file.

Example: Writing Text to a File

Building on the previous example, let's write some text into the file:

#include <iostream> #include <fstream> int main() { std::ofstream outputFile("my_output_file.txt"); if (!outputFile.is_open()) { std::cerr << "Error: Could not open file for writing!\n"; return 1; } outputFile << "This is the first line of text.\n"; outputFile << "Here is some more data.\n"; int number = 123; outputFile << "And a number: " << number << std::endl; std::cout << "Data written to 'my_output_file.txt'.\n"; outputFile.close(); return 0; } 

After running this code, you'll find a file named `my_output_file.txt` in the same directory as your executable, containing the specified text. Notice how we can write strings, integers, and use manipulators like `std::endl` just as we would with console output.

Reading from Files: Retrieving Stored Data

To read data from a file, you'll use the stream extraction operator (`>>`) with an `ifstream` or `fstream` object opened in `ios::in` mode. The `>>` operator reads data from the file stream and stores it into a variable. By default, it reads data separated by whitespace.

Example: Reading Text from a File

Now, let's create a program to read the contents of the file we just wrote:

#include <iostream> #include <fstream> #include <string> int main() { std::ifstream inputFile("my_output_file.txt"); // Opens for reading if (!inputFile.is_open()) { std::cerr << "Error: Could not open file for reading!\n"; return 1; } std::string line; std::cout << "Contents of 'my_output_file.txt':\n"; // Read the file line by line while (std::getline(inputFile, line)) { std::cout << line << std::endl; } // Alternative: Reading word by word /* std::string word; while (inputFile >> word) { std::cout << word << " "; } std::cout << std::endl; */ inputFile.close(); return 0; } 

This code opens `my_output_file.txt` for reading. The `std::getline(inputFile, line)` function reads the file content line by line into the `line` string variable until the end of the file is reached. The commented-out section shows how you could read word by word using the `>>` operator.

Reading Character by Character

The example provided in the prompt also demonstrates reading character by character:

#include <iostream> #include <fstream> int main() { std::fstream my_file; my_file.open("my_output_file.txt", std::ios::in); if (!my_file.is_open()) { std::cerr << "No such file or directory\n"; return 1; } char ch; while (my_file >> ch) { // Reads character by character if (my_file.eof()) break; // Check for end-of-file std::cout << ch; } std::cout << std::endl; my_file.close(); return 0; } 

This method reads each character (`char ch`) from the file and prints it to the console. The `my_file.eof()` check is essential to determine when the end of the file has been reached, preventing infinite loops.

Closing Files: Releasing Resources

It is crucial to close files after you are finished with them. Closing a file performs several important tasks:

  • It ensures that any buffered data is written to the file (flushing the stream).
  • It releases the file handle and associated system resources, making them available for other processes.
  • It prevents data corruption or loss.

Both `ofstream`, `ifstream`, and `fstream` objects have a `close()` member function:

void close(); 

While C++ programs will automatically close open files when they terminate, it's considered good practice for the programmer to explicitly close them when they are no longer needed. This demonstrates good resource management.

[caption id="attachment_93034" align="aligncenter" width="704"]Qu'est-ce que la fonction des mots ? "La fonction des mots est de marquer pour nous-mêmes, et de rendre manifeste à autrui les pensées et les conceptions de notre esprit.

Common Scenarios and Best Practices

Appending to a File

If you want to add data to the end of an existing file without overwriting its current content, use the `ios::app` mode:

std::ofstream appendFile("my_log.txt", std::ios::app); if (appendFile.is_open()) { appendFile << "New log entry.\n"; appendFile.close(); } 

Error Handling

Always check if a file was opened successfully using `is_open()` or by directly evaluating the stream object in a boolean context (e.g., `if (myFile)`). This is vital for robust applications.

Binary Files

For non-text files (images, executables, etc.), you must open them in binary mode using `ios::binary`. Reading and writing binary data requires different handling than text data.

Frequently Asked Questions (FAQs)

Q1: How do I check if a file exists before trying to open it?

A1: While you can attempt to open an `ifstream` and check `is_open()`, a more direct way to check for existence is to use a `std::ifstream` and then check its state:

std::ifstream infile("myfile.txt"); if (infile.good()) { // File exists and is readable infile.close(); } else { // File does not exist or is not readable } 

Q2: What's the difference between `getline` and the `>>` operator for reading?

A2: The `>>` operator reads data delimited by whitespace (spaces, tabs, newlines), stopping at the first whitespace character. `std::getline(stream, string)` reads an entire line of text, including spaces, until it encounters a newline character or the end of the file.

Q3: Can I read and write to the same file simultaneously?

A3: Yes, you can use the `fstream` class with the `ios::in | ios::out` mode. However, be cautious with the order of operations and consider using `ios::app` or `ios::trunc` to manage the file's state correctly.

Q4: What happens if I try to write to a file that doesn't exist with `ios::out`?

A4: If the file does not exist and you open it with `ios::out` (or `ofstream` by default), the file will be created.

Q5: How do I handle errors during file writing or reading?

A5: Beyond checking `is_open()`, you can use stream state flags like `fail()`, `bad()`, and `eof()` to monitor the status of I/O operations. For example, after a read operation, `stream.fail()` will be true if the read failed.

Mastering file handling in C++ is an essential step in developing more sophisticated and data-driven applications. By understanding the `fstream` library, its classes, and modes, you can confidently manage data persistence in your C++ projects.

If you want to read more articles similar to C++ File Handling: Read and Write Made Easy, you can visit the Automotive category.

Go up