C/C++---文件读取
在 C++ 中,文件读取操作主要是通过 fstream
类来完成的。fstream
类提供了多种功能,用于从文件读取数据、写入数据以及对文件进行其他处理。文件读取操作可以通过两种主要方式实现:文本文件读取和二进制文件读取。
文件读取在传参工作中,扮演十分重要的角色,方便客户端不接触代码的情况下对系统进行调试。
1. 文件输入流 (ifstream
)
C++ 提供了 ifstream
(Input File Stream)类用于文件读取操作。它是 fstream
类的派生类,专门用于从文件中读取数据。
1.1. ifstream
基本操作
基本的文件读取操作包含打开文件、读取数据、关闭文件等步骤。
1.2. 文件打开
在 C++ 中,通过 ifstream
对象的构造函数或者 open()
函数来打开文件。
std::ifstream file("filename.txt"); // 打开文件进行读取
或者通过 open()
函数:
std::ifstream file;
file.open("filename.txt"); // 打开文件进行读取
1.3. 判断文件是否成功打开
可以使用 is_open()
函数检查文件是否成功打开:
if (!file.is_open()) {std::cerr << "Failed to open file!" << std::endl;
}
1.4. 读取文件内容
文件的读取通常有三种方式:逐字符读取、逐行读取和按格式读取。
- 逐字符读取:使用
get()
函数读取每个字符,包括换行符。
char c;
while (file.get(c)) {std::cout << c; // 打印字符
}
- 逐行读取:使用
getline()
函数读取每一行,直到遇到换行符。
std::string line;
while (std::getline(file, line)) {std::cout << line << std::endl; // 打印每一行
}
- 按格式读取:使用
>>
操作符按照指定格式读取数据(例如整数、浮点数等)。
int a;
double b;
while (file >> a >> b) {std::cout << a << " " << b << std::endl;
}
1.5. 文件结束判断
C++ 的 ifstream
类提供了 eof()
函数来判断是否到达文件的末尾,但是一般来说,使用 while
循环的条件判断可以更好地避免 EOF 错误。例如使用 while (file >> a)
判断文件是否还有数据。
1.6. 关闭文件
在读取完文件后,应该关闭文件:
file.close(); // 关闭文件
2. 处理文件错误
文件操作中常常会遇到各种错误,例如文件不存在、权限问题等。在 C++ 中,可以通过 fail()
函数来检测文件是否读取失败。
if (file.fail()) {std::cerr << "File reading failed!" << std::endl;
}
3. 文件模式
在打开文件时,可以指定文件的模式。常用的文件模式有:
- std::ios::in:以读模式打开文件(默认模式)。
- std::ios::out:以写模式打开文件。
- std::ios::app:以追加模式打开文件。
- std::ios::binary:以二进制模式打开文件。
std::ifstream file("filename.txt", std::ios::in); // 以读取模式打开文件
4. 示例代码
以下是一个完整的文件读取操作示例:
#include <iostream>
#include <fstream>
#include <string>int main() {std::ifstream file("FOV.xyz");// 判断文件是否成功打开if (!file.is_open()) {std::cerr << "Failed to open file!" << std::endl;return 1;}int width, height;double t_l, t_w, scale, chuan, thresh0, thresh1;// 按格式读取文件内容file >> width >> height >> t_l >> t_w >> scale >> chuan >> thresh0 >> thresh1;// 输出读取的数据std::cout << "Width: " << width << std::endl;std::cout << "Height: " << height << std::endl;std::cout << "t_l: " << t_l << std::endl;std::cout << "t_w: " << t_w << std::endl;std::cout << "Scale: " << scale << std::endl;std::cout << "Chuan: " << chuan << std::endl;std::cout << "Thresh0: " << thresh0 << std::endl;std::cout << "Thresh1: " << thresh1 << std::endl;// 关闭文件file.close();return 0;
}
5. 二进制文件读取
ifstream
也可以用来读取二进制文件。二进制文件读取需要使用 std::ios::binary
模式。
std::ifstream file("binaryfile.bin", std::ios::binary);
if (file.is_open()) {// 读取二进制数据char buffer[100];file.read(buffer, sizeof(buffer)); // 读取100字节的数据file.close();
}
在读取二进制文件时,不能使用 >>
操作符,因为 >>
只适用于文本格式的文件,而二进制文件不包含换行符和空格。
6. 文件读取中的异常
如果文件读取过程中出现异常,可以使用 try-catch
语句来捕获异常并处理。
try {std::ifstream file("filename.txt");if (!file.is_open()) {throw std::runtime_error("File could not be opened");}// 继续文件读取操作
} catch (const std::exception &e) {std::cerr << "Error: " << e.what() << std::endl;
}
7. 总结
C++ 提供了丰富的文件读取操作工具,主要通过 ifstream
类来读取文件内容。文件读取可以按照字符、行或格式进行,同时支持文本文件和二进制文件的读取。文件操作过程中,需要注意打开文件、判断是否成功、读取数据以及关闭文件等步骤。同时,文件读取也可能发生错误,需要适当处理异常和错误信息。