小编Dar*_*us 的帖子

如何在文本文件中搜索单词,如果找到则打印出整行

我的程序需要在文本文件中搜索单词,如果找到该单词,则打印/显示整行.例:

employee   name    date joined    position       project        annual salary
tom jones           1/13/2011     accountant    pricing         55000
Susan lee           2/5/2007      Manager       policy          70000

用户输入搜索词:

会计

程序搜索文本accountant.当它找到它时,它返回以下内容:

employee   name    date joined    position       project        annual salary
tom jones           1/13/2011     accountant    pricing         55000

这是我提出的代码,但它不起作用.

void KeyWord(ifstream &FileSearch)
{
     string letters;
     int position =-1;
     string line;
     ifstream readSearch;
     cout<<"enter search word ";
     cin>>letters;
     "\n";
     FileSearch.open("employee");
     if(FileSearch.is_open())
     { 
         while(getline(FileSearch, line))
         {
             FileSearch>>line;
             cout<<line<<endl;
             position=line.find(letters,position+1);
             if(position==string::npos);
             if(FileSearch.eof())
                 break;

             cout<<line<<endl;
         }

     }
     cout<<"Cant find"<<letters<<endl;
 }
Run Code Online (Sandbox Code Playgroud)

c++ io string-matching

8
推荐指数
1
解决办法
4万
查看次数

标签 统计

c++ ×1

io ×1

string-matching ×1