-3

i have just built a program that reads a data from a file named "text.txt" and parse it through JSON as i know the size of a file that im reading character wise so in for loop i have defined the value of i in for loop as 647, pleas help me if the value increases or decreases then how would i manage it.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include "jsmn.h"

void error(char *msg)
{
      perror(msg);
      exit(0);
}
static int jsoneq(const char *json, jsmntok_t *tok, const char *s)
{

    if (tok->type == JSMN_STRING && (int) strlen(s) == tok->end - tok->start && strncmp(json + tok->start, s, tok->end - tok->start) == 0)
    {
        return 0;
    }
    return -1;
}
int main(int argc, char const *argv[])
{ 
    int i,n,r;
    char buf[1024];
    char JSON_STRING[5000];
    jsmn_parser p;
    jsmntok_t t[2048];
    char * fname;
    FILE *fp=fopen("text.txt","r+");
    FILE *ff;
    if(fp==NULL)
    {
        error("file opening error");
    }
    for(int i=0; i<647;i++)   /////////this if the for loop //////////
    {
        JSON_STRING[i] = getc(fp);
    }
    jsmn_init(&p);
    r = jsmn_parse(&p,JSON_STRING,  strlen(JSON_STRING), t,     sizeof(t)/sizeof(t[0]));
    if (r < 0){
        printf("\nFailed to parse JSON: %d\n", r);
        return 1;
    }
    else{
        for (i = 1; i < r; i++){
            if (jsoneq(JSON_STRING, &t[i], "RID") == 0)
            // for extracting the value of Rid from the complete string 
            {/* We may use strndup() to fetch string value */
            //printf("RID: '%.*s'\n",t[i+1].end - t[i+1].start, JSON_STRING+ t[i+1].start);
            printf("- RID: %.*s\n", t[i+1].end-t[i+1].start,JSON_STRING + t[i+1].start);
            sprintf(fname,"%.*s",t[i+1].end - t[i+1].start, JSON_STRING+ t[i+1].start);
            ff=fopen (fname, "w");
            fprintf(ff, "RID: '%.*s'\n",t[i+1].end - t[i+1].start, JSON_STRING+ t[i+1].start );
            i++;
        }
    }
    else if (jsoneq(JSON_STRING, &t[i], "DID") == 0)    //for Extracting the value of DID from te string recived from the client JSON_STrING
    {
        /* We may additionally check if the value is either "true" or "false"*/
        printf("- DID: %.*s\n", t[i+1].end-t[i+1].start,JSON_STRING + t[i+1].start);
        fprintf(ff, "DID: '%.*s'\n",t[i+1].end - t[i+1].start, JSON_STRING+ t[i+1].start );
        i++;
        //sprintf(fname,"%s_%.*s",fname,t[i+1].end - t[i+1].start, JSON_STRING+ t[i+1].start); 
    }
    else if (jsoneq(JSON_STRING, &t[i],"TS") == 0)
    {
        /* We may want to do strtol() here to get numeric value */
        printf("- TS: %.*s\n", t[i+1].end-t[i+1].start,JSON_STRING + t[i+1].start);
        fprintf(ff, "TS: '%.*s'\n",t[i+1].end - t[i+1].start, JSON_STRING+ t[i+1].start );
         // sprintf(fname,"%s_%.*s",fname,t[i+1].end - t[i+1].start, JSON_STRING+ t[i+1].start);

    }
    /*  else
    {
    printf("Unexpected key: %.*s\n", t[i].end-t[i].start,JSON_STRING + t[i].start);}*/
    }
        printf("JSON parsed : %d\n", r);
        printf("output have been generated to the file");
        fprintf(ff, "%s\n",JSON_STRING );
        fclose(ff);
    }
    return EXIT_SUCCESS;
    fclose(fp);
    return 0;
}
MD. Khairul Basar
  • 4,976
  • 14
  • 41
  • 59
numer
  • 3
  • 2
  • 1
    Please indent your code. Also, see http://stackoverflow.com/a/26557243/669576. There are a few examples there. – 001 Sep 02 '16 at 11:44
  • @numer please spare the readers of unrelated code and comments, because they make your post more difficult to read, ie less likely to get answered. – UmNyobe Sep 02 '16 at 13:52

1 Answers1

0

You need first to compute the amount of characters in the file, which will be the size of your json buffer

 //computing how much data is needed
fseek(fp, 0L, SEEK_END);
long int json_size = ftell(fp);
rewind(fp);

Then read the file till you reach the end of the buffer or till the value read is EOF. With the code above both these conditions are the same, but under other conditions you must use both tests. Which give

JSON_STRING = (char*)malloc(json_size+1 * sizeof(char));
JSON_STRING[json_size] = '\0';
//reading the value
int val = 0;
int i = 0;
while(i < json_size && (val = getc(fp)) != EOF)
{
   JSON_STRING[i] = val;
   ++i;
}
Community
  • 1
  • 1
UmNyobe
  • 22,539
  • 9
  • 61
  • 90