How to create, modify and delete files in c++

1. Create files

In C++, files can be created and manipulated using the fstream header file from the standard library. Among them, the ofstream class is used to output file streams (that is, write files). It can create new files or open existing files and write data to them.

The following is a simple example code that creates a new file named "example.txt" under the specified path and writes some text content to it:

#include <fstream>
using namespace std;

int main() {
  ofstream outfile("example.txt");  // 创建新文件
  if (outfile.is_open()) {          // 检查是否成功打开
    outfile << "Hello, world!" << endl;  // 向文件中写入数据
    outfile.close();                // 关闭文件流
    cout << "File created successfully." << endl;
  } else {
    cout << "Failed to create file." << endl;
  }
  return 0;
}

Note that after using the file stream, you need to manually call the close() function to close it. This ensures that data is written to the file correctly and frees up system resources.

2. Modify files

In C++, you can use classes fstreamin header files fstreamto read and write files. Specifically, fstreamthe class provides a file stream object that supports both read and write operations.

The following is a sample code that opens a file named "example.txt" under the specified path and replaces the content with new text content:

#include <fstream>
#include <iostream>
using namespace std;

int main() {
  fstream file("example.txt", ios::in | ios::out); // 打开文件
  if (file.is_open()) { // 检查是否成功打开
    file.seekp(0); // 将文件指针移动到文件开头
    file << "This is a new text." << endl; // 将新文本写入文件
    file.close(); // 关闭文件流
    cout << "File modified successfully." << endl;
  } else {
    cout << "Failed to modify file." << endl;
  }
  return 0;
}

In the above code, ios::in | ios::outthe parameters are used to specify that the file stream supports both read and write operations (ie, readable and writable). We then use seekp()a function to move the file pointer to the beginning of the file and write the new text to the file. Finally, be sure to close the file stream to ensure the data is saved correctly.

3. Delete files

In C++, you can delete a file using <cstdio>the functions provided in the header file. remove()This function requires passing in a string parameter representing the file path.

The following is a sample code to delete a file named "example.txt" under the specified path:

#include <cstdio>
#include <iostream>
using namespace std;

int main() {
  const char* file_path = "example.txt"; // 指定文件路径
  if (remove(file_path) != 0) { // 尝试删除文件
    cout << "Failed to delete file." << endl;
  } else {
    cout << "File deleted successfully." << endl;
  }
  return 0;
}

In the above code, we use remove()a function to delete the specified file. If the return value of the function is not 0, it means that the deletion failed. Otherwise, it means the deletion is successful.

(If you have any questions, please post in the comment area, I am online 12 hours a day)

Guess you like

Origin blog.csdn.net/YT21198/article/details/131115345