文件类型引发错误:非静态成员函数的使用无效

FILE type throwing error: invalid use of non-static member function

本文关键字:函数 无效 静态成员 类型 错误 文件      更新时间:2023-10-16

我正在尝试将二进制文件读取到从结构的特定成员开始的结构中。但是,当我尝试执行此操作时,出现以下错误:

error: invalid use of non-static member function

我想知道为什么会这样?

typedef struct  eh {    /* ethernet header          */
    unsigned char eh_dst[6];
    unsigned char eh_src[6];
    unsigned short eh_type;
} ethHead;
typedef struct  ep  {        /* complete structure of Ethernet packet*/
    short   ep_ifn;      /* originating interface number        */
    short   ep_len;      /* length of the packet            */
    struct eh ep_eh;     /* the ethernet header         */
    char ep_data[1500];
    unsigned long ep_crc;
} ethFrame;
ep epReadFromFile(string nameOfFile){
    FILE *fp = fopen(nameOfFile.c_str, "rb");
    ep EPH;
    if (fp != 0) {
        fread(&EPH.ep_eh, sizeof(ep), 1, fp);
        printf("reading in File");
    }
    else {
        printf("Can't read file");
    }
    fclose(fp);
    return EPH;
}

此外,如果有更好的方法来使用 fstream 执行此操作,我将非常感谢您的建议。

我已经尝试使用标准库来执行此操作:

ep epReadFromFile(string nameOfFile){
    ifstream input_file(nameOfFile, ios::binary);
    ep ethernetPacket;
    input_file.read((char*)&ethernetPacket.ep_eh, sizeof(eh));
    return ethernetPacket;
}

但这不会在文件中正确读取。

您正在使用 nameOfFile.c_str ,它是字符串类的成员函数。

将其更改为nameOfFile.c_str()