立即获取线并测试 EOF
getline and testing EOF at once
我想问我的问题,我试图阅读Getline和EOF问题,但没有帮助。
问题是我不知道这里哪里会出错:使用的功能(获取线或检查EOF)是否有问题?
如果文本中没有文本.txt则文件显示发现了某些内容。但我不知道我为什么或在哪里犯了错误......
我想要的是:搜索字符串,如果 txt 文件中没有文本,我希望它说 EOF 或其他东西。它仍然说 - 即使文件为空 - 我正在寻找的字符串在第 1 行中找到,位置 1 - 例如
我在那里放代码:
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int openFile(void);
int closeFile(void);
int getTime(void);
int findTime();
int findDate();
int stringFind(string);
bool getOneLine(void);
string what;
bool ifound = false;
string foundstring;
string filename ;
fstream inputfile;
string sentence ;
size_t found ;
string foundTime ;
string foundDate ;
bool timeIsHere = false;
bool dateIsHere = false;
int iterTime = 0;
int iterDate = 0;
int line = 0;
int main (void){
sentence.clear();
cout << " Enter the file name:" << endl;
openFile();
while (getOneLine() != false) {
stringFind("Time");
}
cout << "END OF PROGRAM" << endl;
system("PAUSE");
///getTime();
closeFile();
system("PAUSE");
}
int closeFile(void) {
inputfile.close();
cout << " File: " << filename << " - was closed...";
return 0;
}
int openFile(void) {
cout << " Insert file name in program directory or full path to desired file you want to edit:"<<endl;
cout << " Do not use path with a space in directory address or filename ! " << endl;
cout<<" ";
getline(cin, filename);
inputfile.open(filename, ios::in);
cout <<" file_state: " << inputfile.fail();
if (inputfile.fail() == 1) {
cout << " - Cannot open your file" << endl;
}
else cout << " - File was openned sucesfully"<< endl;
return 0;
}
int stringFind(string what) {
cout << " I am looking for:" << what << endl;
found = what.find(sentence);
if (found == string::npos) {
cout << " I could not find this string " << endl;
}
else if(found != string::npos){
cout << " substring was found in line: " << line + 1 << " position: " << found + 1 << endl << endl;
ifound = true;
foundstring = sentence;
}
return 0;
}
bool getOneLine(void) {
if (inputfile.eof()) {
cout << "END OF FILE" << endl << endl;
return false;
}
else{
getline(inputfile, sentence);
cout << "next sentence is: "<< sentence << endl;
return true;
}
}
我是新手,我没有人要问 - 个人.我试图编辑 While cycle 和 IF,以确保我没有犯严重的错误,但我不知道。
我尝试了例如示例.txt并且此文件为空。
始终测试读取尝试后输入是否成功!流无法知道您正在尝试执行的操作。它只能报告到目前为止尝试是否成功。所以,你会做类似的事情
if (std::getline(stream, line)) {
// deal with the successful case
}
else {
// deal with the failure case
}
在失败的情况下,您可能希望使用eof()
来确定失败是否是由于到达流的末尾:到达文件末尾并因此设置std::ios_base:eofbit
通常不是错误,而只是指示您已完成。它可能仍然是一个错误,例如,当知道要读取多少行但获得的行较少时。
使用getline()
和EOF
检查的正确方法是这样的:
bool getOneLine(void) {
if (getline(inputfile, sentence)) {
cout << "next sentence is: "<< sentence << endl;
return true;
}
if (inputfile.eof())
cout << "EOF reached" << endl;
else
cout << "Some IO error" << endl;
return false;
}
你这里有一个错误:
found = what.find(sentence);
你正在what
内部寻找sentence
.如果sentence
为空,则会找到它。
将其更改为
found = sentence.find(what);
您应该明确学习如何使用调试器。这样你就会很快发现这样的问题!
相关文章:
- 使用C++库在Android项目中修改gradle中的cmake参数,用于插入指令的测试
- 如何确定我已使用非编码文件到达 EOF?
- 无法编译 rtmidi 测试 cmidiin.cpp 文件, 非法指令
- 尽管测试成功,CppUnit测试核心仍被丢弃.为什么
- 数据成员SFINAE的C++17测试:gcc vs clang
- 如何使用重载的相等(==)运算符向测试用例添加描述
- 为什么二进制搜索在我的测试中不起作用
- 从父数组测试用例构造二叉树失败
- 试图对缓存进行跨步测试,但程序并没有结束
- 有什么好的方法可以让系统调用代理允许在单元测试中进行模拟
- OpenGL在启用深度测试时不会丢弃我的碎片
- 为测试目标创建具有不同源文件夹的文件
- 在子目录中使用target_sources()命令时用于单元测试(qtest)的项目结构
- VC++本机单元测试,找不到调试符号
- 换位表导致测试失败(但在游戏中运行良好)
- 用于交叉编译和CMake的预处理器宏的单元测试
- fgetwc EOF 循环测试失败,但 65535 正常
- fstream 和 eof() 的问题(总共花了 5 个小时进行调试、寻找解决方案和测试)
- ostream << istream 并在空文件上测试 EOF,从 istream 获取字符的替代方法
- 立即获取线并测试 EOF