请教c语言下程序出错 0xc00000005(附代码)

fyyk1211 2009-01-02 01:30:44
目前小弟觉得应该是fread的错误,请指教了




#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include<conio.h>

#define P12345 printf("%-5s%-8s%-20s%-8s%-8s%-12s%-10s\n","Num","Name","dept","duty","wage","mark","tot") ;
#define P56789 printf("%-5s%-8s%-20s%-8.2lf%-12s%-8s\n",info.Num,info.Name,temp1,info.wage,info.mark,info.tot);




enum Dept{ NG, logistics, Administration, humanresources, Financial}; /*部门 */




struct Info
{
char Num[5]; /* 序号 */
char Name[8]; /* 姓名 */
enum Dept dept; /* 部门 */
float wage; /* 工资 */
float price; /*奖金*/
char mark[12]; /* 备注 */
float tot; /* 总和 */
};
void menu();



int load() /*统计参数所指定的文件中的记录的个数的函数*/
{
struct Info info;
FILE *fp;
int i;
if((fp=fopen("data.dat","rb"))==NULL)
{
printf("\nCannot open file\n");
getchar();
return 0;
}

for(i=0;!feof(fp);i++)
fread(&info,sizeof(info),1,fp);
fclose(fp);
return(i-1); /*返回记录个数*/

}
void display() /* 职工信息显示函数 */
{
int amount; /* 信息文件中的记录总数 */
struct Info info;
FILE *fp1;
int total = 0;
char temp[10];
char temp1[10];
amount=load("inform.txt");
if((fp1=fopen("inform.txt","r+")) == NULL)
{
printf("\tCan not open the inform file!");
getchar();
exit(1);
}

P12345;

for(;total<amount;)
{
if(fread(&info, sizeof(info), 1, fp1) != 1)
{
printf("\nfile read wrong!\n");
getchar();
exit(1);
}



total++;

if(info.dept==Financial) strcpy(temp1,"Financial");
else if(info.dept==humanresources) strcpy(temp1,"humanresources");
else if(info.dept==Administration) strcpy(temp1,"Administration");
else if(info.dept==logistics) strcpy(temp1,"logistics");
else strcpy(temp1,"NG");
P56789;

if((total != 0) && (total%10 ==0))
{
printf("\n\n\tPress any key to continue......");
getchar();
puts("\n\n");
P12345;
}

}
fclose(fp1);
printf("\n\n\tThere are %d record in all!",total);
getchar();



}
main()
{
int amoment;
amoment=load();
printf("%d",amoment);
getchar();
display();
getchar();
}
...全文
729 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
qrlvls 2009-01-04
  • 打赏
  • 举报
回复
F5调试解决,看调用堆栈窗口的记录
fandh 2009-01-04
  • 打赏
  • 举报
回复
0xc00000005 内存非法访问
我怀疑是你读文件那儿出错!建议你单步调试看看!
wutaihua 2009-01-02
  • 打赏
  • 举报
回复
楼主你单步跟一下,这样循环读取的文件,看代码除非是比较明显的,不然比较难解决,你可以看看是一开始数据读出来就不对(这有可能是结构体字节对齐的问题),还是说你最后的时候不对,循环越界了,从代码上看,我们都没有你的文件,也不好调试。多加点输出信息,跟踪一下
wocow3 2009-01-02
  • 打赏
  • 举报
回复
0xc00000005 内存非法访问

检查哪儿是不是越界了
wiowei 2009-01-02
  • 打赏
  • 举报
回复
fread(&info,sizeof(info),1,fp);
sizeof(info)是分配的结构体存储空间大小,不是你的数据大小
fyyk1211 2009-01-02
  • 打赏
  • 举报
回复
其实这个错误我已经发现了。。

刚才打算回复结果没出来。。。

改了还是不能用啊,但是十分感谢了。
wutaihua 2009-01-02
  • 打赏
  • 举报
回复
load函数是没有参数的

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <conio.h>

#define P12345 printf("%-5s%-8s%-20s%-8s%-8s%-12s%-10s\n","Num","Name","dept","duty","wage","mark","tot") ;
#define P56789 printf("%-5s%-8s%-20s%-8.2lf%-12s%-8s\n",info.Num,info.Name,temp1,info.wage,info.mark,info.tot);




enum Dept{ NG, logistics, Administration, humanresources, Financial}; /*部门 */



struct Info
{
char Num[5]; /* 序号 */
char Name[8]; /* 姓名 */
enum Dept dept; /* 部门 */
float wage; /* 工资 */
float price; /*奖金*/
char mark[12]; /* 备注 */
float tot; /* 总和 */
};
void menu();



int load() /*统计参数所指定的文件中的记录的个数的函数*/
{
struct Info info;
FILE *fp;
int i;
if((fp=fopen("data.dat","rb"))==NULL)
{
printf("\nCannot open file\n");
getchar();
return 0;
}

for(i=0;!feof(fp);i++)
fread(&info,sizeof(info),1,fp);
fclose(fp);
return(i-1); /*返回记录个数*/

}
void display() /* 职工信息显示函数 */
{
int amount; /* 信息文件中的记录总数 */
struct Info info;
FILE *fp1;
int total = 0;
char temp[10];
char temp1[10];
amount=load("inform.txt"); //首先说,这个地方就有错误了,你申请的load函数是没有参数的,但是你调用的时候却用了参数。
if((fp1=fopen("inform.txt","r+")) == NULL)
{
printf("\tCan not open the inform file!");
getchar();
exit(1);
}

P12345;

for(;total <amount;)
{
if(fread(&info, sizeof(info), 1, fp1) != 1)
{
printf("\nfile read wrong!\n");
getchar();
exit(1);
}

total++;

if(info.dept==Financial) strcpy(temp1,"Financial");
else if(info.dept==humanresources) strcpy(temp1,"humanresources");
else if(info.dept==Administration) strcpy(temp1,"Administration");
else if(info.dept==logistics) strcpy(temp1,"logistics");
else strcpy(temp1,"NG");
P56789;

if((total != 0) && (total%10 ==0))
{
printf("\n\n\tPress any key to continue......");
getchar();
puts("\n\n");
P12345;
}

}
fclose(fp1);
printf("\n\n\tThere are %d record in all!",total);
getchar();



}
main()
{
int amoment;
amoment=load();
printf("%d",amoment);
getchar();
display();
getchar();
}

4,012

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC 数据库
社区管理员
  • 数据库
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧