标题:再次读取文件时,把原来的记录打印了两遍,是fread的问题还是我代码逻辑问题.
只看楼主
code_man_x
Rank: 1
等 级:新手上路
威 望:1
帖 子:6
专家分:5
注 册:2017-10-2
结帖率:0
已结贴  问题点数:20 回复次数:1 
再次读取文件时,把原来的记录打印了两遍,是fread的问题还是我代码逻辑问题.
以下是代码,代码的逻辑是可以再次读取文件时,可以把原来已经存储的记录输出.
但是出现的问题是,再次读取文件时,会把文件存储的记录输出两遍.
自己调试了好几遍,感觉是代码中红色箭头的逻辑问题...但是不知道为什么这里逻辑会问题
按照道理,第一次如果存储两个记录,文件中存储的结构应该只有两个.重新打开文件时,count++到2时,fread函数读取应该不能再读到数据.
但是编译器还是继续进入while循环,把第一次的两个记录又读取一遍,再输出.
各位大神帮看看
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAXTITL 40
#define MAXAUTL 40
#define MAXBKS 10
char *s_gets(char *st, int n);
struct book
{
    char title[MAXTITL];
    char author[MAXAUTL];
    float value;
};
int main()
{
    struct book library[MAXBKS];
    int count =0;
    int index, filecount;
    FILE *pbooks;
    int size = sizeof(struct book);
    if ( (pbooks = fopen("C:/JavaTest/book2.dat", "a+b")) == NULL)
    {
        fputs("Can't open book.dat file\n",stderr);
        exit(1);
    }
    rewind(pbooks);
>>>>>>>    while (count < MAXBKS && fread(&library[count], size, 1, pbooks) == 1 && feof(pbooks) != EOF)
    {
        if (count == 0)
        {
            puts("Current contents of book.dat");
        }
        printf("%s by %s: $%.2f\n",library[count].title,library[count].author, library[count].value);
        count++;
    }
    filecount = count;
    if (count == MAXBKS)
    {
        fputs("The book.dat file is full.",stderr);
        exit(2);
    }
    puts("Please add new book titles.");
    puts("Press [enter] at the start of a line to stop.");
    while (count < MAXBKS && s_gets(library[count].title,MAXTITL) != NULL && library[count].title[0] != '\0')
    {
        puts("Now enter the anthor.");
        s_gets(library[count].author, MAXAUTL);
        puts("Now enter the value.");
        scanf("%f", &library[count++].value);
        while (getchar() != '\n')
        {
            continue;
        }
        if (count < MAXBKS)
        {
            puts("Enter the next title.");
        }
    }
    if (count >0 )
    {
        puts("Here is the list of your books:");
        for(index = 0; index < count; index++)
        {
            printf("%s by %s: $%.2f\n",library[index].title,library[index].author, library[index].value);
            fwrite(&library[filecount], size, count - filecount , pbooks);
        }
    }else
        {
            puts("No books? Too bad.\n");
        }
    puts("Bye.\n");
    fclose(pbooks);
    return 0;
}
char* s_gets(char *st, int n)
{
    char * ret_val;
    char * find;
    ret_val = fgets(st, n, stdin);
    if (ret_val)
    {
        find  = strchr(st, '\n');
        if (find)
        {
            *find = '\0';
        }else
        {
            while (getchar() != '\n')
            {
                continue;
            }
        }
    }
    return ret_val;
}
搜索更多相关主题的帖子: 文件 count char library puts 
2017-10-03 21:32
炎天
Rank: 13Rank: 13Rank: 13Rank: 13
来 自:桃花岛
等 级:贵宾
威 望:29
帖 子:1218
专家分:4986
注 册:2016-9-15
得分:20 


以前的原有的数据用结构体数组输出, 后加入的数据写入文件,并且输出,
另外while循环没问题, while (count < MAXBKS && fread(&library[count], size, 1, pbooks) == 1 && feof(pbooks) != EOF)
count <MAXBKS可写可不写, count每次运行开始都被赋值为0
程序代码:
    if (count >0)
    {
        puts("Here is the list of your books:");
        for (index = 0; index < filecount; index++)
        {
            printf("%s by %s: $%.2f\n", library[index].title, library[index].author, library[index].value);
        }
        for (; index < count; index++)
        {
            fwrite(&library[index], size, 1, pbooks);
            printf("%s by %s: $%.2f\n", library[index].title, library[index].author, library[index].value);
        }
    }
    else
    {
        puts("No books? Too bad.\n");
    }


[此贴子已经被作者于2017-10-10 17:45编辑过]


早知做人那么辛苦!  当初不应该下凡
2017-10-04 10:22



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




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

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