C++ string::find未找到匹配项

C++ string::find未找到匹配项,c++,string,C++,String,我正在尝试使用string::find方法来确定.txt文件的一行中是否存在字符串“hello”(前面和后面有空格)。如果有,它应该打印出行号(位置不重要)。问题是,它找不到字符串。请帮忙 int main() { string key (" hello "); ifstream myReadFile; myReadFile.open("test.txt"); string s; int lineCount = 1; int found;

我正在尝试使用string::find方法来确定.txt文件的一行中是否存在字符串“hello”(前面和后面有空格)。如果有,它应该打印出行号(位置不重要)。问题是,它找不到字符串。请帮忙

int main() {
    string key (" hello ");
    ifstream myReadFile;
    myReadFile.open("test.txt");
    string s;
    int lineCount = 1;
    int found;
    if (myReadFile.is_open()) {
        while (!myReadFile.eof()) {
            getline(myReadFile, s);
            found = s.find(key);
            if(found != string::npos) {
                cout<<lineCount<<endl;
                lineCount++;
            }
         }
    }
    myReadFile.close();
    return 0;
}
intmain(){
字符串键(“hello”);
ifstreammyreadfile;
myReadFile.open(“test.txt”);
字符串s;
int lineCount=1;
int-found;
如果(myReadFile.is_open()){
而(!myReadFile.eof()){
getline(myReadFile,s);
找到=s.find(键);
如果(找到!=字符串::npos){

cout
int found
应该是
string::size\u type
。这可能是您的问题,因为int是有符号的,而size\u t是无符号的。有关详细信息,请参阅

npos是静态成员常量值 以最大可能的价值 类型为size\u t的元素

编辑:


感谢Martin的评论,我将
size\u t
替换为
string::size\u type

它看起来像是在计算包含该字符串的行数。在循环的每次迭代中,都应该增加行号var,而不仅仅是在找到字符串时

int main() {
    std::string key (" hello ");
    ifstream myReadFile;
    myReadFile.open("test.txt");


    if (myReadFile) {

        std::string line;
        int line_number = 0;
        while (std::getline(myReadFile, line)) {
            line_number++;                
            if (line.find(key) != std::string::npos)
                std::cout << line_number << std::endl;
        }

    } else {
        std::cout << "Error opening file\n";
    }
}
intmain(){
字符串键(“hello”);
ifstreammyreadfile;
myReadFile.open(“test.txt”);
if(myReadFile){
std::字符串行;
int line_number=0;
while(std::getline(myReadFile,line)){
行数++;
if(line.find(key)!=std::string::npos)

std::cout如果您看到的问题是您的程序总是打印1、2、3…而不是正确的行号,这是因为如果找到子字符串,您只会增加
lineCount
;要修复它,请将
lineCount++
移动到
If(found!=string::npos)
块之后

如果您根本看不到任何输出,则可能是文件不包含
“hello”
(大小写很重要,而且这些空格字符与其他空格不匹配)或“test.txt”位置不正确或名称错误


注意:
found
string::npos
之间的比较在这里没有问题(即使一个是有符号的
int
,另一个是
size\u t
(在64位系统上可能是
无符号的int
或可能是
无符号的long
)。有趣的是,如果将
found
更改为
unsigned int
,而
size\u t
恰好是一种更宽的无符号类型,则它将中断(在32位计算机上,您可以通过将
found
设置为
unsigned short
来模拟这种情况)。由于您实际上没有使用
found
,因此最好完全避免转换,如果(s.find(key)!=string::npos)
,则只需执行

在调试器中运行它,并查看其错误所在。注意:
string::find()
不会返回
int
。请使用std::string::size\u type。马丁:虽然我也喜欢这种结构,但OP的程序实际上不会错误处理最后一行;
getline
会清除字符串,如果您尝试读取最后一行之后的一行。因此,在循环中始终有一个额外的运行
s
空,但这不会导致输出错误。@马丁:正确,但在尝试读取之前,流不会处于错误状态(这是while循环的条件)。换句话说,在读取最后一行后,流仍然良好(因为没有读取失败)。因此循环再次运行,getline清除字符串,然后查看EOF。这与使用
>
(这是通常导致错误的原因)不同因为它在清除字符串之前跳过了空格,而流在这一部分中可能会坏掉。@Sumudu:是的。删除注释。啊,谢谢,我之前就这样做了,我一定是修复了另一个问题,忘了更改它。根据我的标准NPO副本(和find)具有std::string::size类型_type@Martin:这很奇怪,我一直使用它作为size\t大多数实现使用size\t作为std::string::size\u type的类型(即字符串内部有
typedef std::size\t size\u type
)但从技术上讲,它们是不同的。@Martin:啊,好吧,我明白你现在所说的,很高兴知道,谢谢你没有看到
if(myReadFile)