Reading a file and alocating memory

This is a discussion on Reading a file and alocating memory within the C Programming forums, part of the General Programming Boards category; i have a structure: Code: typedef struct intrare //variabilele din in1.txt { char *nume; double val; } intrare; and i ...

  1. #1
    Registered User
    Join Date
    Dec 2005
    Posts
    167

    Reading a file and alocating memory

    i have a structure:
    Code:
    typedef struct intrare //variabilele din in1.txt
    {
            char *nume;
            double val;
    } intrare;
    and i am trying to read the following type of file
    Code:
    a=42 b=432 test=-423.2154
    the function read is:

    Code:
    intrare *citire_var(FILE *f,int *n)
    {
            intrare *in=calloc(1,sizeof(intrare));
            void *t;
            char c[1];
            int j=0;
    
            *n=0;
            while((c[0]=fgetc(f))!='\n')
            {
                    if((c[0]!=' ')&&(c[0]!='='))
                    {
                            j++;
                            in[*n].nume=realloc(in[*n].nume,j*sizeof(char));
                            strncat(in[*n].nume,c,1);
                    }
                    if(c[0]=='=') fscanf(f,"%lf",&in[*n].val);
                    if(c[0]==' ')
                    {
                            j=0;
                            (*n)++;
                            printf("%d",*n);
                            t=realloc(in,(*n+1)*sizeof(intrare));
                            printf("<-\n");
                            if(t!=NULL) in=t;
                            else printf("Error\n");
                    }
            }
            (*n)++;
    
            return in;
    }
    the problem is that after some steps it segm faults me.
    please help!

    thank you!

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,484
    > in[*n].nume=realloc(in[*n].nume,j*sizeof(char));
    You're not using a temporary pointer, like I showed you in your previous post.
    Though you do the right thing later on.

    > strncat(in[*n].nume,c,1);
    1. c isn't a proper string, it has no \0
    2. nume isn't guaranteed to be a proper string either following realloc
    3. you don't allow space for a \0
    4. strncat doesn't guarantee that the result will have a \0 either.
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User
    Join Date
    Dec 2005
    Posts
    167
    i've made some changes but it still doesn't work. just ignore the first reallocation. i know that i will have to modify so i will not lose my data. it still segm faults me!
    Code:
    intrare *citire_var(FILE *f,int *n)
    {
            intrare *in=calloc(1,sizeof(intrare));
            void *t;
            char c;
            int j=0;
    
            *n=0;
            while((c=fgetc(f))!='\n')
            {
                    if(c!='=')
                    {
                            j++;
                            in[*n].nume=realloc(in[*n].nume,(j+1)*sizeof(char));
                            in[*n].nume[j-1]=c;
                    }
                    if(c=='=')
                    {
                            in[*n].nume[j]=0;
                            fscanf(f,"%lf",&in[*n].val);
                            c=fgetc(f);
                            if(c=='\n') break;
                    }
                    if(c==' ')
                    {
                            j=0;
                            (*n)++;
                            printf("%d",*n);
                            t=realloc(in,(*n+1)*sizeof(intrare));
                            printf("<-\n");
                            if(t!=NULL) in=t;
                            else printf("Eroare la alocare\n");
                    }
            }
            (*n)++;
            return in;
    }
    thank you for answering!

  4. #4
    Registered User
    Join Date
    Feb 2006
    Posts
    155
    >in[*n].nume=realloc(in[*n].nume,(j+1)*sizeof(char));

  5. #5
    Registered User
    Join Date
    Dec 2005
    Posts
    167
    nope.. it segm fault here:
    > t=realloc(in,(*n+1)*sizeof(intrare));

  6. #6
    Registered User
    Join Date
    Feb 2006
    Posts
    155
    Code:
    intrare *citire_var(FILE *f,int *n)
    {
            intrare *in=(intrare*)calloc(1,sizeof(intrare));
            void *t,*t1=NULL;
            char c[1];
    
    
            int j=0;
     
    
            *n=0;
            while((c[0]=fgetc(f))!='\n')
            {
               
                 
                    if((c[0]!=' ')&&(c[0]!='='))
                    {   
                            j++;
                           
                               t1=realloc(t1,j*sizeof(char));
                           
                            
                           
                           strncat(t1,c,1);
                             in[*n].nume=t1;
                           
                    }
                    if(c[0]=='=') fscanf(f,"%d",&in[*n].val);
                   if(c[0]==' ') 
                    {printf("d");
                          
                            j=0;
                            (*n)++;
                            printf("%d",*n);
                            t=(void*)realloc((void*)in,(*n+1)*sizeof(intrare));
                            printf("<-\n");
                            if(t!=NULL) in=t;
                            else printf("Error\n");
                           t1=NULL;
                            
                    }
             }
    
            (*n)++;
    
            return in;
    }
    Last edited by qqqqxxxx; 04-20-2006 at 06:38 AM.

Popular pages Recent additions subscribe to a feed

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