Home > Backend Development > C++ > body text

How to use file operation functions in C++?

WBOY
Release: 2023-11-18 15:33:50
Original
1153 people have browsed it

How to use file operation functions in C++?

C is a popular programming language that provides many useful functions and libraries to help developers handle various tasks. Among them, the file operation function is what we often need to use, because files are one of the main ways we store data and information.

In this article, we will introduce how to use the file operation functions in C to read, write, add, delete and modify files. To start this article, let's look at opening and closing files in C.

Opening and closing files

C has a standard file format, namely file header file header . This header file provides three main classes for handling file operations: ifstream, ofstream and fstream. Through them, we can open and close files, read and write files, and other operations.

To open a file, we use ofstream or ifstream object:

ofstream outputFile("example.txt");
Copy after login

In this example, we create a file named "example.txt" and associate it with ofstream object "outputFile" associated. ofstream is used to write to a file and will open the file and empty it. If the file does not exist, it will be created automatically. Similarly, we can use ifstream to open an existing file for reading:

ifstream inputFile("example.txt");
Copy after login

In this case, we create an ifstream object "inputFile" and associate it to the file "example.txt" superior. ifstream is used to read data from a file.

After completing the file operation, we need to close the file. This can be achieved by calling the close() function:

outputFile.close();
inputFile.close();
Copy after login

Reading a file

Reading a file is a common operation, and we usually need to obtain data and information from the file. To read a file we can use ifstream class and getline() function. This function allows us to get a line of text from a file.

ifstream inputFile("example.txt");
string line;

while (getline(inputFile, line)) {
    cout << line << endl;
}

inputFile.close();
Copy after login

In this example, we opened the file "example.txt" using the ifstream object "inputFile" and read each line into the string object "line". Using a while loop, we can output all the contents of the file to the console line by line. Finally, we close the file to free up resources.

Write to file

In addition to reading data from files, we often need to write data to files. To write to a file we can use ofstream class and << operator. This operator is used to write data to a file.

ofstream outputFile("example.txt");

if (outputFile.is_open()) {
    outputFile << "Hello, world!" << endl;
    outputFile.close();
}
Copy after login

In this example, we create an ofstream object "outputFile" and write a line "Hello, world!" to the file "example.txt". We also ensure that the file is opened successfully by calling the is_open() function. Finally, we close the file and release the resources.

Add to file

Sometimes, we need to add data to the end of an existing file instead of overwriting it. To add data at the end of the file we can use ofstream class and ios::app mode. This mode is used to append data rather than overwrite data.

ofstream outputFile("example.txt", ios::app);

if (outputFile.is_open()) {
    outputFile << "This is a new line!" << endl;
    outputFile.close();
}
Copy after login

In this example, we again create an ofstream object "outputFile", but this time we use the ios::app mode to append the data to the end of the file. We write a line of data to the file "This is a new line!" and close the file to free up resources.

Delete files

In some cases, we need to delete files. To delete a file, we need to use the remove() function:

if (remove("example.txt") != 0) {
    cout << "Error deleting file";
} else {
    cout << "File successfully deleted";
}
Copy after login

In this example, we use the remove() function to delete the file "example.txt". If the function executes successfully, the return value is 0; if an error occurs, the return value is non-zero. We can judge whether the operation was successful based on the return value.

Modify files

Finally, sometimes we need to modify files. To modify a file we can use the fstream class. This class is similar to the ifstream and ofstream classes, but can be used to read and write files.

fstream file("example.txt", ios::in | ios::out);

if (file.is_open()) {
    string line;
    while (getline(file, line)) {
        if (line == "Hello, world!") {
            file.seekp(-line.length(), ios::cur);
            file << "This is a new line!";
            break;
        }
    }

    file.close();
}
Copy after login

In this example, we create an fstream object "file" and set its file mode to ios::in | ios::out for reading and writing files. We use a while loop to read a line of data from the file and look for a specific line ("Hello, world!"). If the line is found, the seekp() function is used to move the pointer to the current position and the

This article describes how to use the file operation functions in C to read, write, add, delete and modify files. Since file operation functions play an important role in writing code, familiarity with their syntax and usage is essential for C development.

The above is the detailed content of How to use file operation functions in C++?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!