You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

C++文件操作代码Dev-C++可运行,VS Code编译报错求助

C++文件读取编译报错问题

我是编程新手,写的C文件操作代码在Dev-C里能正常运行输出预期结果,但在VS Code里编译报错,只有读取文件有问题,写入功能正常。

代码示例

#include <iostream>
#include <fstream>
#include <string.h>

using namespace std;
class student{
    private:
        char name[25];
        int id;
        int age;
    public:
        void get(){
            cin>>name>>id>>age;
        }
        void show(){
            cout<<name<<id<<age;
        }
        void write2file(){
            ofstream outfile("student.dat",ios::binary|ios::app);
            get();
            outfile.write(reinterpret_cast<char*>(this),sizeof(*this));
        }
        void readfromfile(){
            ifstream infile("student.dat",ios::binary|ios::in);
            while(!infile.eof()){
                if(infile.read(reinterpret_cast<char*>(this),sizeof(*this))>0){
                    show();
                }
            }
        }
};
int main(){
    student s;
    s.write2file();
    s.readfromfile();
    return 0;
}

编译报错信息

awd.cpp: In member function 'void student::readfromfile()':
awd.cpp:26:76: error: no match for 'operator>' (operand types are 'std::basic_istream<char>::__istream_type' {aka 'std::basic_istream<char>'} and 'int')        
   26 |                 if(infile.read(reinterpret_cast<char*>(this),sizeof(*this))>0){
      |                    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^~
      |                               |                                             |
      |                               |                                             int
      |                               std::basic_istream<char>::__istream_type {aka 'std::basic_istream<char>'}
awd.cpp:26:76: note: candidate: 'operator>(int, int)' (built-in)
   26 |                 if(infile.read(reinterpret_cast<char*>(this),sizeof(*this))>0){
      |                    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^~
awd.cpp:26:76: note:   no known conversion for argument 1 from 'std::basic_istream<char>::__istream_type' {aka 'std::basic_istream<char>'} to 'int'
In file included from C:/msys64/mingw64/include/c++/12.1.0/string:47,
                 from C:/msys64/mingw64/include/c++/12.1.0/bits/locale_classes.h:40,
                 from C:/msys64/mingw64/include/c++/12.1.0/bits/ios_base.h:41,
                 from C:/msys64/mingw64/include/c++/12.1.0/ios:42,
                 from C:/msys64/mingw64/include/c++/12.1.0/ostream:38,
                 from C:/msys64/mingw64/include/c++/12.1.0/iostream:39,
                 from awd.cpp:1:

问题原因与修复

报错核心是infile.read()返回的是流对象本身,并非读取的字节数,不能直接和整数0比较。Dev-C使用的旧版MinGW编译器允许这种非标准写法,但VS Code里的新版编译器(如GCC)严格遵循C标准,因此触发报错。同时原代码用while(!infile.eof())的写法存在逻辑漏洞,会导致最后一次读取失败后仍执行show()。

修复后的读取函数

void readfromfile(){
    ifstream infile("student.dat",ios::binary|ios::in);
    while(infile.read(reinterpret_cast<char*>(this),sizeof(*this))){
        show();
    }
}

说明

  • 直接用read()作为循环条件,读取成功时继续执行,失败(如到达文件末尾)则退出循环,这是C++标准的正确写法。
  • 避免使用eof()判断循环,因为流的EOF标志只有在尝试读取超出文件末尾时才会被设置,容易出现多执行一次的错误。

内容的提问来源于stack exchange,提问作者sorry myan

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.24 16:24:21