Thread: Need help:reading and writing structure into file

  1. #1
    Registered User
    Join Date
    Sep 2010
    Posts
    6

    Unhappy Need help:reading and writing structure into file

    This is the code
    Code:
    #include<stdio.h>
    #include<stdlib.h>
    #include<string.h>
    
    #define MAX     10
    struct module
    {
            char *m_module[MAX];
    //      int m_status[MAX];
    };
    
    int main()
    {
            FILE *fp;
            struct module m;
            int i;
    /*
            if((fp = fopen("module_info.txt","wb")) == NULL)
            {
                    printf("\nUnable to open the file!!!\n");
                    return 0;
            }
    
            for(i = 0 ; i < MAX ; i++)
            {
                    printf("\nEnter the module no %d:",i+1);
                    m.m_module[i] = (char *)malloc(sizeof(char)*10);
                    __fpurge(stdin);
                    gets(m.m_module[i]);
    //              m.m_status[i] = 0;
            }
            fwrite(&m,sizeof(struct module),1,fp);
    
    
            
            if(fclose(fp) == EOF)
            {
                    printf("\nError in closing the file!!!");
                    exit(1);
            }
    
    */
    if((fp = fopen("module_info.txt","rb")) == NULL)
            {
                    printf("\nUnable to open the file!!!\n");
                    return 0;
            }
    
    
            fread(&m,sizeof(struct module),1,fp);
            for(i = 0 ; i < MAX ; i++)
            {
                    printf("%s \n",m.m_module[i]);
            }
    
            if(fclose(fp) == EOF)
            {
                    printf("\nError in closing the file!!!");
                    exit(1);
            }
    
            return 0;
    }
    getting segmentation fault in printf("%s \n",m.m_module[i]) statement.
    what is wrong?

  2. #2
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    In your code you've already created an instance of your module as "m". There is no need to allocate more memory for it and it is an error to do so.

    To locate the correct module in your file you would use fseek() to position yourself in the file then fread() to read the correct number of bytes indicated by sizeof() into your struct.

    If you want to work with more than one struct at a time you need to declare your "m" variable as an array of structs m[MAX] then you can indext them as m[0] etc.

    You're getting a segmentation fault because you are treating a single struct as an array of structs.

  3. #3
    Registered User
    Join Date
    Aug 2010
    Posts
    231
    You should use
    Code:
    fopen("module_info.txt","w+b")
    and
    Code:
    fgets(m.m_module[i],10,stdin)
    and you must run writing and reading in the same context (without /**/).
    If you use not the same context then you should define
    Code:
    struct module
    {
          char m_module[MAX][MAX];
          int m_status[MAX];
    };
    and remove the line
    Code:
    m.m_module[i] = (char *)malloc(sizeof(char)*10);

  4. #4
    Registered User
    Join Date
    Sep 2010
    Posts
    6

    Unhappy BUT

    Thanks but
    char *m_module[MAX];
    is array of char ptr memory must be allocated for each char *

  5. #5
    Registered User
    Join Date
    Aug 2010
    Posts
    231
    I know what i write, i know the difference between array and pointer (since many years).
    Try the code variations, debug/understand it and be happy.

  6. #6
    Registered User
    Join Date
    Sep 2007
    Posts
    1,012
    Half of your code is commented out. You neglect to explain this. I assume that the commented out part is used first, to write the file; but you should take the time to explain your code when you're asking for help. If you expend a little effort in describing your problem, you will probably get better results.

    If my assumption is correct, your problem is that you're writing out pointers to your file. All you're storing in the struct is the return value of your call to malloc(). The strings themselves live elsewhere. Thus when you read the pointers back in, they're pointing to garbage. This would not be the case if you read the file during the same run that you wrote it. But as you're starting the program afresh, all the memory you allocated has disappeared.

  7. #7
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    Quote Originally Posted by BillyTKid View Post
    You should use
    Code:
    fgets(m.m_module[i],10,stdin)
    I agree with the above and I think testing the return of malloc for NULL might be needed.

    Code:
    m.m_module[i] = (char *)malloc(sizeof(char)*10);
    I think cas is right about writing out pointers being a problem.

    Tim S.
    Last edited by stahta01; 10-26-2010 at 11:49 AM. Reason: Added cas note:

  8. #8
    Registered User
    Join Date
    Sep 2010
    Posts
    6

    Smile Solved

    Thank u guys.....cas is right...thanks again

Popular pages Recent additions subscribe to a feed

Tags for this Thread