How to check if a file exist , if it does read its content to another file.

This is a discussion on How to check if a file exist , if it does read its content to another file. within the C Programming forums, part of the General Programming Boards category; Hey, I have this assignment for next week that requires me to write a program in c that prompts a ...

  1. #1
    Registered User
    Join Date
    Apr 2012
    Posts
    2

    Post How to check if a file exist , if it does read its content to another file.

    Hey, I have this assignment for next week that requires me to write a program in c that prompts a user for a filename, if the file exist then tell the user that it exist and read the information in that file to another file called farmreport.dat, else if it does not exist create it from scratch and prompt the user for information and that information is to be sent to farmreport.dat

    The name of the file that the user will enter is cropdata.dat, in this file there is the person's first name and last name on the first line and the quantity and prices of the foods they grew. Eg.

    John Brown
    100 2.30 300 2.00 450 1.30


    Can you help me out.
    So far I have written code to create the file from scratch but the information is not being saved in the file and the file is not ending. To save the info I have to enter Ctrl + Z . I cant figure out the code for if the file exist, tell the user it does and read the information into farmreport.dat.


    Here is the code
    Code:
     
    Code:
    #include <stdio.h>
    #include <string.h>
    
    
    
    
    int main (void)
    {
    
    
        char filename[90];
        int num;
    
    
    int  amount1, amount2, amount3, amount4, amount5, amount6, amount7, amount8, amount9, amount10 =0;
        float   price1 , price2, price3, price4, price5, price6, price7, price8, price9, price10 = 0.0;
        char fname[20];
        char lname[20];
        int count=1;
        float revenue1, revenue2, revenue3, revenue4, revenue5, revenue6, revenue7, revenue8 , revenue9, revenue10 , total_revenue = 0.0;
        int maxpumpkins, maxonions, maxsweetpotatoes, maxyams, maxlettuce, maxgreenpeppers, maxtomatoes, maxcucumbers, maxbananas, maxgarlic =0;
    
    
        int totalpumpkins=0;
        int totalonions =0;
        int totalsweetpotatoes =0;
        int totalyams=0;
        int totallettuce=0;
        int totalgreenpeppers=0;
        int totaltomatoes=0;
        int totalcucumbers=0;
        int totalbananas=0;
        int totalgarlic = 0;
        int importpumpkins, importonions, importsweetpotatoes, importyams, importlettuce, importgreenpeppers, importtomatoes, importcucumbers, importbananas, importgarlic= 0;
        int surpluspumpkins, surplusonions, surplussweetpotatoes, surplusyams, surpluslettuce, surplusgreenpeppers, surplustomatoes, surpluscucumbers, surplusbananas, surplusgarlic=0;
    
    
         char answer;
         char response;
    
    
        FILE *fp1;
        
    
    
        printf(" Enter filename \n");
        scanf("%s" , filename);
    
    
        
    
    
        if ( ( fp1 = fopen (filename, "w")) == NULL) 
        {
            printf(" File could not be opened\n");
        } // end if
    
    
        else {
            
    
    
            printf(" Please enter farmer's first and last name \n ");
            
            scanf("%s%s", fname, lname);
        
             printf(" Please enter the amount of pumpkins \n ");
            scanf("%d", &amount1);
            printf("Now kindly state the price of pumpkins per pound\n ");
            scanf("%f", &price1);
            //fflush(stdin);
    
    
              printf(" Please enter the amount of onions \n ");
            scanf("%d", &amount2);
            printf("Now kindly state the price of onions per pound\n ");
            scanf("%f", &price2);
    
    
    
    
            printf(" Please enter the amount of sweet potatoes \n ");
            scanf("%d", &amount3);
            printf("Now kindly state the price of sweet potatoes per pound \n ");
            scanf("%f", &price3);
     
    
    
            printf(" Please enter the amount of yams \n ");
            scanf("%d", &amount4);
            printf("Now kindly state the price of yams per pound\n ");
            scanf("%f", &price4);
    
    
            printf(" Please enter the amount of lettuce \n ");
            scanf("%d", &amount5);
            printf("Now kindly state the price of lettuce per pound\n ");
            scanf("%f", &price5);
    
    
            printf(" Please enter the amount of greenpeppers \n ");
            scanf("%d", &amount6);
            printf("Now kindly state the price of greenpeppers per pound\n ");
            scanf("%f", &price6);
    
    
            printf(" Please enter the amount of tomatoes \n ");
            scanf("%d", &amount7);
            printf("Now kindly state the price of tomatoes per pound\n ");
            scanf("%f", &price7);
    
    
            printf(" Please enter the amount of cucumbers \n ");
            scanf("%d", &amount8);
            printf("Now kindly state the price of cucumbers per pound\n ");
            scanf("%f", &price8);
    
    
            printf(" Please enter the amount of bananas \n ");
            scanf("%d", &amount9);
            printf("Now kindly state the price of bananas per pound\n ");
            scanf("%f", &price9);
    
    
    
    
    
    
            printf(" Please enter the amount of garlic \n ");
            scanf("%d", &amount10);
            printf("Now kindly state the price of garlic per pound\n ");
            scanf("%f", &price10);  
    
    
            while (!feof (fp1)) {
            
                 fprintf(fp1, "%s %s \n", fname, lname);
                 scanf("%s%s", fname , lname);
    
    
                 fprintf( fp1 , "%d %f \n" , amount1 , price1);
                // scanf("%d%f", &amount1, &price1);
    
    
                 fprintf( fp1 , "%d %f \n" , amount2 , price2);
                // scanf("%d%f", &amount2, &price2);
    
    
                 fprintf( fp1 , "%d %f \n" , amount3 , price3);
                 scanf("%d%f", &amount3, &price3);
    
    
    
    
                 fprintf( fp1 , "%d %f \n" , amount4 , price4);
                 scanf("%d%f", &amount4, &price4);
    
    
    
    
                 fprintf( fp1 , "%d %f \n" , amount5 , price5);
                 scanf("%d%f", &amount5, &price5);
    
    
                 fprintf( fp1 , "%d %f \n" , amount6 , price6);
                 scanf("%d%f", &amount6, &price6);
    
    
                 fprintf( fp1 , "%d %f \n" , amount7 , price7);
                 scanf("%d%f", &amount7, &price7);
    
    
                 fprintf( fp1 , "%d %f \n" , amount8 , price8);
                 scanf("%d%f", &amount8, &price8);
    
    
                 fprintf( fp1 , "%d %f \n" , amount9 , price9);
                 scanf("%d%f", &amount9, &price9);
    
    
                 fprintf( fp1 , "%d %f \n" , amount10 , price10);
                 scanf("%d%f", &amount10, &price10); 
            
    
    
    
    
            } // endwhile
    
    
            fclose(fp1);
            printf(" \n Written Successfully in the file.\n");
        }
        return 0;
        }
    Last edited by BajanGal; 04-13-2012 at 07:18 PM.

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    If fopen fails, it doesn't exist (or at least as far as you are concerned it doesn't). If it doesn't fail, read it, then write it some place, in whatever manner you feel like.
    Code:
    try to open file
    if file opened
        while not at end of file
            read some stuff
            write some stuff
    else
        while not done
            read some stuff from user
            write some stuff
    Now I'll leave the taking the parts of that that are nearly identical and turning it into a streamline version of that as an exercise to the reader.


    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Apr 2012
    Posts
    2
    That's exactly what I was thinking. But I'm having problems formulating the code. Thanks!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 7
    Last Post: 06-16-2008, 02:18 PM
  2. Replies: 19
    Last Post: 06-16-2008, 06:00 AM
  3. read a text file and display the content into the list box
    By mr_empty in forum Windows Programming
    Replies: 10
    Last Post: 12-18-2007, 12:55 AM
  4. File I/O Read and Check
    By rabbit in forum C++ Programming
    Replies: 3
    Last Post: 02-06-2006, 09:02 PM
  5. hwo to check if a file exist in certain dir?
    By Jasonymk in forum C++ Programming
    Replies: 4
    Last Post: 03-02-2003, 08:20 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21