2
所以在這裏我基本上有兩個迴路,除了他們FSCANF到不同的目錄,這基本上做同樣的事情。 第二個應該fscanf到一個結構,並導致程序崩潰。 這是爲什麼? 導致程序崩潰的代碼是程序中最後一個循環。對於結構循環導致崩潰
#include <stdio.h>
#include <conio.h>
#include <string.h>
#define MAXLEN 100
int main()
{
char filename1[MAXLEN];
char filename2[MAXLEN];
char filename3[MAXLEN];
char filename4[MAXLEN];
char itemname[MAXLEN];
printf("Enter the input file: ");
scanf("%s", filename1);
strcpy(filename3,filename1);
strcat(filename3,"Output.txt");
strcpy(filename4,filename1);
strcat(filename4,"Log.txt");
strcpy(filename2, filename1);
strcat(filename2, "Customers.txt");
strcat(filename1, ".txt");
printf("%s will be used",filename1);
FILE *inputfile1 = NULL;
FILE *inputfile2 = NULL;
FILE *outputfile = NULL;
FILE *logfile = NULL;
inputfile1 = fopen(filename1, "r");
inputfile2 = fopen(filename2, "r");
outputfile = fopen(filename3, "w");
logfile = fopen(filename4, "w");
int numberofitems =0;
while (fscanf(inputfile1,"%s",itemname)==1){
numberofitems++;
}
rewind(inputfile1);
numberofitems /= 4;
struct storestock{
char itemnames[numberofitems][MAXLEN];
int isdecimal[numberofitems];
double stock[numberofitems];
double price[numberofitems];
};
typedef struct storestock store;
store inventory;
int i;
for (i=0; i < numberofitems; i++)
{
fscanf(inputfile1,"%s %d %lf %lf",inventory.itemnames[i],&(inventory.isdecimal[i]),
&(inventory.stock[i]),&(inventory.price[i]));
printf("\n %dst item %s %d %lf %lf", i+1,inventory.itemnames[i],inventory.isdecimal[i]
,inventory.stock[i],inventory.price[i]);
}
struct customers{
char customername[MAXLEN];
char wanteditems[10][MAXLEN];
double amountwanted[10];
};
int j,k,l;
int numberofcustomers = 0;
int itemnumber=0;
double itemamount;
char string[MAXLEN];
for (j=0;j<100;j++){
if (fscanf(inputfile2,"%s %lf", string,&itemamount)==1){
numberofcustomers++;
printf("\n%s",string);
}}
printf("%d", numberofcustomers);
struct customers mycustomers[numberofcustomers];
rewind(inputfile2);
**for (k=0;k<100;k++){
if (fscanf(inputfile2,"%s %lf", mycustomers[k].customername,&itemamount)==1){
printf("\n%s", mycustomers[k].customername);}
}**
getch();
return 0;
}
嘗試'for(k = 0; k
請提供[最小,完整和可驗證示例](http://stackoverflow.com/help/mcve)。 –
沒有工作酷傢伙 –