内存问题
程序代码:
#include <stdio.h> #include <stdlib.h> #define PROJNUM 2 #define _ , #define INFO(a) printf("info:");printf(a); #define INFOLINEF(a) printf("info:");printf(a);printf("\n"); #define ERROR(a) printf("info:");printf(a); #define ERRORLINEF(a) printf("info:");printf(a);printf("\n"); struct Date { unsigned int year; unsigned int month; unsigned int day; }; struct Project { char ProjName[32]; char ProjManager[32]; float ProjIncome; struct Date ProjStar; struct Date ProjEnd; float projOutcome; }; void ProjectSave(struct Project *ProjAddr,FILE *fp_in); void ProjectPrint(FILE *fp_out); void main() { int i; struct Project *pProj=NULL,*pProjCopy=NULL; FILE*fp; fp=fopen("D:\\C_language\\ProjectManagement.txt","wb+"); if(NULL==fp) { ERRORLINEF("文件打开失败!!"); return; } else { INFOLINEF("成功,文件打开完成。"); } pProj=(struct Project*)malloc(PROJNUM*sizeof(struct Project)); pProjCopy=pProj; INFOLINEF("-----------------------------------------"); for(i=0;i<PROJNUM;i++,pProj++) { INFO("输入项目名称:"); scanf("%s",pProj->ProjName); INFO("输入项目管理员名:"); scanf("%s",pProj->ProjManager); INFO("输入项目收益:"); scanf("%f",pProj->ProjIncome); INFO("输入项目开始日期:"); scanf("%u %u %u",pProj->ProjStar.year,pProj->ProjStar.month,pProj->ProjStar.day); INFO("输入项目结束日期:"); scanf("%u %u %u",pProj->ProjEnd.year,pProj->ProjEnd.month,pProj->ProjEnd.day); INFO("项目开销:"); scanf("%f",pProj->projOutcome); } ProjectSave(pProjCopy,fp); INFOLINEF("----------------------------------------"); rewind(fp); ProjectPrint(fp); rewind(fp); i=fclose(fp); if(0==i) { INFOLINEF("文件关闭时成功"); } else { ERRORLINEF("文件关闭失败"); } free(pProjCopy); INFOLINEF("----------------------------------------"); } void ProjectSave(struct Project *ProjAddr,FILE *fp_in) { int i=0; i=fwrite(ProjAddr,PROJNUM*sizeof(struct Project),1,fp_in); if(0==i) { ERRORLINEF("文件写入失败"); } else { INFOLINEF("文件写入成功"); } rewind(fp_in); } void ProjectPrint(FILE *fp_out) { int i=0; struct Project OutProject[2]; fread(OutProject,PROJNUM*sizeof(struct Project),1,fp_out); for(i=0;i<PROJNUM;i++) { INFOLINEF("项目名称:%s",OutProject[i].ProjNam); INFOLINEF("项目负责人:%s",OutProject[i].ProMananger); INFOLINEF("项目收入:%f",OutProject[i].InCome); INFOLINEF("项目开始时间:%d年%d月%d日",outProject[i].projStart.year,outProject[i].projStart.month,outProject[i].projStart.day); INFOLINEF("项目结束时间:%d年%d月%d日",outProject[i].projEnd.year,outProject[i].projEnd.month,outProject[i].projEnd.day); INFOLINEF("项目支出:%f",OutProject[i].OutCome); } }