我制作了以下程序。粗体部分有错误。我得到的输出中的计数值为零。我编译代码时没有错误。
#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();
}
最佳答案
字符串比较不是通过==
来完成的。那只是比较地址
替换
if(line=="HE")
与
if(!strcmp(line, "HE"))
编辑
不区分大小写
if(!strcmpi(line, "HE"))
关于c++ - 经典 C++ 中的数据文件处理(很像 C),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15378904/