Thread: I have some problems reading struc from file

  1. #1
    Registered User
    Join Date
    Dec 2003
    Posts
    10

    I have some problems reading struc from file

    The program is working except for few problems:


    1. If I initialize the cust_name with space it will not display apt_id for other items when I run the program next time but if I initialize it with any character it will be ok.

    2. When I add a cust_name with more than 4 character it will add to it some strange characters.


    3. How can I pause to let the user see some data when he select an option.

    4. When I enter an option not in the menu the menu is then displayed more then once.

    Any other hints that let me improve my program is appreciated.

    Thank you.


    Code:
    /*A simple program that maintain apartment reservation
     and it use struct and array and it save data in a text file  */
    
    #include <stdio.h>
    
    /* num_lines is the number of screen display lines */
    #define num_lines	25
    
    struct appts
    {
          int apt_id;
          char vacant;
          char floor;
          char *cust_name;
          int no_of_days;
    };
    
    struct appts apt[20];
    
    
    /* Function prototypes */
    
    void CLS(void);
    void DISP_DATA(void);
    void GET_DATA(void);
    void SAVE_DATA(void);
    void LIST_VACNT(void);
    void RESERV_APT(void);
    void DEL_RESRV(void);
    void MENU(void);
    
    /*****************************************************************************/
    
    void CLS()
    {
    	int n;
    
    	for(n = 0; n < num_lines; n++)
    		puts("");
    
    }
    
    /*****************************************************************************/
    void GET_DATA()
    {
        FILE *fp;
        int i;
    
    	CLS();
    
        fp=fopen("txtfile.txt","r");
    
        if (fp == NULL)
    
             for (i=0;i<20;i++)
             {
                 apt[i].apt_id = i + 100;
                 apt[i].vacant = 'n';
                 apt[i].floor= '1';
                 apt[i].cust_name=' ';
                 apt[i].no_of_days=0;
             }
    
        else
        {
              for (i=0;i<20;i++)
              {
                  fscanf(fp,"%d\n",&apt[i].apt_id);
                  fscanf(fp,"%c\n",&apt[i].vacant);
                  fscanf(fp,"%c\n",&apt[i].floor);
                  fscanf(fp,"%s\n",&apt[i].cust_name);
                  fscanf(fp,"%d\n",&apt[i].no_of_days);
              }
    
              fclose(fp);
         }
    
    }
    /*****************************************************************************/
    void DISP_DATA()
    {
    	/* Displays data */
     
     int i;
     char text[5];
    
    
    	    CLS();
    
            for (i=0;i<20;i++)
            {
           printf("\n-------------------------------------------------------\n");
           printf("%d",apt[i].apt_id);
           printf("%c",apt[i].vacant);
           printf("%c",apt[i].floor);
           printf("%s",&apt[i].cust_name);
           printf("%d",apt[i].no_of_days);
    
            }
    }
    /*****************************************************************************/
    void LIST_VACNT()
    {
        int i;
    
        CLS();
    
    
        for(i=0;i<20;i++)
        {
            if (apt[i].vacant == 'n')
                printf("\nvacant ===> %d",apt[i].apt_id);
        }
    
    }
    /*****************************************************************************/
    void RESERV_APT()
    {
        int x,y;
    
    
        CLS();
    
        printf("\nEnter aparment no");
    
        scanf("%d",&y);
    
        x=y-100;
    
        if (apt[x].vacant=='n')
        {
            apt[x].vacant='y';
    
            printf("\nEnter name");
            scanf("%s",&apt[x].cust_name);
    
            apt[x].no_of_days = 5;
         }
         else
             printf("this apartment is reserved!!!!!!!");
    
    }
    /*****************************************************************************/
    void DEL_RESRV()
    {
    
      int i,x,y;
      char name[20];
    
        CLS();
    
        printf("\nEnter apt no");
    
        scanf("%d",&y);
    
        x=y-100;
    
        if (apt[x].vacant=='y')
        {
            apt[x].vacant='n';
    
            apt[x].cust_name= ' ';
    
            apt[x].no_of_days= 0;
        }
         else
             printf("this apt is not reserved!!!!!!!");
    
    
    
    }
    /*****************************************************************************/
    void MENU()
    {
        char option;
    	char text[10];
    
     do
    	{
    		CLS();
    
    
            if (option == 'a')
    
               DISP_DATA();
    
            else if (option == 'b')
                     LIST_VACNT();
    
                 else if (option == 'c')
                          RESERV_APT();
    
                      else if (option == 'd')
                               DEL_RESRV();
                           else
                           {
                                printf("\n\t\t\tSelect Option");
    	                        printf("\n\n\t\t\t a) List all apartments");
                    		    printf("\n\n\t\t\t b) List vacant apartments");
    	                	    printf("\n\n\t\t\t c) Reserve apartment");
                                printf("\n\n\t\t\t d) Delete apartment reservation");
                                printf("\n\n\t\t\t x) Exit");
    	                 	    printf("\n");
                           }
    
    		scanf("%c",&option);
     
    
    	}
    	while(option != 'x');
    }
    /*****************************************************************************/
    void SAVE_DATA()
    {
    
       FILE *fp;
    
       int i;
    
       fp= fopen("txtfile.txt","w");
    
       for (i=0;i<20;i++)
       {
           fprintf(fp,"%d\n",apt[i].apt_id);
           fprintf(fp,"%c\n",apt[i].vacant);
           fprintf(fp,"%c\n",apt[i].floor);
           fprintf(fp,"%s\n",&apt[i].cust_name);
           fprintf(fp,"%d\n",apt[i].no_of_days);
       }
    
    
        fclose(fp);
    }
    /*****************************************************************************/
    void main()
    {
    	CLS();
        GET_DATA();
    	MENU();
        SAVE_DATA();
    }

  2. #2
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >>char *cust_name;
    That's a pointer... where is it pointing to? Answer that and you'll be part way there (Hint: [])
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  3. #3
    Registered User
    Join Date
    Dec 2003
    Posts
    10
    I changed the *cust_name to cust_name[20] in the struc but still
    there is a problem.

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > 1. If I initialize the cust_name with space it will not display apt_id for other items
    spaces are significant to fscanf(), it acts as a field separator
    This means all your fields which follow will be wrong


    > 4. When I enter an option not in the menu the menu is then displayed more then once.
    You're using scanf()
    Read the FAQ to find out better ways of reading input
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Registered User
    Join Date
    Dec 2003
    Posts
    10
    The program is ok now. Thank you.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A development process
    By Noir in forum C Programming
    Replies: 37
    Last Post: 07-10-2011, 10:39 PM
  2. Newbie homework help
    By fossage in forum C Programming
    Replies: 3
    Last Post: 04-30-2009, 04:27 PM
  3. Formatting the contents of a text file
    By dagorsul in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2008, 12:36 PM
  4. Basic text file encoder
    By Abda92 in forum C Programming
    Replies: 15
    Last Post: 05-22-2007, 01:19 PM
  5. help with text input
    By Alphawaves in forum C Programming
    Replies: 8
    Last Post: 04-08-2007, 04:54 PM