读取行的一部分(getline()) [英] Reading parts of a line (getline())

查看:151
本文介绍了读取行的一部分(getline())的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

基本上,这个程序搜索一个.txt文件中的一个单词,如果它找到它,它打印行和行号。这是我到目前为止。

Basically this program searches a .txt file for a word and if it finds it, it prints the line and the line number. Here is what I have so far.

代码:

#include "std_lib_facilities.h"

int main()
{
    string findword;
    cout << "Enter word to search for.\n";
    cin >> findword;

    char filename[20];
    cout << "Enter file to search in.\n";
    cin >> filename;
    ifstream ist(filename);

    string line;
    string word;
    int linecounter = 1;
    while(getline(ist, line))
    {
     if(line.find(findword) != string::npos){
             cout << line << " " << linecounter << endl;}
     ++linecounter;
     }

     keep_window_open();
}

解决。

推荐答案

您正在寻找 find

You're looking for find:

if (line.find(findword) != string::npos) { ... }

这篇关于读取行的一部分(getline())的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆