在C++中,可以使用标准库函数来判断文件结尾。以下是一个示例代码:
#include <iostream>
#include <fstream>
#include <string>
int main() {
std::ifstream file("example.txt");
if (!file.is_open()) {
std::cerr << "Could not open file." << std::endl;
return 1;
}
file.seekg(0, std::ios::end);
std::streampos end = file.tellg();
if (end == 0) {
std::cout << "File is empty." << std::endl;
} else {
file.seekg(-1, std::ios::end);
char c;
file.get(c);
if (c == '\n') {
std::cout << "File ends with a newline character." << std::endl;
} else {
std::cout << "File does not end with a newline character." << std::endl;
}
}
file.close();
return 0;
}
在这个示例中,我们首先打开一个文件并获取文件的大小。然后我们移动文件指针到文件末尾,并读取最后一个字符。最后我们判断文件是否以换行符结尾。
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,请发送邮件至 55@qq.com 举报,一经查实,本站将立刻删除。转转请注明出处:https://www.szhjjp.com/n/993811.html