C++需要使用 .good 而不是 .eof
C++ need to use .good instead of .eof
我可能的重复项:
为什么循环条件中的 iostream::eof 被认为是错误的?
是一个完全的初学者,我有这个功能,我在其中使用 .eof,但我被告知这不是一个好主意,因为这不适用于 linux,我应该使用 .good所以你能告诉我我会做什么以及我应该如何修改它吗
?std::string ex_file_licensing::getUsedLicense(const std::string &FlexLMfileName){
std::ofstream openFile;
std::string line;
std::string tempString;
std::string testResult;
size_t start_pos;
size_t stop_pos;
std::string retour ="";
filebuf fb;
fb.open (FlexLMfileName.c_str(),ios::in);
istream toOpen(&fb);
if(toOpen.eof()){
while(!toOpen.eof()){
getline(toOpen,line);
if(line.find("Checkout") != std::string::npos ){
start_pos = line.find(":");
tempString = line.substr(start_pos+1);
stop_pos = tempString.find("/");
testResult = tempString.substr(start_pos, stop_pos);
}
else if (line.find("FLEXnet") != std::string::npos ){
start_pos = line.find("FLEXnet");
tempString = line.substr(start_pos+1);
stop_pos = tempString.find("Feature");
testResult = tempString.substr(start_pos, stop_pos);
}
cout << testResult << endl;
retour.append(testResult);
}
fb.close();
}
return retour;
}
使用这样的循环:
while (getline(toOpen, line)) {
...
}
两者都不要使用:流有一个内置的布尔转换运算符,因此你可以简单地用if (toOpen)
或while(toOpen)
来测试它,它就可以工作了。
顺便说一下,您的if
条件看起来是错误的,因为它仅在文件流达到 EOF 字符时才执行其中的代码。
相关文章:
- 如何确定我已使用非编码文件到达 EOF?
- std::ifstream::read 不会读取所有 512 字节,并设置 EOF 和失败位
- 而(!inputfile.eof())只读取第一行?
- EOF有更安全的替代方案吗?它在我的情况下不起作用
- "错误 C0000:语法错误,令牌"<EOF>"处出现意外$end,并且不确定
- 如何在 EOF 之前从文件中读取并将其放入字符串中?
- 如何在从文件中读取整数时使用 file.eof()?
- Is it good to use SDL_PIXELFORMAT_UNKNOWN?
- "eof"可以设置为流吗?
- 吃完 EOF 后重复使用 std::cin
- 文件处理,eof() 定义
- std::cin 在读取 EOF 并清除后不再读取
- 为什么 EOF 在循环扫描期间没有终止?
- cin.clear()是否保留EOF
- 而(getline(fin,str)){}即使在到达eof之后仍在处理数据
- 通过终端在文件中输入时检测EOF(文件结束)时出现问题
- 为什么如果我从键盘输入EOF克里昂不要在运行窗口打印程序的输出?
- 为什么"cin"在EOF之后不再wοrk?
- 在C++流中,eof(),fail(),bad()和good()有什么区别
- C++需要使用 .good 而不是 .eof