| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1055 人关注过本帖
标题:为什么执行到feof 这里的时候出现了段错误?
只看楼主 加入收藏
thlgood
Rank: 5Rank: 5
等 级:职业侠客
帖 子:281
专家分:381
注 册:2010-9-24
结帖率:91.43%
收藏
已结贴  问题点数:20 回复次数:1 
为什么执行到feof 这里的时候出现了段错误?
代码在这里:

程序代码:
#include <stdio.h>
#include <stdlib.h>

struct redType{
    int account;
    int amount;
};

struct rcdType{
    int account;
    int amount;
    enum{
        INSERT,     //插入
        CHANGE,     //修改
        DELETE      //删除
    }code;
};

typedef struct redType redType;
typedef struct rcdType rcdType;
/*
void printRed(void* addr, int sizePerOne, int count)
{
    int i = 0;
    void *ptr = addr;
    for (i = 0; i < count; i++)
    {
        printf("%d\t%d\n", ((redType*)ptr)->account, ((redType*)ptr)->amount);
        ptr += sizePerOne;
    }
}*/

void mergeFile(FILE* oldFile, FILE* changeFile, FILE *newFile)
{
    struct redType oldItem;
    struct rcdType changeItem;

    fread(&oldItem, sizeof(redType), 1, oldFile);
    feof(oldFile);
    fread(&changeFile, sizeof(rcdType), 1, changeFile);
    feof(changeFile);
}


int main()
{
    redType oldGroup[8] = {{1,  50 }, {5,   78}, 
                           {12, 100}, {14,  95},
                           {15, 360}, {18,  200},
                           {20, 510}, {600, 0}};

    rcdType changeGroup[6] = {{8, 100, INSERT},
                              {12, -25, CHANGE},
                              {14, 38, CHANGE},
                              {18, -200, DELETE},
                              {21, 60, INSERT},
                              {800, 0, DELETE}};

    FILE* oldFile = fopen("old","wb");
    fwrite(oldGroup ,sizeof(redType), 8 , oldFile);
    fclose(oldFile);

    FILE* changeFile = fopen("change","wb");
    fwrite(changeGroup , sizeof(rcdType), 6, changeFile);
    fclose(changeFile);

    FILE* oldFp = fopen("old", "rb");
    FILE* changeFp = fopen("change", "rb");
    FILE* newFp = fopen("new", "wb");

    if (!(oldFp && changeFp && newFp))
    {
        perror("Error!");
        exit(1);
    }
    mergeFile(oldFp, changeFp, newFp);
    return 0;
}


其实这原本是一个比较大的程序,只不过被我给简化掉了。程序不是我写的,是别人写的,由于她运行的时候出现了问题,所以我决定改改这个程序。
经过我的调试之后,发现问题出现在这里:
程序代码:
fread(&oldItem, sizeof(redType), 1, oldFile);
feof(oldFile);
fread(&changeFile, sizeof(rcdType), 1, changeFile);
feof(changeFile);

当执行到feof语句的时候,程序就出现 段错误,请问是什么原因导致了段错误
我调试的时候发现文件指针不为NULL。


顺便提一句:帮我找出问题就好了,不要管这个程序是干嘛用的,也不要管这个程序为什么要这么写,找出错误才是重点,谢谢!
我的平台:Linux GCC 4.6.3/GCC 4.8.0
搜索更多相关主题的帖子: color amount account 
2013-05-27 20:45
hahayezhe
Rank: 15Rank: 15Rank: 15Rank: 15Rank: 15
来 自:湖南张家界
等 级:贵宾
威 望:24
帖 子:1386
专家分:6999
注 册:2010-3-8
收藏
得分:20 
很明显的笔误fread(&changeFile, sizeof(rcdType), 1, changeFile);
2013-05-30 17:29
快速回复:为什么执行到feof 这里的时候出现了段错误?
数据加载中...
 
   



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

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