C语言使用文件处理从ITEM.txt搜索字符串始终提示找不到求助
问题排查与修复方案
问题原因
- 字符串缺少结束符:C语言中字符串必须以
\0作为结束标识,代码逐字符读取商品名存入a数组后,没有添加结束符,strcmp对比时会读取数组中后续的脏数据,永远无法匹配成功。 - 大小写逻辑不一致:代码将文件中读取的字符全部转成小写,但未对用户输入的
order内容做小写转换,大小写不同的相同内容也会判定为不匹配。 - 执行顺序错误:读取完一行商品名后,代码先将计数变量
k重置为0再执行对比,逻辑顺序颠倒。 - 未定义行为风险:
fflush(stdin)是标准C未定义的操作,仅在部分编译器中生效,容易引发输入缓冲区处理异常。 - 变量类型错误:
fgetc返回值为int类型,用于兼容EOF(值为-1)的存储,用char类型接收fgetc返回值可能因溢出导致EOF判断失效。
修复方案
按以下步骤修改代码:
- 读取完一行商品名后,立即在
a[k]位置赋值\0补全字符串结束符。 - 用户输入
order后,遍历将所有字符转成小写,和文件中处理后的内容保持一致。 - 调整执行顺序:读完一行→补全结束符→执行对比→再重置
k为0读取下一行。 - 替换
fflush(stdin)为标准的输入缓冲区清空实现。 - 将接收
fgetc返回值的变量e改为int类型。
修复后完整代码
#include <stdio.h> #include <stdlib.h> #include <string.h> #include <ctype.h> // 通用清空输入缓冲区函数 void clear_stdin() { int ch; while ((ch = getchar()) != '\n' && ch != EOF); } int main() { int choice, quantity; char order[1000], a[1000]; char c; int e; // 改为int类型接收fgetc返回值 int amount = 0, line_num = 0, j = 0; int price[1000] = {77, 100, 43, 35, 64, 125, 151, 91, 118, 184, 27, 47, 25, 100, 50, 59, 45, 350, 370, 530, 395, 460, 430, 620, 150, 145, 96, 81, 54, 94, 579, 565, 165, 206, 193, 198, 849, 184, 165, 75, 165, 126, 210, 280, 210, 189, 199, 350, 478, 440, 215, 207, 309, 139, 248, 204, 195, 253, 175, 90, 175, 329, 175, 199}; int Quantity[1000] = {100, 100, 100, 150, 150, 100, 100, 100, 100, 100, 50, 50, 50, 50, 50, 10, 10, 10, 10, 5, 5, 10, 10, 10, 5, 1000, 5, 5, 5, 5, 30, 30, 30, 30, 30, 30, 30, 30, 30, 10, 10, 300, 500, 500, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100}; char q; printf("1.MENU\n2.ORDER\n3.PAY_BILL\n4.EXIT\n"); printf("Enter choice : "); scanf("%d", &choice); clear_stdin(); // 清空scanf留下的换行符 FILE *ptr = NULL; FILE *p = NULL; while (choice < 4) { switch (choice) { case 1: ptr = fopen("DATABASE.txt", "r"); if(ptr == NULL) { printf("打开菜单文件失败\n"); break; } while (1) { c = fgetc(ptr); if (c == EOF) { break; } printf("%c", c); } printf("\n"); fclose(ptr); break; case 2: while (1) { label: printf("What would you like to order: "); scanf("%[^\n]%*c", order); // 用户输入转小写 for(int i=0; order[i]; i++) { order[i] = tolower(order[i]); } p = fopen("ITEM.txt", "r"); if(p == NULL) { printf("打开商品文件失败\n"); goto label; } int k = 0; line_num = 0; // 每次搜索重置行号 int found = 0; while (1) { e = fgetc(p); if (e == EOF) { break; } k = 0; while (e != '\n' && e != EOF) { a[k++] = tolower(e); e = fgetc(p); } a[k] = '\0'; // 补全字符串结束符 line_num++; if (strcmp(a, order) == 0) { found = 1; break; } } fclose(p); if(!found) { printf("couldn't find \n"); goto label; } printf("Please Enter the Quantity(in numbers): "); scanf("%d", &quantity); clear_stdin(); printf("Added to your cart\n"); printf("\n"); printf("Would you like to place another order?y/n: "); scanf("%c", &q); clear_stdin(); if (q == 'n') { break; } } printf("\n"); break; case 3: printf("\n"); break; default: printf("输入选项无效\n"); break; } printf("Enter choice : "); scanf("%d", &choice); clear_stdin(); } printf("Thank You For visiting our market....Have a nice day"); return 0; }
内容的提问来源于stack exchange,提问作者Abhirup Bhattacharya
相关产品推荐
相关产品推荐

