2017-03-08 47 views
-2
#include <iostream> 
#include <fstream> 
//#include <cstring> 
//#include <string> 

using namespace std; 

int main() 
{ 
    string word; 
    ifstream infile; 
    infile.open ("inputfile.txt"); 

    if (infile.fail()) 
     { 
      cout<<"UNABLE TO ACCESS INPUT FILE"; 
     } 

    while(!infile.eof()) 
     { 
      while (infile>> word) 
       { 
        cout<<word<<endl<<endl; 
       } 
     } 

    infile.close(); 

    system("pause"); 

    return 0; 
} 

上面的代码COUTS在输入文本文件中的所有词语的我如何只选择一个字?我问这个,因为我希望最终能够从用户CIN一个字,并找到在输入文件或者删除或者用另一个词替换这个词。如何从包含多个句子的输入文件中查找单个单词。 C++

+0

你可以使用if语句来检查什么字呢打印出来 – Gab

+2

首先请阅读前[为什么的iostream算错了一个循环条件中:: EOF ?](http://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-considered-wrong)。然后想想你最内层循环做什么(不需要外部,并且是错了,因为你现在已经阅读)。 –

回答

1

我这里是从字符串找字

std::string str ("There are two needles in this haystack with needles."); 
std::string str2 ("needle"); 

std::size_t found = str.find(str2); 
if (found!=std::string::npos) 
std::cout << "first 'needle' found at: " << found << '\n'; 
+0

如果它为你工作,请upvote我的评论:) –

相关问题