Search code examples
cfileoutputstrtok

How can i use strtok?


{"2014-02-13T06:20:00": "3.0", "2014-02-13T13:50:00": "7.0", "2014-02-13T06:00:00": "2", "2014-02-13T03:00:00": "3", "2014-02-13T13:00:00": "6", "2014-02-13T18:50:00": "4.0", "2014-02-13T13:20:00": "6.0", "2014-02-13T15:00:00": "6", "2014-02-13T08:50:00": "4.0", "2014-02-13T21:50:00": "4.0", "2014-02-13T08:00:00": "3", "2014-02-13T07:50:00": "3.0", "2014-02-13T08:20:00": "4.0", "2014-02-13T21:20:00": "3.0", "2014-02-13T11:50:00": "6.0", "2014-02-13T11:20:00": "6.0", "2014-02-13T17:50:00": "5.0", "2014-02-13T11:00:00": "6", "2014-02-13T05:50:00": "2.0", "2014-02-13T20:50:00": "3.0", "2014-02-13T20:20:00": "4.0", "2014-02-13T16:00:00": "6", "2014-02-13T23:50:00": "2.0", "2014-02-13T21:00:00": "3", "2014-02-13T07:20:00": "3.0", "2014-02-13T03:20:00": "3.0", "2014-02-13T07:00:00": "3", "2014-02-13T15:50:00": "6.0", "2014-02-13T03:50:00": "2.0", "2014-02-13T04:00:00": "2", "2014-02-13T12:00:00": "6", "2014-02-13T04:20:00": "2.0", "2014-02-13T12:20:00": "6.0", "2014-02-13T12:50:00": "6.0", "2014-02-13T22:50:00": "3.0", "2014-02-13T09:00:00": "4", "2014-02-13T09:20:00": "4.0", "2014-02-13T09:50:00": "4.0", "2014-02-13T18:00:00": "5", "2014-02-13T05:20:00": "2.0", "2014-02-13T15:20:00": "6.0", "2014-02-13T00:50:00": "4.0", "2014-02-13T14:50:00": "7.0", "2014-02-13T00:00:00": "4", "2014-02-13T00:20:00": "4.0", "2014-02-13T06:50:00": "3.0", "2014-02-13T22:00:00": "4", "2014-02-13T18:20:00": "5.0", "2014-02-13T02:50:00": "3.0", "2014-02-13T02:20:00": "3.0", "2014-02-13T04:50:00": "2.0", "2014-02-13T02:00:00": "3", "2014-02-13T23:00:00": "3", "2014-02-13T16:50:00": "5.0", "2014-02-13T19:50:00": "4.0", "2014-02-13T19:20:00": "4.0", "2014-02-13T05:00:00": "2", "2014-02-13T19:00:00": "4", "2014-02-13T23:20:00": "3.0", "2014-02-13T14:20:00": "7.0", "2014-02-13T10:20:00": "5.0", "2014-02-13T10:00:00": "4", "2014-02-13T10:50:00": "5.0", "2014-02-13T17:00:00": "5", "2014-02-13T01:00:00": "4", "2014-02-13T17:20:00": "5.0", "2014-02-13T01:20:00": "4.0", "2014-02-13T01:50:00": "4.0", "2014-02-13T22:20:00": "3.0", "2014-02-13T16:20:00 :"6.0"}

I have this text, which represent (for example): date -> 2014-02-13T06:20:00 humid rate -> 4.0

At first, I want to have as an output date \n humid rate \n date \n humid rate etc. etc.. I've tried to use strtok, but i couldn't do much.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define LINESIZE 128

int main()
{
    FILE *fp = fopen("tempm.txt", "r");
    char line[LINESIZE];
    char *value;

    while(fgets(line, sizeof(line), fp))
    {
        value = strtok(line, "\" \":");
        printf("\n%s", value);  
    }

    return 0;
}

This is the code I've written so far.


Solution

  • fscanf using scansets might work.

    #include <stdio.h>
    #include <stdlib.h>
    
    int main ( void) {
        char *filename = "tempm.txt";
        char date[30] = "";
        char humidity[30] = "";
        FILE *fp = NULL;
        if ( NULL == ( fp = fopen ( filename, "r"))) {
            perror ( filename);
            exit ( EXIT_FAILURE);
        }
        fscanf ( fp, "%*[^0123456789]");//scan and discard up to first digit
        while ( 1 == fscanf ( fp, "%29[^\"]", date)) {//scan up to a quote
            fscanf ( fp, "%*[^0123456789]");//scan and discard up to next digit
            if ( 1 == fscanf ( fp, "%29[^\"]", humidity)) {//scan up to a quote
                printf ( "%s\n%s\n", date, humidity);
            }
            fscanf ( fp, "%*[^0123456789]");//scan and discard up to next digit
        }
        fclose ( fp);
        return 0;
    }