0

I have many objects saved in a file as the following form:

{
 "name":"name1",
 "city":"city1",
 "phone":"125698745663" 
},

I have a file that content objects as these last, So I would write a function that take as parameter a file, then give us as an output a list of strings, every string is an object of the file, for example name1 city1 125698745663. Actually I found that a little hard and I have been searching many days for that, I don't want to use libraries already exist cause I have read many documentation but they was difficult to understand for me. I tried to write some functions. That's my essay:

#include<stdlib.h>
/*               
{
     "name":"name1",
     "city":"city1",
     "phone":"125698745663" 
}
/* Boxes of my list */
typedef_struct box
{
   char* current_string;
   box* next;
}box;   
/* Define the structure of the list that I'll return after*/
typedef_struct list
{
   box* beginning;
}list;
/*The function that return the list of the objects that existed in our files*/
**char lire (FILE* my_file) //It is possibly to put the path of the file as an argument
{
      char* nameFile;
      printf("enter the name of the file");
      scanf("%s", &nameFile);
      my_file = fopen( "nameFile","r");
      if (my_file != NULL)
      {
        box* temp;
        int i=0;
        list* my_list;
        int first_time = 0;
        /* browse file elements */
        do
        {
            char c = fgetc(my_file); 
           /* I put the following condition to get the value without its key*/
            if(c == ":")  
            {
                while(c!=",")
                {
                 temp->current_string[i] = c; 
                 i++;
                 // printf("%c",c);  
                 c=fgetc(my_file);
                }        
             }
            /*I put this condition to pass to the following element of the list and fill it*/
            if(c == "}")
            {
                first_time++; 
                /*This condition is for getting the begining of the list because it is what distinguishes the list*/
                if(first_time==1) 
                      my_list->beginning->current_string = temp->current_string;
                temp = temp->next;
            } 
         } while (c != EOF); 
        fclose(my_file);
      }
return my_list;
}```


 
Yassok
  • 1
  • 4
  • `char nameFile[200]; scanf("%199s", nameFile);` – user3386109 Jan 26 '21 at 01:27
  • @user3386109 Should I modify that? – Yassok Jan 26 '21 at 01:31
  • Yes, the code you have (`char* nameFile; scanf("%s", &nameFile);`) may appear to work for very short filenames. However, it has [undefined behavior](https://stackoverflow.com/questions/2397984). Which means that it will stop working at the most inconvenient time. – user3386109 Jan 26 '21 at 01:39

1 Answers1

0

Well, I believe that C, especially without JSON library like cjson is not the appropriate tool to do that.

You might have a look to a list of command-line tools related to JSON: https://ilya-sher.org/2018/04/10/list-of-json-tools-for-command-line/

I think the best is to download a command-line tool (pre-compiled if possible) and extract the information you need.

An example how to use jq: Get outputs from jq on a single line

You can download jq from here: https://stedolan.github.io/jq/download/

EDIT: give an example

After jq download, you can use it from the command line cmd.exe:

jq-win64.exe
jq - commandline JSON processor [version 1.6]

Usage:  jq-win64.exe [options] <jq filter> [file...]
        jq-win64.exe [options] --args <jq filter> [strings...]
        jq-win64.exe [options] --jsonargs <jq filter> [JSON_TEXTS...]
...

test.json

[{
 "name":"name1",
 "city":"city1",
 "phone":"125698745663" 
},
{
 "name":"name2",
 "city":"city2",
 "phone":"125698745663" 
}
]

jq command line:

> jq-win64.exe ".[]|.name+\",\"+.city+\",\"+.phone" test.json
"name1,city1,125698745663"
"name2,city2,125698745663"

.[] basically means that one wants to iterate over a JSON array. |.name basically means that one wants to extract name, add a , and .city and so on.

Robert
  • 2,711
  • 7
  • 15
  • Firstly, I would thank you for these informations, I appreciate that. – Yassok Jan 26 '21 at 13:53
  • But tell me please, is these links you have sent me For C language? because I checked them I could not understand the syntaxe and what does JP mean? Please if you have any source code which does the same function as I already posted or guide me to some ressources could help please, it was lots of days and I am searching for that. Anyway I would thank you again @Robert – Yassok Jan 26 '21 at 14:01
  • No this is not C programming. If I understood properly, you are facing some issues developing a program to extract data from `JSON`. One way to avoid `C` programming issues is to avoid to C programming. I suggest to use an existing program instead of developing a new one. You can download `jq` and use it from the command line. – Robert Jan 27 '21 at 01:23
  • Thank you so much @Robert, it is really appreciated. Actually this is a school project, where I have to develop a program for searching people exist in a data base with their information (name, city, age....) using Json file. And I must use C programming to develop this program. I've understood from your comment I am kinda lost. – Yassok Jan 31 '21 at 21:53