-1

I have a kindof complicated file format:

{
      "color": [
        45, 
        200, 
        34
      ], 
      "docnum": 5183, 
      "form": "avoir", 
      "graph": "jdm.N.flat", 
      "id": 0, 
      "lang": "fr", 
      "neighbors": 17, 
      "pos": "N", 
      "pzero": true, 
      "rank": 1, 
      "score": 0.0028284271, 
      "type": 1
    }, 
    {
      "color": [
        45, 
        200, 
        34
      ], 
      "docnum": 22809, 
      "form": "argent", 
      "graph": "jdm.N.flat", 
      "id": 1, 
      "lang": "fr", 
      "neighbors": 65, 
      "pos": "N", 
      "pzero": false, 
      "rank": 2, 
      "score": 0.0028284271, 
      "type": 1
    }, 

This kind of list goes on, for hundreds of entries. I would like to read in the variable numbers, and a string (docnum, form, id, neighbors, rank, score) so i created a format string for this kind of input:

     main(){

     FILE* in=fopen("fr.N.bien2","r");
     int maxwords=100;
     int maxstringlen=100;

     char* nodes=malloc(sizeof(char)*maxstringlen*maxwords);
     if(!nodes){printf("Nodes couldn't be allocated!\n");}

     int i=0;

     int* IDs=malloc(sizeof(int)*3*maxwords);
     int docnum,nei,rank;
     float score;
     char* pzero;
     while(fscanf(in," { \"color\": [ 45, 200, 34 ], \"docnum\": %i,      \"form\": %s \"graph\": \"jdm.N.flat\", \"id\": %i , \"lang\": \"fr\",  \"neighbors\": %i , \"pos\": \"N\", \"pzero\": false, \"rank\": %i , \"score\": %f , \"type\" :1  },",&docnum,&nodes[i],&IDs[i],&nei,pzero,&rank,&score))
    {
         printf("node:%s,ID=%i\n",&nodes[i],IDs[i]);
         i++;
   }
   }

It looks complicated, but it seems to be working, because I get the first instance right. The output is:

>>node:"avoir",,ID=0

However, output stops at that, even though the format is exactly repeating in the file (as you can see in the sample). Am I missing something important here? Is there an easier way to read in this kind of data from such a file?

1 Answers1

1

And this complicated format is JSON

As the comments suggest, look for a library instead (they can be found following the previous link) there's many libraries and for different languages as well.

In fact, your question might be already answered here

Community
  • 1
  • 1
Eekan
  • 71
  • 1
  • 9