| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1460 人关注过本帖
标题:文件输入输出的问题
取消只看楼主 加入收藏
新人学习
Rank: 3Rank: 3
等 级:论坛游侠
威 望:1
帖 子:145
专家分:187
注 册:2017-10-26
结帖率:66.67%
收藏
 问题点数:0 回复次数:0 
文件输入输出的问题
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define XS 5

struct Student
{
    int xuehao;
    char xingming[10];
    int yuwen;
    int shuxue;
    int yingyu;
    int pingjun;
}xuesheng[XS];
void paixu(struct Student a[], int n)
{
    int i, j;
    for(i = 0; i < XS - 1; i++)
    {
        for(j = i + 1; j < XS; j++)
        {
            if(a[i].pingjun > a[j].pingjun)
            {
                struct Student b;
                b.xuehao = a[i].xuehao;
                strcpy(b.xingming, a[i].xingming);
                b.yuwen = a[i].yuwen;
                b.shuxue = a[i].shuxue;
                b.yingyu = a[i].yingyu;
                b.pingjun = a[i].pingjun;
                a[i].xuehao = a[j].xuehao;
                strcpy(a[i].xingming, a[j].xingming);
                a[i].yuwen = a[j].yuwen;
                a[i].shuxue = a[j].shuxue;
                a[i].yingyu = a[j].yingyu;
                a[i].pingjun = a[j].pingjun;
                a[j].xuehao = b.xuehao;
                strcpy(a[j].xingming, b.xingming);
                a[j].yuwen = b.yuwen;
                a[j].shuxue = b.shuxue;
                a[j].yingyu = b.yingyu;
                a[j].pingjun = b.pingjun;
            }
        }
    }
}

void ceshi(void)   //用这个函数测试读取文件失败,但是我重新写了一个cpp文件测试却是成功的.高手来解答下.
{
    struct Student
    {
        int xuehao;
        char xingming[10];
        int yuwen;
        int shuxue;
        int yingyu;
        int pingjun;
    }xue[XS];
    FILE *fp;
    if((fp = fopen("stu_sort.dat","rb")) == NULL)
    {
        printf("打开文件失败.\n");
        exit(0);
    }
    for(int i = 0; i < XS; i++)
    {
        if(fread(&xue[i],sizeof(xue[i]), 1, fp) != 1)
        {
            printf("xue[%d]提取数据失败.\n",i);
        }
    }
    for(int i = 0; i < XS; i++)
        printf("学号:%d, 姓名:%-6s, 语文成绩:%d, 数学成绩:%d, 英语成绩:%d, 平均分:%d.\n",xue[i].xuehao,xue[i].xingming,xue[i].yuwen,xue[i].shuxue,xue[i].yingyu,xue[i].pingjun);
    fclose(fp);
}

int main()
{
    FILE *fp;
    if((fp = fopen("stud.dat", "rb")) == NULL)
    {
        printf("打开文件失败.\n");
        exit(0);
    }
    for(int i = 0; i < XS; i++)
    {
        if(fread(&xuesheng[i], sizeof(xuesheng[i]), 1, fp) != 1)
        {
            printf("读取文件数据失败.\n");
        }
    }
    for(int i = 0; i < XS; i++)
        printf("学号:%d, 姓名:%-6s, 语文成绩:%d, 数学成绩:%d, 英语成绩:%d, 平均分:%d.\n",xuesheng[i].xuehao,xuesheng[i].xingming,xuesheng[i].yuwen,xuesheng[i].shuxue,xuesheng[i].yingyu,xuesheng[i].pingjun);

    paixu(xuesheng, XS);

    fclose(fp);

    if((fp = fopen("stu_sort.dat", "wb")) == NULL)
    {
        printf("打开文件失败.\n");
        exit(0);
    }

    for(int i = 0; i < XS; i++)
    {
        if(fwrite(&xuesheng[i], sizeof(xuesheng[i]), 1, fp) == 1)
        {
            printf("xuesheng[%d]数据拷贝成功.\n",i);
        }

    }
    ceshi();
    fclose(fp);
    system("pause");
    return 0;
}
搜索更多相关主题的帖子: 文件 int for 失败 printf 
2018-03-24 10:18
快速回复:文件输入输出的问题
数据加载中...
 
   



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

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