簡體   English   中英

如何使用 strtok?

[英]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"}

我有這個文本,它代表(例如):日期 -> 2014-02-13T06:20:00 濕度率 -> 4.0

起初,我想擁有一個 output 日期 \n 濕度率 \n 日期 \n 濕度率等。我試過使用strtok ,但我做不了太多。

#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;
}

這是我到目前為止寫的代碼。

使用掃描集的fscanf可能會起作用。

#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;
}

您顯示的數據似乎與使用JSON 解析器更兼容。 (但這不是你問的。)

如何使用 strtok?
首先,研究輸入數據的模式。 你的是可重復的,使strtok()的使用可行,但它的格式化方式將使解析變得有趣。

使用您提供的輸入文件,分隔符是不幸的:例如,在時間字符串段之間使用“,”分隔符:以及用於濕度的第二個子字符串前面:

"2014-02-13T06:20:00": "3.0", 
              ^  ^   ^
              |  |   |_this would be better as a comma ","
              |__|_____These are fine as is

但只要輸入文件實際上是你所擁有的形狀,下面的代碼就會解析它。

對於您的問題:我如何使用 strtok? ,下面的實現說明了如何做到這一點。 它結合使用嵌套的strtok()和對strchr()的調用來遍歷文件,將每個項目分成變量。

int fsize(FILE *fp);

int main(void)
{
    FILE *fp = fopen("tempm.txt", "r");

    char *value;
    char hr[4];
    char min[4];
    char sec[4];
    char date[12];
    char hum_rate[6];
    char *data;
    char line[100][50];//change if file size changes!
    double num = 0.0;

    char *tok;

    int filesize = fsize(fp);//get file size
    data = calloc(filesize+1, 1);//size the buffer to contain file contents
    if(!data) //failed, exit
    {
        return 0    
    }
    fgets(data, filesize+1, fp);//read file int buffer (its one line)

   int i = 0;
    value = strtok(data, ",");//get string segmemts
    while(value)
    {   //separate into data into date-time-hum_rate segments
        strcpy(line[i++], value);//line segments
        value = strtok(NULL, ",");
    }
    //we now have data segments separated
    int count = i;//preserve value of i into count
    for(i=0;i<count;i++)
    {
        value = strtok(line[i], "T");//get date
        if(value)
        {
            tok = strchr(value, '"');
            strcpy(date, tok+1);//consume the extra "
            value = strtok(NULL, ":");//hour
            if(value)
            {
                strcpy(hr, value);
                value = strtok(NULL, ":");//minute
                if(value)
                {
                    strcpy(min, value);//second
                    value = strtok(NULL, ":\"");
                    if(value)
                    {
                        strcpy(sec, value);
                        value = strtok(NULL, ":");//humidity
                        if(value)
                        {
                            tok = strchr(value, '"');//consume the extra "
                            num = strtod(tok+1, NULL);
                            sprintf(hum_rate, "%.1lf", num);
                        }
                    }
                }
            }
        }
        //output to test results:
        printf("%d)\nDate: %s\nTime: %s:%s:%s\nHumidity Rate: %s\n\n", i+1, date, hr, min, sec, hum_rate);
        free(data);
    }

    return 0;
}

int fsize(FILE *fp)
{
    int prev=ftell(fp);
    fseek(fp, 0L, SEEK_END);
    int sz=ftell(fp);
    fseek(fp,prev,SEEK_SET); //go back to where we were
    return sz;
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM