picador
picador

Reputation: 155

Parse data format in C

I am new in C so I have never done that before. What I am doing is open a file, parse the data and print the result in another file. Assume that the file open (in the main function) was ok and now I use strtok() function to break the string in tokens.

Using the above function I get an output file like this:

{"date": "11:20:50.359", "date": "08-02-16"}

But what I would like is to get the data:

{"date": "11:20:50.359 08-02-16"}

This is my code:

void tokenize( char *oneline, FILE *fileName){

   const char delimiter[6] = DELIMITER__;
   char *token;
   int counter = 0;

   /* get the first token */
   token = strtok(oneline, delimiter);

   char time_str[50];
   char date_str[50];

   /* walk through other tokens */
   while( token != NULL ) { 

     if (counter == 1 ){
    char * strA = "\"", * strB = ":", str[50], strC[50], strD[50];
    int v = 0, x = 3, y = 6, z=13;

    strncpy(str,token,v);
    str[v] = '\0';
    strcat(str,strA);
    strcat(str,token+v);

    strncpy(strC,str,x);
    strC[x] = '\0';
    strcat(strC,strB);
    strcat(strC,str+x);

    strncpy(strD,strC,y);
    strD[y] = '\0';
    strcat(strD,strB);
    strcat(strD,strC+y);

    strncpy(time_str,strD,z);
    time_str[z] = '\0';
    strcat(time_str,strA);
    strcat(time_str,strD+z);    

        fprintf(fileName, "%s", "\"date\": ");
        fprintf(fileName,"%s, ", time_str);
     }

     if (counter == 9 ){ 
    char * strA = "\"", * strB = "-", str[50], strC[50], strD[50];
    int v = 0, x = 3, y = 6, z=9;

    strncpy(str,token,v);
    str[v] = '\0';
    strcat(str,strA);
    strcat(str,token+v);

    strncpy(strC,str,x);
    strC[x] = '\0';
    strcat(strC,strB);
    strcat(strC,str+x);

    strncpy(strD,strC,y);
    strD[y] = '\0';
    strcat(strD,strB);
    strcat(strD,strC+y);

    strncpy(date_str,strD,z);
    date_str[z] = '\0';
    strcat(date_str,strA);
    strcat(date_str,strD+z);    

        fprintf(fileName, "%s", "\"date\": ");
        fprintf(fileName,"%s", date_str);
     }

      token = strtok(NULL, delimiter);
      ++counter;
   }
}

I would be gratefull if somebody could help me.

Upvotes: 0

Views: 97

Answers (1)

BeyelerStudios
BeyelerStudios

Reputation: 4283

Parse time_str and date_str first, then print them out:

void tokenize(char *oneline, FILE *fileName) {
    const char delimiter[6] = DELIMITER__;
    char time_str[50];
    char date_str[50];
    char *token;
    int counter = 0;
    /* parse time and date */
    for(token = strtok(oneline, delimiter);
            token != NULL;
            token = strtok(NULL, delimiter), ++counter)
    {
        if(counter == 1) {
            char *strA = "\"", *strB = ":", str[50], strC[50], strD[50];
            int v = 0, x = 3, y = 6, z=13;
            strncpy(str,token,v);
            str[v] = '\0';
            strcat(str,strA);
            strcat(str,token+v);
            strncpy(strC,str,x);
            strC[x] = '\0';
            strcat(strC,strB);
            strcat(strC,str+x);
            strncpy(strD,strC,y);
            strD[y] = '\0';
            strcat(strD,strB);
            strcat(strD,strC+y);
            strncpy(time_str,strD,z);
            time_str[z] = '\0';
            strcat(time_str,strA);
            strcat(time_str,strD+z);        
        } else if (counter == 9) {
            char *strA = "\"", *strB = "-", str[50], strC[50], strD[50];
            int v = 0, x = 3, y = 6, z=9;
            strncpy(str,token,v);
            str[v] = '\0';
            strcat(str,strA);
            strcat(str,token+v);
            strncpy(strC,str,x);
            strC[x] = '\0';
            strcat(strC,strB);
            strcat(strC,str+x);
            strncpy(strD,strC,y);
            strD[y] = '\0';
            strcat(strD,strB);
            strcat(strD,strC+y);
            strncpy(date_str,strD,z);
            date_str[z] = '\0';
            strcat(date_str,strA);
            strcat(date_str,strD+z);        
        }
    }
    if(counter < 10) {
        /* handle error case */
    } else {
        fprintf(fileName,"\"date\": %s %s", time_str, date_str);
    }
}

Upvotes: 1

Related Questions