-1
下面是从文本文件中输入:C编程的文本文件不读整条生产线
19 BISON-BURGER 10 15.000000
10 BRAISED-COD 5 17.000000
23 MOJITO 8 11.000000
3 IRISH-COFEE 6 2.300000
7 LAMB-SHOULDER 8 23.000000
输出从编译器的输入是键后出来的是:
10 BRAISED-COD 5 17.000000
3 IRISH-COFEE 6 2.300000
7 LAMB-SHOULDER 8 23.000000
为什么编译器跳过一些线?我需要做什么改变?
请帮忙。非常感谢。
下面是完整的代码:
#include <stdio.h>
#include <stdlib.h>
#include <windows.h>
#include <ctype.h>
struct product
{
int quantity, reorder, i, id;
char name[20];
float price;
};
int main()
{
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);
}
了解如何使用调试器。然后,您可以一行一行地查看代码,看看它在做什么,同时监视变量并查看它们如何变化。 –
因为你读了两行:一次在'while'条件下,并且再次在'while'的主体中。那不读同一行,它读另一行。 –
为什么每次都通过输入循环打开和关闭输出文件?为什么不在循环之前打开一次,然后在循环结束后关闭它? – FredK