好的,我知道代碼可能很長,但我儘可能地儘量減少它。我想讓代碼工作,以便重新創建我的問題。我的問題是,當我嘗試讀取文本文件時,它不會讀取所有項目。它似乎只讀了最後幾個。我可能無意中改變了一些東西,因爲它以前工作得很好。當你在程序和註冊項目,它計數項目就好了。但是,當您打開文件時,只是將項目保存在其中。它計數的項目數量少於實際文件中的項目數量,並且陣列全部錯誤。如果任何人都可以在我的代碼中看到問題,那將非常感激。程序將無法正確讀取文本文件
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define MAX 20
struct items
{
int itemnumber;
char name[30];
int balance;
};
void open_file(FILE *enter_filename, char filename[], struct items aItems[], int *num_items)
{
int i=0;
printf("Choose filename (.txt).\n");
scanf("%19s", filename);
enter_filename=fopen(filename, "r+");
if(enter_filename)
{
while(!feof(enter_filename))
{
for(i = 0; i < *num_items; i++)
{
fscanf(enter_filename, "Itemnumber: %d\n", &aItems[i].itemnumber);
fscanf(enter_filename, "Name: %s\n", aItems[i].name);
fscanf(enter_filename, "Balance: %d\n", &aItems[i].balance);
}
if(!feof(enter_filename))
{
*num_items=*num_items + 1;
}
}
printf("\nNumber of items: %d \n",*num_items);
fclose(enter_filename);
}
else
{
printf("That file doesn't exist! Create a new one.\n");
printf("What name do you want for your new file?\n");
scanf("%19s", filename);
enter_filename=fopen(filename, "w+");
printf("File is created!\n");
*num_items = 0;
fclose(enter_filename);
}
}
void register_item(struct items *aItems, int *num_items)
{
int success=1;
if(*num_items < MAX)
{
while(1)
{
printf("Item number:\n");
scanf("%d", &aItems[*num_items].itemnumber);
for(int i=0; i < *num_items; i++)
{
if(aItems[*num_items].itemnumber == aItems[i].itemnumber)
{
printf("Item number already exists, choose a unique item number.\n");
success=0;
break;
}
else
{
success=1;
}
}
if(success)break;
}
printf("Name:\n");
scanf("%29s", aItems[*num_items].name);
strlwr(aItems[*num_items].name);
printf("Balance:\n");
scanf("%d", &aItems[*num_items].balance);
*num_items+=1;
}
}
void print_item(struct items aItems[], int num_items)
{
int i;
for (i=0; i < num_items; i++)
{
printf("%d. Item number: %d Name: %s Balance: %d\n", i+1, aItems[i].itemnumber, aItems[i].name, aItems[i].balance);
}
}
void quit_program(char filename[], struct items aItems[], int *num_items)
{
FILE *fil;
fil=fopen(filename, "w+");
int i;
for(i = 0; i < *num_items; i++)
{
fprintf(fil, "Itemnumber: %d\n", aItems[i].itemnumber);
fprintf(fil, "Name: %s\n", aItems[i].name);
fprintf(fil, "Balance: %d\n\n", aItems[i].balance);
}
fclose(fil);
}
int main(void)
{
FILE *enter_filename;
struct items aItems[MAX];
int menu, num_items=0;
char filename[20];
open_file(enter_filename,filename, aItems, &num_items);
while(menu!=3)
{
printf("\n");
printf("1. Register new items to inventory.\n");
printf("2. Print all items from inventory.\n");
printf("3. Quit\n");
scanf("%d", &menu);
if(menu==1)
{
register_item(aItems, &num_items);
}
if(menu==2)
{
print_item(aItems, num_items);
}
if(menu==3)
{
quit_program(filename, aItems, &num_items);
}
}
return 0;
}
我不明白這可能是一個[mcve]讀取文件的問題。也許從頭創建一個?一般來說,我推薦閱讀[如何調試小程序](https:// ericlippert。COM/2014/03/05 /如何調試的小程序/)。 –
請參閱[爲什麼「while(!feof(file))」總是錯誤?](https://stackoverflow.com/questions/5431941/why-is-while-feof-file-always-wrong)。這是一個非常糟糕的模式。另外,請考慮顯示它應該讀取的部分文件。 – unwind
如果我從這裏刪除更多的代碼或嘗試從頭開始,它將是一個不同的程序。我只添加了運行程序所需的東西。 – Camel