经典c++中的数据文件处理(很像c)

Data File handling in classic c++ (much like C)

本文关键字:很像 处理 文件 c++ 数据 经典      更新时间:2023-10-16

我编写了以下程序。粗体部分有个错误。我得到的输出中count的值是0。当我编译代码时,没有错误。

#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. Shenis 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"))
相关文章: