为什么我的fwrite()函数写入文件,但是fread()没有从中读取?

时间:2018-04-11 15:47:44

标签: c file file-io

好的,所以我的代码中我正在为文件编写一个结构数组。当我检查文件时,信息在那里,但当我调用fread()时,当我尝试访问数据时没有打印出来。我认为fwrite()指向的数据在结束时不在正确的位置。提前谢谢。

这是main()

#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <stdlib.h>

struct NOVEL
{
    char title[50];
    char newtitle[50];
    char author[50];
    char newauthor[50];
    char genre[20];
    char newgenre[20];
    char quality[20];
    int numpages;
};

int addbooks(struct NOVEL[], char[]);
void displaybooks(struct NOVEL[], int, char[]);
void sortbooks(struct NOVEL[], int);
void title(struct NOVEL[], int);
void author(struct NOVEL[], int);
void category(struct NOVEL[], int);
void numpages(struct NOVEL[], int);

int main()
{
    //Define the variables
    int num, numbooks = 0;
    char repeat = 'Y', name[50];
    struct NOVEL library[100] = {0};



    //Asks what the user wants to do
    while (repeat == 'Y' || repeat == 'y')
    {
        printf("1. Add Books\n2. Display Books\n3. Sort Books\nPlease enter your choice: ");
        scanf("%i", &num);
        getchar();

        switch (num)
        {
                //Add Books
            case 1:
                numbooks = addbooks(library, name);
                break;

                //Display Books
            case 2:
                displaybooks(library, numbooks, name);
                break;

                //Sort Books
            case 3:
                sortbooks(library, numbooks);
                break;

            default:
                printf("Incorrect Number Entered!\n");
        }

        printf("\nWould you like to do something else? [Y/N]: ");
        scanf(" %c", &repeat);
    }

    return 0;
}

这是我做的addbooks()函数:

int addbooks(struct NOVEL books[], char filename[])
{
    static int i = 0;
    char stop = 'Y';
    FILE *fpnew = NULL;

    //The user adds their books here
    while (i <= 29 && (stop == 'Y' || stop == 'y'))
    {
        printf("Please enter the name of the file you would like to create/use: ");
        gets(filename);

        if ((fpnew = fopen(filename, "wb")) == NULL)
        {
            printf("Can't open the file name: %s", filename);
            exit(1);
        }

        printf("Please enter your book's title: ");
        gets(books[i].title);

        printf("Please enter your book's author: ");
        gets(books[i].author);

        printf("Please enter your book's genre: ");
        gets(books[i].genre);

        printf("Please enter your book's rating [1-10]: ");
        gets(books[i].quality);

        printf("Please enter your book's number of pages: ");
        scanf("%i", &books[i].numpages);
        getchar();

        i++;


        printf("Add another book? [Y/N]: ");
        scanf("%c", &stop);
        getchar();
    }

    fwrite(books, sizeof(struct NOVEL), i, fpnew);
    fclose(fpnew);

    return i;
}

这是我制作的displaybooks()函数:

void displaybooks(struct NOVEL books1[], int numberofbooks, char dfilename[])
{
    int j;
    FILE *fpdisplay = NULL;

    //struct NOVEL read[30];
    printf("Please enter the name of the file you would like to create/use: ");
    gets(dfilename);

    if ((fpdisplay = fopen(dfilename, "rb")) == NULL)
    {
        printf("Can't open the file name: %s", dfilename);
        exit(1);
    }

    fread(books1, sizeof(struct NOVEL), numberofbooks, fpdisplay);
    fclose(fpdisplay);

    //Prints out the book's information
    printf("%-30s%-30s%-30s%-30s%-30s\n", "Title", "Author", "Genre", "Quality", "Number of Pages");

    for (j = 0; j < numberofbooks; j++)
    {
        printf("%-30s%-30s%-30s%-30s%-30i\n", books1[j].title, books1[j].author, books1[j].genre, books1[j].quality, books1[j].numpages);
    }
}

2 个答案:

答案 0 :(得分:2)

这可能会或可能不会解决您的问题,但您可以在fopen的{​​{1}}循环内拨打while。如果您向图书馆添加15本图书,则最终会调用addbooks 15次。这是不正确的。

你有:

fopen

需要将该代码块移出循环。

while (i <= 29 && (stop == 'Y' || stop == 'y'))
{
    printf("Please enter the name of the file you would like to create/use: ");
    gets(filename);

    if ((fpnew = fopen(filename, "wb")) == NULL)
    {
        printf("Can't open the file name: %s", filename);
        exit(1);
    }

改善建议

使用

printf("Please enter the name of the file you would like to create/use: ");
gets(filename);

if ((fpnew = fopen(filename, "wb")) == NULL)
{
    printf("Can't open the file name: %s", filename);
    exit(1);
}

while (i <= 29 && (stop == 'Y' || stop == 'y'))
{

有点令人困惑,至少对我而言。当char stop = 'Y'; ... while (i <= 29 && (stop == 'Y' || stop == 'y')) stop时,您想要停止,而不是继续循环。变量的更好名称是'Y'readmore

readnext

答案 1 :(得分:0)

Title        Author           Genre        Quality       Number of Pages               
Mydreams       xyz            Biography      10             100            
SportDay       pqr            Fiction        10             100

这是你程序的输出,那里有什么问题?你的“显示”功能只能从文件中读取。