首页 > 解决方案 > 文件中的内容在c程序中不存在

问题描述

如何制作文件,我尝试在c中制作file.txt但出现错误,我编写的程序成功执行,但在文件或记事本中内容不存在。

这是我想要输入到 file.txt 的代码片段:

int a,b,c,d,e = 0,f = 0;
int check,check2,check3;
int *fptr;
fptr = fopen("file.txt","w");

printf("\nenter the amount of food to be purchased  : ");
check=scanf("%d", &b);
printf("\n");
            
if (check != 0) {
 for (a=1; a<=b; a++){
   printf("the price of food of- %d \t : ",a);
   check2=scanf("%d", &c);
   printf("\n");
                
   if (check2==0) {
     printf("Please enter in numeric !!\n");
     while ((getchar()) != '\n'); 
     system("PAUSE");
     goto cashier;
   }
   printf("the amount ordered \t : ");
   check3=scanf("%d", &d);
   printf("\n");
                
   if (check3==0) {
     printf("Please enter in numeric !!\n");
     while ((getchar()) != '\n'); 
     system("PAUSE");
     goto cashier;
   }
    e=c*d;
    f+=e;
}
printf("total price of food  \t : Rp.%d",f);
printf("\n");

char isTrue;
printf("\n\nPush [Y] to save a copy from your payment [T]\nTo Out\n:");
scanf(" %c",&isTrue);
if(isTrue == 'y' || isTrue == 'Y'){
fprintf(fptr, "the food you buy as much : %d\n", b);
fprintf(fptr, "total price of food : %d\n", f);

fclose(fptr);
goto cashier;

}

我应该怎么做才能修复这个程序?

我想要这个 fprintf(fptr, "the food you buy as much : %d\n", b); 将执行并打印它:"write the food you buy as much" 在我的记事本中。也在我的记事本文件中。 txt 我之前没有写任何东西,而且我之前也没有创建这个 file.txt

标签: cio

解决方案


推荐阅读