| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 6581 人关注过本帖
标题:怎么把文件里的数据读到结构体里呢?
只看楼主 加入收藏
瓦力
Rank: 2
等 级:论坛游民
帖 子:41
专家分:27
注 册:2011-11-18
结帖率:100%
收藏
已结贴  问题点数:5 回复次数:7 
怎么把文件里的数据读到结构体里呢?
怎么把文件里的数据读到结构体里呢?
如果用fread该怎么办
搜索更多相关主题的帖子: 怎么办 数据 结构体 
2012-04-06 12:57
hellovfp
Rank: 16Rank: 16Rank: 16Rank: 16
等 级:禁止访问
威 望:30
帖 子:2976
专家分:7697
注 册:2009-7-21
收藏
得分:0 
你是如何把数据写进文件中的呢?

我们都在路上。。。。。
2012-04-06 13:21
瓦力
Rank: 2
等 级:论坛游民
帖 子:41
专家分:27
注 册:2011-11-18
收藏
得分:0 
程序代码:
BOOK *store(BOOK *head)                          //存储
{
    FILE *fp;
    BOOK *ptr, *p;
    p = head;

    if((fp = fopen("bookinfo.txt","w")) == NULL){
        printf("File open error!\n");
        exit(0);
    }
                                                     

    for(ptr = head;ptr;ptr = ptr->next)
    fprintf(fp,"%d   %s   %s %6.2f\n",ptr->num,ptr->writer,ptr->name,ptr->price);
    if(fclose(fp)){
        printf("Can not close the file!\n");
        exit(0);
    }

    return head;
}
2012-04-06 14:10
瓦力
Rank: 2
等 级:论坛游民
帖 子:41
专家分:27
注 册:2011-11-18
收藏
得分:0 
回复 2楼 hellovfp
程序代码:
BOOK *store(BOOK *head)                          //存储
{
    FILE *fp;
    BOOK *ptr, *p;
    p = head;

    if((fp = fopen("bookinfo.txt","w")) == NULL){
        printf("File open error!\n");
        exit(0);
    }
                                                     

    for(ptr = head;ptr;ptr = ptr->next)
    fprintf(fp,"%d   %s   %s %6.2f\n",ptr->num,ptr->writer,ptr->name,ptr->price);
    if(fclose(fp)){
        printf("Can not close the file!\n");
        exit(0);
    }

    return head;
}
2012-04-06 14:10
hellovfp
Rank: 16Rank: 16Rank: 16Rank: 16
等 级:禁止访问
威 望:30
帖 子:2976
专家分:7697
注 册:2009-7-21
收藏
得分:2 
你的读代码呢?

我们都在路上。。。。。
2012-04-06 15:41
瓦力
Rank: 2
等 级:论坛游民
帖 子:41
专家分:27
注 册:2011-11-18
收藏
得分:0 
回复 4楼 瓦力
程序代码:
BOOK *read()
{
    BOOK *ptr, *head, *tail;
    head = tail = NULL;
    int num;
    char writer[CHAR], name[CHAR];
    double price;

    if((fp = fopen("bookinfo.txt","r")) == NULL){
        printf("open fail!\n");
        exit(0);
    }
    while(! feof(fp))
    {
        ptr=(BOOK *)malloc(size);
        if(head == NULL)
            head = ptr;
        else
            tail->next = ptr;
        tail = ptr;
        fscanf(fp,"%d %s %s %lf",&num, writer, name, &price);
        ptr->num = num;
        strcpy(ptr->writer, writer);
        strcpy(ptr->name, name);
        ptr->price = price;
    }
    tail = NULL;

    return head;
}
2012-04-06 21:47
yhmm
Rank: 2
等 级:论坛游民
帖 子:22
专家分:72
注 册:2012-2-27
收藏
得分:3 
和读普通数据类型一样啊
把结构体里的数据分开读就好

                      捷建软件开发互助平台
2012-04-07 12:11
逆流而战
Rank: 1
等 级:新手上路
帖 子:1
专家分:0
注 册:2014-12-3
收藏
得分:0 
回复 6 楼 瓦力
void load_data()
{
    int test;    //测试是否读到文件末端
    rewind(data_base);    //将文件指针定位的文件首位
    fscanf(data_base,"%d",&test);    //测试读一次文件
    while(!feof(data_base))    //如果文件没有到达末端
    {    //读人文件中的记录
//        printf("Now loading......\n");
        if(head == 0)
        {
            head = (struct student *)malloc(sizeof(struct student));    //开辟新记录的存储空间
            move = head;    //将结构体指针定位到结构体头部
            last = head;               
        }
        else
        {
            last = (struct student *)malloc(sizeof(struct student));    //开辟新记录的存储空间
            move->next = last;    //结构体移动指针向后移动
            move = last;        
        }
        fscanf(data_base,"%d%s%d%d%d",&move->num,move->name,&move->mark.chinese,&move->mark.math,&move->mark.english);
        output_information(move);
        test = fseek(data_base,sizeof(struct student),SEEK_SET);    //文件指针向下一个记录移动
/*        if(test != 0)
        {
            printf("Now loading......\n");
        }*/
        n++;    //记录总数加一
            printf("Now loading......\n");
    }
    last->next = 0;
}
为什么直接将文件读到的值给结构体不行呢
2014-12-03 21:42
快速回复:怎么把文件里的数据读到结构体里呢?
数据加载中...
 
   



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

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