我的代码应该找到产品ID,名称,数量和价格。 输入产品ID后,应打印产品ID所在的整行。
文本文件包含此输入:
1 CADBURY 999 1.900000
2 PEPSI 999 2.500000
3 IPHONE 976 2500.000000
4 SPIRULINA 100 50.000000
代码编译时输入为'4'。但输出是循环的:
Please enter product ID :
这不是读'4'的整行。
以下是代码:
#include <stdio.h>
#include <stdlib.h>
#include <windows.h>
#include <ctype.h>
int addProduct();
struct product
{
int quantity, reorder, i, id;
char name[20];
float price;
};
int secondmain()
{
FILE * fp;
int i=0;
struct product a;
system("cls");
char checker;
do
{
fp = fopen("addproduct.txt","a+t");
system("cls");
printf("Enter product ID : ");
scanf(" %d", &a.id);
printf("Enter product name : ");
scanf(" %s", a.name);
printf("Enter product quantity : ");
scanf(" %d", &a.quantity);
printf("Enter product price : ");
scanf(" %f", &a.price);
fprintf(fp, "%d %s %d %f\n\n", a.id, a.name, a.quantity, a.price);
printf("Record saved!\n\n");
fclose(fp);
printf("Do you want to enter new product? Y / N : ");
scanf(" %c", &checker);
checker = toupper(checker);
i++;
system("cls");
}
while(checker=='Y');
if(checker == 'N')
{
fp = fopen("addproduct.txt","r");
while(fscanf(fp, "%d %s %d %f", &a.id, a.name, &a.quantity, &a.price)==4)
{
fscanf(fp, "%d %s %d %f", &a.id, a.name, &a.quantity, &a.price);
printf("%d %s %d %f\n\n", a.id, a.name, a.quantity, a.price);
}
fclose(fp);
}
return(0);
}
/*-------------- ERROR START HERE-----------------*/
int main()
{
FILE * fp;
system("cls");
struct product a;
char array[255];
int productID;
fp = fopen("addproduct.txt","r");
while(1){
fscanf(fp, "%d %s %d %f", &a.id, a.name, &a.quantity, &a.price);
productID = atoi(array);
printf("Please enter product ID : ");
scanf(" %d", &productID);
if(productID == a.id)
{
fgets(array, 255, (FILE*)fp);
printf("%d %s %d %f\n\n", a.id, a.name, a.quantity, a.price);
}
}
return(0);
}
答案 0 :(得分:1)
while (fscanf(fp, "%d %s %d %f", &a.id, a.name, &a.quantity, &a.price) == 4)
{
fscanf(fp, "%d %s %d %f", &a.id, a.name, &a.quantity, &a.price);
printf("%d %s %d %f\n\n", a.id, a.name, a.quantity, a.price);
}
您正在拨打fscanf
两次,它会跳过第二行。
fgets(array, 255, (FILE*)fp);
printf("%d %s %d %f\n\n", a.id, a.name, a.quantity, a.price);
这部分是读取文本中的行。然后应该使用sscanf
或strtok
来解析行。
您可能还需要刷新标准输入,否则scanf(" %c", &checker)
可能无效。它使事情更容易将其分解为函数。例如:
int find_item_by_id(const char* fname, int find_id)
{
int found = 0;
struct product a;
FILE *fp = fopen(fname, "r");
if (fp)
{
while (fscanf(fp, "%d %s %d %f", &a.id, a.name, &a.quantity, &a.price) == 4)
{
if (a.id == find_id)
{
found = 1;
break;
}
}
fclose(fp);
}
return found;
}
void add_item(const char* fname)
{
struct product a;
printf("Enter product ID : ");
scanf(" %d", &a.id);
printf("Enter product name : ");
scanf(" %s", a.name);
printf("Enter product quantity : ");
scanf(" %d", &a.quantity);
printf("Enter product price : ");
scanf(" %f", &a.price);
if (find_item_by_id(fname, a.id) != 0)
{
printf("item already exists\n");
return;
}
FILE *fp = fopen(fname, "a+");
if (fp)
{
fprintf(fp, "%d %s %d %.2f\n", a.id, a.name, a.quantity, a.price);
fclose(fp);
}
}
int main()
{
const char* filename = "c:\\test\\_test.txt";
printf("list:\n");
struct product a;
FILE *fp = fopen(filename, "r");
if (fp)
{
while (fscanf(fp, "%d %s %d %f", &a.id, a.name, &a.quantity, &a.price) == 4)
printf("%d %s %d %f\n", a.id, a.name, a.quantity, a.price);
fclose(fp);
}
while(1)
{
add_item(filename);
printf("Add nother item? (y/n)\n");
int result;
while (1)
{
result = getchar();
if (result == 'y' || result == 'n')
break;
}
if (result != 'y')
break;
}
return 0;
}