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

标签 c++ file file-io

我制作了以下程序。粗体部分有错误。我得到的输出中的计数值为零。我编译代码时没有错误。

#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/

相关文章:

java - 从 REST Controller 返回的 InputStreamResource 中未释放的 InputStream

c++ - Visual Studio 2010 不重建/重新编译

linux - 运行 'cat' 可以加快 Linux 机器上后续文件随机访问的速度吗?

java - 如何获取文件目录的绝对路径?

linux - 在 Linux 中以编程方式创建的文件变为 NULL

linux - 文件夹中最近的文件

C#多线程文件IO(阅读)

c++ - 重新创建一个基于 shared_ptr 的单例

c++ - 难以理解 std::transform 的行为

c++ - 虚拟或类型转换