严重性 代码 说明 项目 文件 行 禁止显示状态
错误 C2678 二进制“!=”: 没有找到接受“std::ifstream”类型的左操作数的运算符(或没有可接受的转换)
首先是输入代码:
struct student
{
char strname[20];
int grade;
};
int main()
{
ofstream out;
out.open("d:\\a.txt");
student stu1 = { "张三",90 };
student stu2 = { "李四",80 };
out.write((const char*)&stu1, sizeof(student));
out.write((const char*)&stu2, sizeof(student));
cout << stu1.strname << "\t" << stu1.grade << endl;
cout << stu2.strname << "\t" << stu2.grade << endl;
out.close();
return 0;
}
手动打开a.txt文件,内容是:张三 Z 李四 P,
读取代码:
int main()
{
ifstream fin("d:\\a.txt");
if (fin != NULL)
{
cout << fin.rdbuf();
}
fin.close();
}
在!=位置报错,但是程序可以运行,运行结果和输入相同,显示的是:
张三 90
李四 80
点了确定后,结果才出现
代码修改:
int main()
{
ifstream fin("d:\\a.txt");
if (!fin) return 0;
else
{
cout << fin.rdbuf();
}
fin.close();
}
修改后运行不报错,但是输出结果错误了:
不是输入的值,而是文件内容。
if (!fin)
->
if (!fin.eof())