2013-03-13 51 views
0

我做了以下程序。粗体部分存在错误。我得到的输出中的计数值为零。编译代码时没有错误。经典C++中的数据文件处理(很像C)

#include<iostream.h> 
#include<conio.h> 
#include<fstream.h> 
void main() 
{ 
    clrscr(); 
    void count(); 
    fstream file("STORY.TXT",ios::in|ios::out); 
    file<<"He is playing in the ground. She\nis playinbg with her dolls.\n"; 
    file.close(); 
    count(); 
    getch(); 
} 
void count() 
{ 
    ifstream file("STORY.TXT"); 
    file.seekg(0);int count=0; 
    while(!file.eof()) 
    { 
     char line[10]; 
     **file.get(line,10,' '); 
     cout<<line<<"\n"; 
     if(line=="HE") 
      ++count;** 
    } 
    cout<<count; 
    file.close(); 
} 
+0

你'EOF()'while循环可能是错的,看到[这里](http://stackoverflow.com/questions/5605125/why -is-iostreameof-内,一个循环条件考虑的,是错误的)。 – juanchopanza 2013-03-13 08:00:28

回答

3

字符串比较不通过==完成。这只是比较地址 替换

if(line=="HE") 

if(!strcmp(line, "HE")) 

编辑

对于不区分大小写

if(!strcmpi(line, "HE")) 
+0

它也是区分大小写的。所以你的计数仍然是0。 – 2013-03-13 07:06:52

+1

您希望'strcasecmp'进行不区分大小写的比较。 – 2013-03-13 07:07:58

+0

哦yea..case sensitive.should我使用getline或获取函数来获取字符串。是否得到函数保留''字符和getline放弃它? – Smatik 2013-03-13 07:10:16