关于fread
假如a.txt里的内容如下:
12343109TP4562012-1-1
4565310703.652012-1-5
下面的代码会输出一些乱码,求指点!
#include<cstdlib>#include<iostream>using namespace std;struct Stu{ char ZH[15]; char XH[15]; char SY[15]; char SJ[15]; };typedef struct Node{ Stu stu; struct Node *next;}*No;int main(){ FILE *in; if((in=fopen("C:\\Users\\Jack\\Desktop\\a.txt","r"))==NULL){ printf("cannot open this file\n"); system("pause"); exit(0); } No p=(Node*)malloc(sizeof(Node)); p->next=NULL; No ip=p; while(!feof(in)){ No temp=(No)malloc(sizeof(Node)); fread(&(temp->stu),sizeof(Stu),1,in); temp->next=NULL; ip->next=temp; ip=temp; } fclose(in); for(ip=p->next;ip;ip=ip->next){ cout<<ip->stu.ZH; cout<<ip->stu.XH; cout<<ip->stu.SY; cout<<ip->stu.SJ; } system("pause"); return 0;}