文件类型引发错误:非静态成员函数的使用无效
FILE type throwing error: invalid use of non-static member function
我正在尝试将二进制文件读取到从结构的特定成员开始的结构中。但是,当我尝试执行此操作时,出现以下错误:
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*)ðernetPacket.ep_eh, sizeof(eh));
return ethernetPacket;
}
但这不会在文件中正确读取。
您正在使用 nameOfFile.c_str
,它是字符串类的成员函数。
将其更改为nameOfFile.c_str()
相关文章:
- 为什么将值返回函数传递给重载=运算符对运算符函数有效,而对其他运算符无效
- 程序中的布尔函数返回输入的范围无论如何都是无效的
- 在这个函数中是有缺陷的,因为取消引用 null 是无效的,所以我想更改代码
- 在 Microsoft Access SQL 中调用自定义 DLL 函数时传递的内存地址无效
- C++无效的函数类型转换
- C++无效使用非静态成员函数?
- 模板和无效函数调用C++
- 错误: 无效使用非静态数据成员"应用程序::应用程序构造函数"
- 无效打印(矢量<string>)函数未打印
- 在 stream_descriptor::async_wait 中无效使用非静态成员函数
- C26486 - 不向函数传递可能无效的指针?
- 为什么构造函数的虚拟函数调用有时有效,但其他调用却无效
- 调用boost v1.58中的函数时出现模板名称使用无效错误
- 使用 vector.sort() 的非静态成员函数无效使用
- 将数组传递给构造函数无效
- 使用auto参数munmap_chunk()返回auto的函数:无效指针
- Boost::函数绑定的成员函数无效
- 为什么在模板类中声明函数无效?
- 使用非英语文件名调用avio_open函数无效
- Android NDK线程使用非静态成员函数无效