strtok()(C)意外结果

时间:2015-04-02 18:44:55

标签: c file strtok

我打开一个这样的文本文件:

    <2504><titlos><desc><paragwgos>

我希望将其内容保存到以下变量中:

int num; //2504
char t[100], d[100], p[100];//titlos , desc , paragwgos

所以我写下这部分代码:

void readB(char *buffer, char *title, char *description, char *producer)
{
    char s[2]="><", *token;
    token = strtok(buffer, s);
    strcpy(title, token);
    token = strtok(NULL, s);
    strcpy(description, token);
    token = strtok(NULL, s);
    strcpy(producer, token);
}


int main()
{
    int num;
    char t[100], d[100], p[100], buffer[408];
    FILE *f;
    if ( (f = fopen("inp.txt", "r"))==NULL )
    {
        printf("Error opening requested file!");
        exit(1);
    }
    fscanf(f, "%*c%d", &num);
    fgets(buffer, 408, f);
    readB(buffer, t, d, p);
    printf("%d %s %s %s\n", num, t, d, p);
    fclose(f);
    return 0;
}

但不是&#34; 2504 titlos desc paragwgos&#34; 它显示:&#34; 2504 titlos desc pa&#34; 有人看到代码有什么问题吗?

0 个答案:

没有答案