Home >Backend Development >C++ >Using text files for input and output in C++
In C, the processing of text files is very important, because most programs need to perform input and output with external files, such as reading data files stored on disk or converting Data within the program is stored in files. Therefore, this article will introduce how to use text files for input and output in C.
1. Opening and closing text files
In C, you need to use the file stream class object (fstream) to read and write files. Before using a file, you need to create a file stream object and associate it with the file. The constructor of a file stream object can accept the file name and opening method as parameters. There are three common file opening methods in C:
The following is a simple example to demonstrate how to open a file:
#include <fstream> using namespace std; int main() { // 创建文件流对象并关联文件 fstream file("example.txt", ios::in | ios::out); // 判断文件是否打开成功 if (!file) { cout << "无法打开文件" << endl; return -1; } // 文件操作 // 关闭文件 file.close(); return 0; }
In the above code, a file stream object named file is created through the constructor of the fstream class. And associate it with the file example.txt. When a file is opened, a method is used that is both readable and writable. In other words, when you need to read a file, you need to use the ios::in option; when you need to write a file, you need to use the ios::out option; when you need to add content at the end of the file, you need to use ios ::appoptions. For convenience, both readable and writable methods are used here.
After the file operation is completed, the file needs to be closed. This can be achieved through the close() function of the file stream object.
2. Reading data from the file
After opening the text file, we can use the file input stream object (ifstream) to read data from the text file. C provides a variety of methods to read data from files, including the following:
Operator: Read a word or a value from the file.
The following is a simple example that demonstrates how to read data from a file:
#include <fstream> #include <iostream> using namespace std; int main() { // 创建文件流对象并关联文件 ifstream file("example.txt"); // 判断文件是否打开成功 if (!file) { cout << "无法打开文件" << endl; return -1; } // 读取文件内容 string str; while (getline(file, str)) { cout << str << endl; } // 关闭文件 file.close(); return 0; }
In the above code, we create an input file stream object named file, And associate it to the file example.txt. In the while loop, the file content is read line by line by calling the getline() function, and each line of data is stored in the string variable str. Finally, the read content is output through cout.
3. Write data to a file
In C, you can use the file output stream object (ofstream) to write data to a text file. Commonly used writing functions include the following:
The following is a simple example that demonstrates how to write data to a file:
#include <fstream> #include <iostream> using namespace std; int main() { // 创建文件流对象并关联文件 ofstream file("example.txt"); // 判断文件是否打开成功 if (!file) { cout << "无法打开文件" << endl; return -1; } // 向文件中写入数据 for (int i = 1; i <= 10; i++) { file << "This is line " << i << endl; } // 关闭文件 file.close(); return 0; }
In the above code, we create an output file stream object named file, And associate it to the file example.txt. In the for loop, data is written to the file through the
4. File pointer
When performing file operations, you need to use a pointer to indicate the current file read and write location. The file pointer, often called the file position indicator, is an internal variable maintained by each file that records the number of bytes from the start of the file to the current position.
C provides the following functions to operate the file pointer:
The following is a simple example that demonstrates how to operate the file pointer:
#include <fstream> #include <iostream> using namespace std; int main() { // 创建文件流对象并关联文件 fstream file("example.txt", ios::in | ios::out); // 判断文件是否打开成功 if (!file) { cout << "无法打开文件" << endl; return -1; } // 获取、设置文件指针位置 file.seekp(6); // 将文件写位置设置为6 file.put('X'); // 在文件位置6处写入字符'X' file.seekg(0); // 将文件读位置设置为0 char ch = file.get(); // 从文件位置0读取一个字符 cout << "第一个字符是:" << ch << endl; // 关闭文件 file.close(); return 0; }
In the above code, we create a file stream object named file and associate it with to the file example.txt. Before manipulating the file pointer, the file needs to be opened.
In the example, we use the seekp() function to set the file pointer position to 6, and then use the put() function to write the character 'X' at file position 6. Next, use the seekg() function to set the file pointer position to 0, use the get() function to read a character from file position 0, and output it.
5. Summary
The above are the basic operations for input and output of text files using C. When performing file operations, you need to pay attention to the order in which files are opened and closed, as well as the operation of the file pointer position. Text file reading and writing operations, pointer positioning and other operations are very simple, but you need to keep in mind the commonly used functions and operation methods.
The above is the detailed content of Using text files for input and output in C++. For more information, please follow other related articles on the PHP Chinese website!