【问题标题】:Simple Search for Files in C在 C 中简单搜索文件
【发布时间】:2014-03-01 11:57:00
【问题描述】:

我正在学习用 C 编程,并且正在做一个小项目:图书馆管理软件。我遇到的问题是使搜索功能起作用。我知道如果我处理大量数据,我的代码非常简单且效率低下,但既然情况并非如此,我认为它应该可以工作。 这是我写的函数:

void search_for_book (FILE *BooksFile) {
    BooksFile = fopen("Data.txt", "a+");

    char book_title[100], test_book[100], author[100], category[100];
    int answer, status, more_books;

    more_books: // To search for more books

    printf("Type the title of the book you would like to look for:\n");
    scanf("%s", book_title);

    while(!feof(BooksFile)){
        fscanf(BooksFile, "%s", test_book);

        if(strcmp(book_title, test_book) == 0) { // strcmp returns zero if strings are equal
            printf("The book you informed was found.\n");
            printf("Do you want to see its information? Type 1 for yes and 2 for no.\n");
            scanf("%d", &answer);

            if (answer == 1) { // User wants information
                fscanf(BooksFile, "%s", author);
                fscanf(BooksFile, "%s", category);
                fscanf(BooksFile, "%d", &status);

                printf("Book's title: %s\n", book_title);
                printf("Book's author: %s\n", author);
                printf("Book's category: %s\n", category);
                printf("Book's status: %d\n", status);

                break;

            } else { // User doesn't want information
                printf("Would you like to search for another book? Type zero if that's the case.\n");
                scanf("%d", &more_books);

                if (more_books == 0) {
                    goto more_books;
                }

                break;
            }
        }
    } 
}

我的代码有什么问题,我该如何解决?当我调用这个函数时,程序只执行 printf(要求输入书名),然后简单地返回主函数,不搜索任何内容。

【问题讨论】:

  • 首先在 c 中避免 goto。使用goto 在内部/外部循环之间跳转很危险吗?它是如何工作的?
  • 您可以尝试告诉(请在问题中)什么看起来正在工作,或者工作不同你期待。
  • 发送到代码审查部分。
  • 你犯了什么错误?更多解释请? (例如:我在 blabla 行有指针错误)
  • 我并没有真正得到错误。当我调用该函数时,程序会执行 printf(要求输入书名)并且不会搜索该书

标签: c string file search


【解决方案1】:

您的代码似乎效率低下,我不知道您为什么在 search_for_book 函数中传递文件指针,而不是您可以在本地获取并使用它打开。我对你的函数做了一些修改,试试吧。

void search_for_book() 
{
    FILE *BooksFile;

    BooksFile = fopen("Data.txt", "r");

    char book_title[100], test_book[100], author[100], category[100];
    int answer, status, more_books;

    while(1)
    {
        printf("Type the title of the book you would like to look for:\n");
        scanf("%s", book_title);

        while(!feof(BooksFile))
        {
            fscanf(BooksFile, "%s", test_book);

            if(strcmp(book_title, test_book) == 0) 
            { 
                printf("The book you informed was found.\n");
                printf("Do you want to see its information? Type 1 for yes and 2 for no.\n");
                scanf("%d", &answer);

                if (answer == 1) 
                { 
                    // User wants information
                    fscanf(BooksFile, "%s", author);
                    fscanf(BooksFile, "%s", category);
                    fscanf(BooksFile, "%d", &status);

                    printf("Book's title: %s\n", book_title);
                    printf("Book's author: %s\n", author);
                    printf("Book's category: %s\n", category);
                    printf("Book's status: %d\n", status);
                    break;
                } 

            }
        } 

        rewind(BooksFile);
        printf("Would you like to search for another book? Type zero if that's the case.\n");
        scanf("%d",&more_books);

        if(more_books == 1)
         continue;
        else
          break;
    }
}

【讨论】:

    【解决方案2】:
    • 用“r”而不是“a+”打开文件,a+ 用于追加和文件末尾,您没有写入文件。
    • 看起来您的文件包含标题、作者、类别和状态,当您搜索时您阅读了所有行,因此您可能会在作者行中找到您要查找的文本。即使您不需要,也要阅读每本书的所有数据。
    • 当您返回 more_books 时,您不会倒带文件。尝试 fseek(0, SEEK_SET)。或关闭/重新打开文件。
    • 键入用 for 循环替换 goto,它更易于阅读(虽然 goto 不受欢迎但有效)。

    【讨论】:

    • 修改了我的代码,但是搜索还没有工作。感谢您的建议!
    • 你可以尝试用gets替换fscanf("%s"),这样更简单。另请注意,您必须输入确切的大写和小写标题,使用 stricmp 来避免这种情况。还要注意你的字符串中可能有一个讨厌的 '\n' 字符;)
    猜你喜欢
    • 2016-01-26
    • 1970-01-01
    • 2020-08-06
    • 2018-12-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-07-10
    相关资源
    最近更新 更多