文件为乱码输出
程序代码:
#include <fstream> #include <iostream> using namespace std; struct student { char name[20]; int num; int age; char sex; }; int main() { student stud[3]={"li",1001,18,'f',"fun",1002,19,'m',"wang",1003,17,'f'}; ofstream outfile("example.txt",ios::binary); if(!outfile) { cout<<"open error!"<<endl; return 1; } for(int i=0;i<3;i++) outfile.write((char *)&stud[i],sizeof(stud[i])); outfile.close(); return 0; }
用的VC,与编译器有没有关系?