File handling help

This is a discussion on File handling help within the C Programming forums, part of the General Programming Boards category; Help i have a small problem i cant display the data written to this file in a switch statement (case ...

  1. #1
    Registered User
    Join Date
    Nov 2012
    Posts
    8

    File handling help

    Help i have a small problem i cant display the data written to this file in a switch statement (case 2) what am i doing wrong..the file data is being written into the text file but i cant display it


    insert
    Code:
     #include <stdio.h>
    #include <conio.h>
    #include <stdlib.h>
    
    FILE *fp;                     //creates a file pointer
    
    typedef struct date_info
    {
      int day;
      char month[12];
      int year;
    } DATE;
    
    DATE get_date(void);                                       //fucntion to input date info
    
    int open_file(FILE **fp);                                  //function to open a file
    void write_file(DATE );                                    //writes data to file
    void get_file (DATE  );                                    //Gets data within file
    int read_file(FILE **fp);                                  //Reads information contained in main file
    
    int menu();
    
    int main()
    {
       DATE date;                                              //Structure data type and variable declared
       int input;
       input = menu();                                       //Menu displayed and choice recived
    
      switch(input)
       {
       case 1:
          system("cls");
    
          printf("\n Enter the date please !! ");
          open_file(&fp);
    
          date = get_date();
          write_file(date);
    
          printf("\n File Written");
          fclose(fp);
       break;
    
    case 2:
    
           read_file(&fp);
           get_file(date);
    
    
           printf("\n\n");
           printf("\n -----------------------------------------");
           printf("\n generated:%d-%s-%d",date.day,date.month,date.year);
           printf("\n -----------------------------------------");
       break;
    
    
       case 3:
           system("cls");
           printf("\nPress [Enter] To exit the program ");
           getch();
           return 0;
       break;
    
    
       default :
           printf("\nError wrong input bye !! ");
    
    
       break;
      }//End of CASE
    
    
    getch();
    }
    
    
    /*FUNCTION DEFINITIONS */
    
    
    int menu()
    {
         int choice;
    
    
         printf("\n Welcome ");
         printf("\n -------------------------------");
    
    
         printf("\n Press  [1] to Enter the date");
         printf("\n Press  [2] to Display the date");
         printf("\n Press  [3] to Exit");
         printf("\n\n Enter Enter your choice : ");
         scanf("%d",&choice);
    
    
         return choice;
    }
    
    
    
    
    DATE get_date(void)
    {
        DATE a;
    
    
        printf("\n Enter the day :");
        scanf("%d",&a.day);
    
    
        printf(" Enter the month:");
        scanf("%s", a.month);
    
    
        printf(" Enter the year :");
        scanf("%d",&a.year);
    
    
        return a;
    }
    
    
    int open_file(FILE **fp)
    {
        *fp    = fopen("Test.txt","w");
        return 0;
    }
    
    
    
    
    int read_file(FILE **fp)
    {
        *fp  = fopen("Test.txt","r");
        return 0;
    }
    
    
    
    
    void write_file(DATE a )
    {
        fprintf(fp ,"  %d %s %d",a.day,a.month,a.year);
    }
    
    
    void get_file (DATE a )
    {
        fscanf(fp ,"%d %s %d",&a.day,a.month,&a.year);
    }
    Last edited by madjm; 03-18-2013 at 02:32 PM.

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    At the end of your data being written to the file, add a newline. Without the \n on the end, the data will be crunched up between one entry and the next. If still no luck, post the data from the data file, after it's been written a few times, so we can see the format more clearly.

  3. #3
    Registered User
    Join Date
    Nov 2012
    Posts
    8
    Quote Originally Posted by Adak View Post
    At the end of your data being written to the file, add a newline. Without the \n on the end, the data will be crunched up between one entry and the next. If still no luck, post the data from the data file, after it's been written a few times, so we can see the format more clearly.
    did that but the output was the initialization of the structure i modified at line 25 ie
    Code:
      DATE date = {0 ," ",0};
    ...the main problem it seems is the "get_file" function that uses fscanf when i put this ..

    insert
    Code:
    fscanf(fp ,"%d %s %d",&date.day,date.month,&date.year);
    the output is perfect so it seems to be that "get_file" fucntion that is causing the problem and i dont know why
    Last edited by madjm; 03-18-2013 at 04:04 PM.

  4. #4
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    One thing I don't like is the set up for your fp - it's global, BUT you pass around the address of it, as a parameter, like it was local. That can cause hard to debug problems, where the global variable is "covered up" by the local copy inside the function, and never gets the new value.

    Delete those parameters for fp, and that way, when you change the fp address anew, the global version of the fp, will receive it.

  5. #5
    Registered User
    Join Date
    Nov 2012
    Posts
    8
    Quote Originally Posted by Adak View Post
    One thing I don't like is the set up for your fp - it's global, BUT you pass around the address of it, as a parameter, like it was local. That can cause hard to debug problems, where the global variable is "covered up" by the local copy inside the function, and never gets the new value.

    Delete those parameters for fp, and that way, when you change the fp address anew, the global version of the fp, will receive it.

    Here is where i am so far i removed the parameters the output is still giving me the initialization of the DATE struct when i use the get_file function idk,


    Code:
     
    
    #include <stdio.h>
    #include <conio.h>
    #include <stdlib.h>
    
    
    FILE *fp;                                                  //creates a file pointer
    
    
    typedef struct date_info
    {
      int day;
      char month[12];
      int year;
    } DATE;
    
    
    DATE get_date(void);                                       //fucntion to input date info
                           
    int open_file();                                           //function to open a file
    void write_file(DATE );                                    //writes data to file
    void get_file (DATE );
    int read_file();                                           //Reads information contained in main file
    
    
    int menu();
    
    
    int main()
    {
       DATE date ={0," ",0};                                  //Structure data type and variable declared
       int input;
       input = menu();                                         //Menu displayed and choice recived
    
    
       switch(input)
       {
       case 1:
          system("cls");
    
    
          printf("\n Enter the date fool !! ");
          open_file();
    
    
          date = get_date();
          write_file(date);
           
          printf("\n File Written");
          fclose(fp);
       break;
    
    
       case 2:
          
           read_file();
           get_file(date);
           
           printf("\n\n");
           printf("\n -----------------------------------------");
           printf("\n generated:%d-%s-%d",date.day,date.month,date.year);
           printf("\n -----------------------------------------");
       break;
    
    
       case 3:
           system("cls");
           printf("\nPress [Enter] To exit the program ");
           getch();
           return 0;
       break;
    
    
       default :
           printf("\nError wrong input bye !! ");
    
    
       break;
      }//End of CASE
    
    
    getch();
    }
    
    
    /*FUNCTION DEFINITIONS */
    
    
    int menu()
    {
         int choice;
    
    
         printf("\n Welcome ");
         printf("\n -------------------------------");
    
    
         printf("\n Press  [1] to Enter the date");
         printf("\n Press  [2] to Display the date");
         printf("\n Press  [3] to Exit");
         printf("\n\n Enter Enter your choice : ");
         scanf("%d",&choice);
    
    
         return choice;
    }
    
    
    
    
    DATE get_date(void)
    {
        DATE a;
    
    
        printf("\n Enter the day :");
        scanf("%d",&a.day);
    
    
        printf(" Enter the month:");
        scanf("%s", a.month);
    
    
        printf(" Enter the year :");
        scanf("%d",&a.year);
    
    
        return a;
    }
    
    
    int open_file()
    {
        fp    = fopen("Test.txt","w");
        return 0;
    }
    
    
    int read_file()
    {
        fp  = fopen("Test.txt","r");
        return 0;
    }
    
    
    void write_file(DATE a )
    {
        fprintf(fp ,"%d %s %d\n",a.day,a.month,a.year);
    }
    
    
    void get_file (DATE a)
    {
        fscanf(fp ,"%d %s %d",&a.day,a.month,&a.year);
    }

  6. #6
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    In your open_file function, the file content is being overwritten/destroyed, every time you open it. If you want to keep the previous entry, open the file in "a" mode (append), whenever you want to write out data.

    Reading data is OK, and yes, the data is in the file - but not for long.

  7. #7
    Registered User
    Join Date
    Nov 2012
    Posts
    8
    Quote Originally Posted by Adak View Post
    In your open_file function, the file content is being overwritten/destroyed, every time you open it. If you want to keep the previous entry, open the file in "a" mode (append), whenever you want to write out data.

    Reading data is OK, and yes, the data is in the file - but not for long.

    maybe i should have explained more but that is what i want the program to do....it gives the user the option of creating and storing the date entered and the next option of simply displaying the date...it is the basic structure of a number generate and display program

  8. #8
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Yeah, I didn't catch that was your intention. Study this:

    Code:
    #include <stdio.h>
    #include <conio.h>
    #include <stdlib.h>
     
     
    FILE *fp;                                                  //creates a file pointer
     
     
    typedef struct date_info
    {
      int day;
      char month[12];
      int year;
    } DATE;
     
     
    DATE get_date(void);                                       //fucntion to input date info
                            
    int open_file();                                           //function to open a file
    void write_file(DATE );                                    //writes data to file
    DATE get_file (DATE );
    int read_file();                                           //Reads information contained in main file
     
     
    int menu(void);
     
     
    int main()
    {
       DATE date ={0," ",0};                                  //Structure data type and variable declared
       int input;
       input = menu();                                         //Menu displayed and choice recived
     
     
       switch(input)
       {
       case 1:
          //system("cls");
     
     
          printf("\n Enter the date fool !! ");
          open_file();
     
     
          date = get_date();
          write_file(date);
            
          printf("\n File Written");
          fclose(fp);
          //return 0;
       break;
     
     
       case 2:
           
           read_file();
           date=get_file(date);
            
           printf("\n\n");
           printf("\n -----------------------------------------");
           printf("\n generated:%d-%s-%d",date.day,date.month,date.year);
           printf("\n -----------------------------------------");
       break;
     
     
       case 3:
           system("cls");
           printf("\nPress [Enter] To exit the program ");
           //getch();
           return 0;
       break;
     
     
       default :
           printf("\nError wrong input bye !! ");
     
     
       break;
      }//End of CASE
     
     
    //getch();
    }
     
     
    /*FUNCTION DEFINITIONS */
     
     
    int menu(void)
    {
         int choice;
     
     
         printf("\n Welcome ");
         printf("\n -------------------------------");
     
     
         printf("\n Press  [1] to Enter the date");
         printf("\n Press  [2] to Display the date");
         printf("\n Press  [3] to Exit");
         printf("\n\n Enter Enter your choice : ");
         scanf("%d",&choice);
     
     
         return choice;
    }
     
     
     
     
    DATE get_date(void)
    {
        DATE a;
     
     
        printf("\n Enter the day :");
        scanf("%d",&a.day);
     
     
        printf(" Enter the month:");
        scanf("%s", a.month);
     
     
        printf(" Enter the year :");
        scanf("%d",&a.year);
     
     
        return a;
    }
     
     
    int open_file(void)
    {
       printf("destroying data\n"); 
       fp    = fopen("Test.txt","w");
        return 0;
    }
     
     
    int read_file(void)
    {
        fp  = fopen("Test.txt","r");
        return 0;
    }
     
     
    void write_file(DATE a )
    {
        fprintf(fp ,"%d %s %d\n",a.day,a.month,a.year);
    }
     
     
    DATE get_file (DATE a)
    {
       int n; 
       n=fscanf(fp ,"%d %s %d",&a.day,a.month,&a.year);
       printf("n: %d\n",n);
    
       return a;
    }

  9. #9
    Registered User
    Join Date
    Nov 2012
    Posts
    8
    wow thanks man..it was really simple and nooby mistake on my part lol

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. file handling
    By Aash in forum C Programming
    Replies: 3
    Last Post: 06-10-2010, 10:12 AM
  2. File Handling -Read write and edit a file
    By aprop in forum C Programming
    Replies: 3
    Last Post: 02-27-2010, 01:01 PM
  3. file handling
    By Chalmers86 in forum C Programming
    Replies: 1
    Last Post: 04-07-2009, 02:33 PM
  4. file handling in C
    By cutelucks in forum C Programming
    Replies: 6
    Last Post: 04-13-2007, 11:35 AM

Tags for this Thread


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