FILE pf失去了它的价值

时间:2016-11-11 15:55:32

标签: c file runtime-error fclose

按照我的意愿创建程序和文件。但是当程序即将结束时(void main() {...return 0;})我收到错误。当我在主函数上检查pFile值时,它就像从未存在过一样。

请帮助。 谢谢你! :)

FILE *pFile;
char name[SIZE];
int amount;
float finalPrice;


pFile = fopen("shoppingList.txt","r+");
if(pFile == NULL)
{
    printf("Error opening file");
    return;
}
CacuShoppingList(pFile);
}





void CacuShoppingList(FILE *pFile)
{
FILE *p2File, *p3File;
char name[SIZE];
int amount;
float unitPrice, sum;
long placeHolder;
int flag;

p2File = fopen("shoppingList.txt", "a");
p3File = fopen("prices.txt", "r");
sum = 0;

while(fscanf(pFile,"%s %d", name, &amount) && amount)
{
    placeHolder = ftell(pFile);
    fseek(pFile, 0, SEEK_SET);
    if(IsProductExist(p3File, name))
    {
        fprintf(p2File, "%s quntity=%d unit price=%.2f total price=%.2f\n",name, amount,unitPrice = GetProductPrice(p3File, name, amount), amount*unitPrice);
        sum += amount*unitPrice;
    }

    else
    {
        fprintf(p2File, "%s not found on price list\n ",name);
    }

    fseek(pFile, placeHolder, SEEK_SET);
}

fprintf(p2File,"\nthe overall price for this shopping is %.2f",sum); 
flag = fclose(pFile);
flag = fclose(p2File);
flag = fclose(p3File);
}

0 个答案:

没有答案