Thread: content in file do not exist in c proggram

  1. #1
    Registered User
    Join Date
    Dec 2020
    Posts
    5

    Post content in file do not exist in c proggram

    How to make a file, i try to make a file.txt in c but i got an error, the program where i write successed execute but in file or in notepad the contents do not exist.


    Code:
    this my snippet of my code what I want to input to 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;
        
        }
    what should i do to fix this program ?


    i want this fprintf(fptr, "the food you buy as much : %d\n", b);will execute and print it : "write the food you buy as much" in my notepad. but in my notepad file.txt i do not write anything before and also i do not create this file.txt name before

  2. #2
    Registered User
    Join Date
    May 2012
    Posts
    505
    The goto cashier is suspicious and is probably the root of your problems.

    rename "main" something else, and write a new temporary main.

    This just opens a file for writing and writes some text to it. Verify that this file appears and contains the text. That means that you have your system running.

    Now delete than main and reinstall your old main. Put diagnostic printf's in the code so you can see the execution flow. For some reason, it must not be gettting to the place you create your file and add text to it.
    I'm the author of MiniBasic: How to write a script interpreter and Basic Algorithms
    Visit my website for lots of associated C programming resources.
    https://github.com/MalcolmMcLean


Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 03-25-2013, 01:33 AM
  2. Replies: 1
    Last Post: 05-13-2012, 10:00 AM
  3. Replies: 2
    Last Post: 04-13-2012, 07:39 PM
  4. Replies: 7
    Last Post: 06-16-2008, 01:18 PM
  5. Proggram Help
    By this_is_me in forum C++ Programming
    Replies: 1
    Last Post: 05-23-2005, 07:54 AM

Tags for this Thread