简体   繁体   English

读取C中特定格式的文本文件

[英]Reading text files with specific format in C

I've been trying to read a text file containing a database of books in C which has a specific format.我一直在尝试阅读包含 C 中具有特定格式的书籍数据库的文本文件。 Here is what I have so far:这是我到目前为止所拥有的:

#include "database_main.h"

struct Book getDetailsFromFile(FILE *ptr)
{
    struct Book bookVar;    /*new book variable as struct*/
    char title[MAX_TITLE_LENGTH+1], author[MAX_AUTHOR_LENGTH+1];   /*temp variables to input data*/
    int year;

    fscanf(ptr, "Title: %100[0-9a-zA-Z ]\n", title);    /*fscanf collecting up to 100 character of only numbers or letters*/
    fprintf(stderr, "%s\n", title);

    fscanf(ptr, "Author: %100[0-9a-zA-Z ]\n", author);
    fprintf(stderr, "%s\n", author);

    fscanf(ptr, "Year: %i\n\n", &year);
    fprintf(stderr, "%i\n", year);

    strcpy(bookVar.title, title);   /*transfering values to stuct*/
    strcpy(bookVar.author, author);
    bookVar.year = year;
    bookVar.right = NULL;
    bookVar.left = NULL;
    return bookVar;
}

/* read file containing database of books */
void read_book_database ( char *file_name )
{
    FILE *fptr;   /*file pointer*/
    if ((fptr = fopen(file_name, "r")) == NULL) {
        fprintf(stderr, "Error! opening file\n");   /*catch error*/
    }
    else
    {
        while (feof(fptr) == 0) /*do until end of file*/
        {
            addBook(getDetailsFromFile(fptr));   /*adds book to database from the file*/
            fprintf(stderr, "Got Book\n");
        } 
        fprintf(stderr, "closing file\n");
        fclose(fptr);
    }
}

When the code is run it continuously repeats "Got Book" without ever printing any details of the book.当代码运行时,它会不断重复“Got Book” ,而不会打印这本书的任何细节。

I've also tried using fgets() which does input the data however seems to separate out the year field to an entirely new book in the database with no other information.我也尝试过使用fgets() ,它确实输入了数据,但似乎将年份字段分离到数据库中的一本全新书籍中,没有其他信息。 This is not an issue with addBook() as this has been tested with manual inputs and works correctly.这不是addBook()的问题,因为它已经过手动输入测试并且可以正常工作。

Secondly fgets() doesn't cut out the Title: and such fields at the beginning of each line of the file.其次fgets()不会删除 Title: 以及文件每行开头的此类字段。 I have tried some of the other online solutions however none have worked.我尝试了其他一些在线解决方案,但都没有奏效。

Thanks for any help you can give.谢谢你提供的所有帮助。

Also please note this is being compiled with the ANSI standard.另请注意,这是使用 ANSI 标准编译的。

So the while(feof(fptr)==0) turned out to be the issue and I've now upadted the whole thing to the following:所以 while(feof(fptr)==0) 原来是问题所在,我现在将整个内容更新为以下内容:

void read_book_database ( char *file_name )
{
    char c[1000];
    FILE *fptr;
    if ((fptr = fopen(file_name, "r")) == NULL) {
        fprintf(stderr, "Error! opening file\n");
    }
    else
    {
        struct Book newBook;
        for(newBook;fscanf(fptr, "Title: %100[0-9a-zA-Z ,]\nAuthor: %100[0-9a-zA-Z ,]\nYear: %i\n\n", &(newBook.title), &(newBook.author), &(newBook.year))==3;)
        {
            fprintf(stderr, "Title: %s\nAuthor: %s\nYear: %i\n\n", newBook.title, newBook.author, newBook.year);
            addBook(newBook);
            /*addBook(getDetailsFromFile(fptr));*/
            fprintf(stderr, "Got Book\n");
        } 
        fprintf(stderr, "closing file\n");
        fclose(fptr);
    }
}

It does now cause some issues in different points of the program but I don't think that's related to the reading of the text file so I believe this particular issue is fixed.它现在确实会在程序的不同点引起一些问题,但我认为这与文本文件的读取无关,所以我相信这个特定问题已得到解决。 Thanks everyone for the help感谢大家的帮助

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM