-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