帮我看看”读取模块“出错在哪里吧

ramen_curator 2018-01-15 05:08:28
调用 读取 函数的时候,直接就程序退出了。
我也不知道怎么办。有劳各位了!
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define LEN sizeof(human)
typedef struct human {
char name[100];
char bumen[100];
char jiguan[100];
char birthday[100];
char xueli[100];
char zhicheng[100];
int gongzi;
char jiangcheng[100];
struct human *next;
}human;
human *creat() {
human *head;
static human dalao = {
"武晓",
"本公司",
"新疆",
"1987.1.1",
"本科在读",
"大老板!",
0,
"小学语文考试第一,奖励全村最好的葡萄干",
NULL
};
head = &dalao;
return (head);
}
void print(human *p) {
printf("\n姓名:%s", p->name);
printf("\n部门:%s", p->bumen);
printf("\n籍贯:%s", p->jiguan);
printf("\n出生年月日:%s", p->birthday);
printf("\n学历/学位:%s", p->xueli);
printf("\n职务/职称:%s", p->zhicheng);
printf("\n工资:%d", p->gongzi);
printf("\n奖惩记录:%s\n", p->jiangcheng);
}
human *scan() {
human *p;
p = (human *)malloc(LEN);
printf("请依次输入 姓名、部门、籍贯、出生年月(1987.1.1)、学历/学位、职务/职称、工资(整数)、奖惩记录。");
printf("\n每个输完请按回车,以输入下一个:\n");
scanf("%s%s%s%s%s%s%d%s", p->name, p->bumen, p->jiguan, p->birthday, p->xueli, p->zhicheng, &p->gongzi, p->jiangcheng);
p->next = NULL;
return (p);
} //输入一个点
human *addone(human *head) {
human *p;
p = scan();
p->next = head->next;
head->next = p;
return head;
}
human * guanliyuan(human *head) {
while (1) {
printf("尊敬的管理员,您好\n");
printf("1.增加一个人员\n");
printf("4.返回上一级\n");
int a;
scanf("%d", &a);
switch (a) {
case 1:head = addone(head); printf("输入成功!\n按回车键以继续..."); break;
case 4:return head;
}
getchar();
getchar();
system("CLS");
}
}
void baocun(human *head);
void denglu(human *head) {
while (1) {
system("CLS");
printf("欢迎来到人事管理系统\n");
printf("1.管理员界面\n3.保存并退出\n请输入数字:");
int i;
scanf("%d", &i);
int miyao = 1653226;//密钥
int shurumiyao;
switch(i) {
case 1:printf("请输入管理员的密钥:");scanf("%d", &shurumiyao);
if (shurumiyao == miyao) {
system("CLS");
printf("欢迎进入管理员界面!\n");
head = guanliyuan(head);
continue;
}
else {printf("输入错误!请重新输入!\n");printf("按回车键继续...");
getchar();getchar();continue;
}break;
case 3:baocun(head);return;break;}}}
void chushihua() {
FILE *fp;
fp = fopen("人事管理数据库.txt", "w");
fclose(fp);}
void chucishiyong() {
printf("此系统初次被使用需要 初始化\n");
printf("如果不是,点击初始化会清空原有数据\n");
printf("\n1.初始化\t2.不初始化\n");
printf("\n请输入数字:");
int i;
scanf("%d", &i);
switch (i) {
case 1: chushihua(); break;
case 2: break;
}
}
human *duqu(human *head) {
printf("此系统如果没有数据,不能使用读取\n");
printf("是否 读取?\n");
printf("1.是\t2.不是\n");
printf("请输入数字:");
int i;
scanf("%d", &i);
if (i == 1) {
FILE *fp;
fp = fopen("人事管理数据库.dat", "rb");
human *p;
while (!feof(fp)) {
p = (human *)malloc(LEN);
fread(p, LEN, 1, fp);
p->next = head->next;
head->next = p;
fclose(fp);
}
}
return head;
}
void baocun(human *head) {
human *p;
p = head;
FILE *fp;
fp = fopen("人事管理数据库.dat", "wb");
p = p->next;
while (p->next!= NULL) {
fwrite(p, LEN, 1, fp);
p = p->next;
}
fclose(fp);
}
int main() {
human *head;
head = creat();
chucishiyong();
head = duqu(head);
denglu(head);
return 0;
}
...全文
601 1 打赏 收藏 转发到动态 举报
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
自信男孩 2018-01-15
  • 打赏
  • 举报
回复
#include<stdio.h>
#include<stdlib.h>
#include<string.h>

#define LEN sizeof(human)

typedef struct human {
    char name[100];
    char bumen[100];
    char jiguan[100];
    char birthday[100];
    char xueli[100];
    char zhicheng[100];
    int gongzi;
    char jiangcheng[100];
    struct human *next;
}human;

void baocun(human *head);

human *creat()
{
    human *head;
    static human dalao = {
        "武晓",
        "本公司",
        "新疆",
        "1987.1.1",
        "本科在读",
        "大老板!",
        0,
        "小学语文考试第一,奖励全村最好的葡萄干",
        NULL
    };
    head = &dalao;
    return (head);
}

void print(human *p)
{
    printf("\n姓名:%s", p->name);
    printf("\n部门:%s", p->bumen);
    printf("\n籍贯:%s", p->jiguan);
    printf("\n出生年月日:%s", p->birthday);
    printf("\n学历/学位:%s", p->xueli);
    printf("\n职务/职称:%s", p->zhicheng);
    printf("\n工资:%d", p->gongzi);
    printf("\n奖惩记录:%s\n", p->jiangcheng);
}

human *scan()
{
    human *p;
    p = (human *)malloc(LEN);
    printf("请依次输入 姓名、部门、籍贯、出生年月(1987.1.1)、学历/学位、职务/职称、工资(整数)、奖惩记录。");
    printf("\n每个输完请按回车,以输入下一个:\n");
    scanf("%s%s%s%s%s%s%d%s", p->name, p->bumen, p->jiguan, p->birthday, p->xueli, p->zhicheng, &p->gongzi, p->jiangcheng);
    p->next = NULL;

    return (p);
}

human *addone(human *head)
{
    human *p;
    p = scan();
    p->next = head->next;
    head->next = p;
    return head;
}

human * guanliyuan(human *head)
{
    int a;
    while (1) {
        printf("尊敬的管理员,您好\n");
        printf("1.增加一个人员\n");
        printf("4.返回上一级\n");
        scanf("%d", &a);
        switch (a) {
            case 1:
                head = addone(head);
                printf("输入成功!\n按回车键以继续...");
                break;
            case 4:
                return head;
        }
        getchar();
        getchar();
        system("CLS");
    }
}


void denglu(human *head)
{
    int ch;
    int miyao = 1653226;//密钥
    int shurumiyao;
    while (1) {
        system("CLS");
        printf("欢迎来到人事管理系统\n");
        printf("1.管理员界面\n3.保存并退出\n请输入数字:");
        scanf("%d", &ch);
        switch(ch) {
            case 1:
                printf("请输入管理员的密钥:");
                scanf("%d", &shurumiyao);
                if (shurumiyao == miyao) {
                    system("CLS");
                    printf("欢迎进入管理员界面!\n");
                    head = guanliyuan(head);
                    continue;
                }
                else {
                    printf("输入错误!请重新输入!\n");
                    printf("按回车键继续...");
                    getchar();getchar();continue;
                }
                break;
            case 3:
                baocun(head);
                return;
        }
    }
}
void chushihua()
{
    FILE *fp;
    fp = fopen("人事管理数据库.txt", "w");
    fclose(fp);
}
void chucishiyong()
{
    int ch;
    printf("此系统初次被使用需要 初始化\n");
    printf("如果不是,点击初始化会清空原有数据\n");
    printf("\n1.初始化\t2.不初始化\n");
    printf("\n请输入数字:");
    scanf("%d", &ch);
    switch (ch) {
        case 1:
            chushihua();
            break;
        case 2:
            break;
    }
}
human *duqu(human *head)
{
    printf("此系统如果没有数据,不能使用读取\n");
    printf("是否 读取?\n");
    printf("1.是\t2.不是\n");
    printf("请输入数字:");
    int ch;
    scanf("%d", &ch);
    if (ch == 1) {
        FILE *fp;
        fp = fopen("人事管理数据库.dat", "rb");
        if (!fp) {
            fprintf(stderr, "Read file error!\n");
            return NULL;
        }
        human *p;
        while (!feof(fp)) {
            p = (human *)malloc(LEN);
            fread(p, LEN, 1, fp);
            p->next = head->next;
            head->next = p;
            fclose(fp);
        }
    }
    return head;
}
void baocun(human *head)
{
    human *p;
    p = head;
    FILE *fp;
    fp = fopen("人事管理数据库.dat", "wb");
    if (!fp) {
        fprintf(stderr, "Read file error!\n");
        return;
    }
    p = p->next;
    while (p->next!= NULL) {
        fwrite(p, LEN, 1, fp);
        p = p->next;
    }
    fclose(fp);
}
int main()
{
    human *head;
    head = creat();
    chucishiyong();
    head = duqu(head);
    denglu(head);
    return 0;
}
参考一下吧。 第一次读取 时出现错误的,程序异常退出,可能是因为要读取的文件不存在,楼主没有判断fopen后的返回值是否为NULL。

69,369

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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