标题:请教各位大佬 一个关于文件操作的问题
只看楼主
W亨
Rank: 1
等 级:新手上路
帖 子:1
专家分:0
注 册:2017-10-20
结帖率:0
已结贴  问题点数:20 回复次数:1 
请教各位大佬 一个关于文件操作的问题
#include<stdio.h>
#include<stdlib.h>
#define N 3

struct student
{
    int no;
    char name[20];
    char sex;
    int age;
    float score;
}stu[N],*st;


void enter()
{
    int i;
    void menu();
    void save();
    printf("请输入学生信息:\n");
    printf("no\tname\tsex\tage\tscore\n");
    for(i=0;i<N;i++)
    {
        scanf("%d-%s-%c-%d-%lf",&stu[i].no,&stu[i].name,&stu[i].sex,&stu[i].age,&stu[i].score);
        
    }
    if(N!=0)
        save(N);
    getchar();
    getchar();
    menu();
   
}

void save()
{
    int i;
    FILE *fp;
    if(!(fp = fopen("student-heng","wb")))
    {
        printf("Cannot open student-heng!\n");
        exit(0);
    }
    for(i=0;i<N;i++)
    {
        fwrite(&stu[i],sizeof(struct student),1,fp);
    }
    fclose(fp);
}

void load()
{
    FILE *fp;
    int i;
    if(!(fopen("student-heng","rb")))
    {
        printf("Cannot open student-heng!\n");
        exit(0);
    }
    for(i=0;i<N;i++)
    {
        fread(&stu[i],sizeof(struct student),1,fp);
        return 0;
    }
    fclose(fp);
}

void menu()
{
    while(1)
    {
        int x;
        printf("------------操作选项-------------\n");
        printf("1:返回菜单\n");
        printf("2:读入数据\n");
        printf("3:查找数据\n");
        printf("4:结束程序\n");
        printf("---------------------------------\n");
        printf("选择操作:",x);
        scanf("%d",&x);
        if(x==4)
            break;
        switch(x)
        {
        case 1:
            break;
        case 2:
            enter();
            break;
        case 3:
            load();
            break;
        default:
            printf("操作不正确");
            break;
        }
    }
}

int main()
{
    menu();
    return 0;
}
为什么执行 操作三load()操作的时候显示 "10.exe已停止工作",请大神指教
搜索更多相关主题的帖子: 操作 stu int void printf 
2017-10-20 18:39
吹水佬
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
等 级:版主
威 望:432
帖 子:10064
专家分:41463
注 册:2014-5-20
得分:20 
#include<stdio.h>
#include<stdlib.h>
#define N 3

struct student
{
    int no;
    char name[20];
    char sex;
    int age;
    float score;
} stu[N],*st;


void enter()
{
    int i;
    void menu();
    void save();
    printf("请输入学生信息:\n");
    printf("no\tname\tsex\tage\tscore\n");
    for(i=0; i<N; i++)
    {
        //scanf("%d-%s-%c-%d-%lf",&stu[i].no,&stu[i].name,&stu[i].sex,&stu[i].age,&stu[i].score);
        scanf("%d%s %c%d%f",&stu[i].no,&stu[i].name,&stu[i].sex,&stu[i].age,&stu[i].score);
    }
    if(N!=0)
        save(N);
    //getchar();
    //getchar();
    //menu();
}

void save()
{
    int i;
    FILE *fp;
    if(!(fp = fopen("student-heng","wb")))
    {
        printf("Cannot open student-heng!\n");
        exit(0);
    }
    for(i=0; i<N; i++)
    {
        fwrite(&stu[i],sizeof(struct student),1,fp);
    }
    fclose(fp);
}

void load()
{
    FILE *fp;
    int i;
    //if(!(fopen("student-heng","rb")))
    if(!(fp=fopen("student-heng","rb")))
    {
        printf("Cannot open student-heng!\n");
        exit(0);
    }
    for(i=0; i<N; i++)
    {
        fread(&stu[i],sizeof(struct student),1,fp);
        //return 0;
    }
    fclose(fp);
}

void list()
{
    int i;
    for(i=0; i<N; i++)
        printf("%d-%s-%c-%d-%f\n",stu[i].no,stu[i].name,stu[i].sex,stu[i].age,stu[i].score);
}


void menu()
{
    while(1)
    {
        int x;
        printf("------------操作选项-------------\n");
        printf("1:返回菜单\n");
        printf("2:读入数据\n");
        printf("3:查找数据\n");
        printf("4:结束程序\n");
        printf("---------------------------------\n");
        printf("选择操作:",x);
        scanf("%d",&x);
        if(x==4)
            break;
        switch(x)
        {
            case 1:
                break;
            case 2:
                enter();
                break;
            case 3:
                load();
                list();
                break;
            default:
                printf("\n操作不正确\n");
                break;
        }
    }
}

int main()
{
    menu();
    return 0;
}
2017-10-21 06:24



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-481600-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.052301 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved