c - What makes this saved array different from the loaded array? -


i have function saves entire array ia file called 'filename' in json text file array file format:

int intarr_save_json( intarr_t* ia, const char* filename ) {     if(ia==null)     {         return 1;     }     int *arr=ia->data;     int n=ia->len;     int i;     file *p;     p=fopen(filename,"w");     if(p!=null)     {         fprintf(p,"[\n");         for(i=0;i<n;i++)         {             if(i!=n-1)               {                 fprintf(p," %d,\n",arr[i]);             }             else             {                 fprintf(p," %d\n",arr[i]);             }         }         fprintf(p,"]");         fclose(p);         return 0;     }     return 1; } 

and function loads new array file called 'filename', saved using intarr_save().

intarr_t* intarr_load_json( const char* filename ) {     file* f = fopen(filename, "r");     if (f == null) return null;      intarr_t* loaded = intarr_create(0);      int value;     //get rid of [     fscanf(f, "%c ", &value);      while (fscanf(f, "%d, ", &value)) {         if (value == ']') break;         intarr_push(loaded, value);     }       fclose(f);     return loaded; } 

it seems nice, but, reason, length of loaded array not same length of saved array. causing this?

edit: function this

int intarr_save_json( intarr_t* ia, const char* filename ) {     if(ia == null)     {         return 1;     }     int *arr=ia->data;     int n=ia->len;     int i;     file *p;     p=fopen(filename,"w");     if(p!=null)     {         fprintf(p,"[\n");         for(i=0;i<n;i++)         {             if(i!=n-1)               {                 fprintf(p," %d,\n",arr[i]);             }             else             {                 fprintf(p," %d\n",arr[i]);             }         }         fprintf(p,"]");         fclose(p);         return 0;     }     return 1; }  , intarr_t* intarr_load_json( const char* filename ) {     file* f = fopen(filename, "r");     if (f == null)     {         return null;     }      intarr_t* loaded = intarr_create(0);      int value;       char line[100];     while ( fgets(line, 100, f) )     {         if ( line[0] == ']' )            {             break;            }          if ( sscanf(line, "%d", &value) != 1 )             {               break;             }      // use number     intarr_push(loaded, value);     }      fclose(f);     return loaded; } 

but load return null pointer. causing this?

the logic used in

while (fscanf(f, "%d, ", &value)) {     if (value == ']') break; 

is flawed.

the last 2 lines of file be

<some number> ] 

the call fscanf fail @ line. hence, end reading 1 less number wrote file.

i suggest changing strategy bit.

// read contents of file line line // process each line. char line[100]; while ( fgets(line, 100, f) ) {     if ( line[0] == ']' )     {        break;     }      if ( sscanf(line, "%d", &value) != 1 )     {        break;     }      // use number     intarr_push(loaded, value); } 

Comments

Popular posts from this blog

Spring Boot + JPA + Hibernate: Unable to locate persister -

go - Golang: panic: runtime error: invalid memory address or nil pointer dereference using bufio.Scanner -

c - double free or corruption (fasttop) -