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

int main() { char bookName[50]; FILE *file;

printf("请输入要查找的图书名称:");
fgets(bookName, sizeof(bookName), stdin);
bookName[strcspn(bookName, "\n")] = '\0'; // 去除换行符

file = fopen("books.txt", "r");
if (file == NULL) {
    printf("无法打开文件\n");
    return 0;
}

char line[100];
int found = 0;

while (fgets(line, sizeof(line), file)) {
    line[strcspn(line, "\n")] = '\0'; // 去除换行符
    
    if (strcmp(line, bookName) == 0) {
        found = 1;
        break;
    }
}

fclose(file);

if (found) {
    printf("图书存在\n");
} else {
    printf("图书不存在\n");
}

return 0;
用C语言实现根据书名判断文件中的图书是否存在

原文地址: http://www.cveoy.top/t/topic/hJjL 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录