问题描述
- C语言运行总是出问题。。。。
-
#include
#include
#include
#include
//将一块内存数据写入文件中 使用的函数为:fwrite;
#define NUM 2
typedef struct
{
char name[20];
int age;
char email[50];
}person; //定义结构体 结构体名为person;void getinfor(person* pinfor)
{
printf("input name: ");
gets(pinfor->name);
printf("input age: ");
scanf("%d",&pinfor->age);
//getchar();
//printf("input email ");
//gets("pinfor->email");
}void main(void)
{person people[NUM]; //声明结构体数组,结构体名称为 person 数组名为 people;
//声明文件,要写入的文件名;
char filename[50];
printf("please input the file name that you want to save date : n");
gets(filename);
FILE* fpo = NULL;
fpo = fopen(filename,"wt");
if (fpo == NULL)
{
printf("the file open fail,check again please!n");
exit(0);
}
else
printf("the file open success!n");
for (int i=0;i<NUM;i++)
{
getinfor(&people[i]);
if (fwrite(&people[i],sizeof(person),1,fpo) == 0)
{
printf("fwrite in failn");
exit(0);
}
else
printf("write in success!n");
}
fclose(fpo);
printf("The file has been closedn");
}各位大牛帮帮忙呀 谢谢大家~!
解决方案
问题很简单,我只说一句话,不懂就说明你对内存理解不行:
在一个被调函数结束的时候不可以返回栈空间或者是局部动态内存。
不理解再问吧!