文件写如的疑问
当写如时候,字符可以正常显示,但是数字却是乱码,
请教哈
是不是和写入方式有关呢?(wb)
代码如下:
#include<stdio.h>
#include<stdlib.h>
#define WLM printf("*******End of the program!******")
#define SIZE 4
struct student_type
{
char name[10];
int num;
int age;
char addr[20];
} stud[SIZE];
//define the struct
void save(char *pfn)
{
FILE *fp;
int i;
char *pf=pfn;
if((fp=fopen(pf,"wb"))==NULL)
{
printf("Can't open the file!\n");
return; //exit(0);
}
for(i=0;i<SIZE;i++)
{
if(fwrite(&stud[i],sizeof(struct student_type),1,fp)!=1)
{
printf("Write Error!\n");
}
}
fclose(fp);//Can't forget!
}//End of The Save()
main()
{
int i;
char filename[20];
char *pfn=filename;
printf("Please input the file name:");
scanf("%s",filename);
printf("Please input the Students' contents:\n");
for(i=0;i<SIZE;i++)
{
scanf("%s%d%d%s",stud[i].name,&stud[i].num,&stud[i].age,stud[i].addr);
save(pfn);
}
WLM;
system("pause");
}