Need help parsing a "|" seperated line from a file

时间:2019-04-23 15:09:15

标签: c parsing

I have to parse a file that would look something like this

String|OtherString|1234|0

String2|OtherString2|4321|1

...

So, I need to go through every line of the file and take each seperate token of each line.

    FILE *fp=fopen("test1.txt","r");
    int c;
    char str1[500];
    char str2[500];
    int num1=0;
    int num2;
    while((c=fgetc(fp))!=EOF){
        fscanf(fp, "%s|%s|%d|%d", &str1[0], &str2[0], &num1, &num2);
    }
    fclose(fp);

There's more to it, but these are the sections relevant to my question. fscanf isn't working, presumably because I've written it wrong. What's supposed to happen is that str1[500] should be set to String, in this case, str2 to OtherString, etc. It seems as though fscanf isn't doing anything, however. Would greatly appreciate some help.

EDIT: I am not adamant about using fgetc or fscanf, these are just what I have atm, I'd use anything that would let me do what I have to

1 个答案:

答案 0 :(得分:2)

循环中的

strtok() 将为您工作。下面是一个简单的示例,几乎没有错误处理等,但是说明了这个概念...

char strArray[4][80];
char *tok = NULL;
char *dup = strdup(origLine);
int i = 0;
if(dup)
{
    tok = strtok(dup, "|\n");
    while(tok)
    {
        strcpy(strArray[i], tok);
        tok = strtok(NULL, "|\n");
        i++;
    }
    free(dup);
}

如果从文件读取,则将此循环逐行放入另一个while循环中,该循环读取文件。对此有用的功能将包括 fopen() fgets() fclose() 。对于从文件读取数据的代码,应考虑的另一项功能是确定要读取的文件中的记录(行)数,并使用该信息来创建适当大小的容器,用该容器填充解析结果。但这将是另一个问题。

注意:此处不建议使用 fgetc() ,因为它每个循环读取一个char,效率不如使用fgets()来读取行中的行。与strtok()结合使用时的文件。

还请注意,通常,文件在字段数,字段内容等方面的格式设置越一致。解析器所需的复杂性就越低。反之亦然。格式不一致的输入文件需要更复杂的解析器。例如,对于人工输入的行数据,所需的解析器通常比用于计算机生成的一组统一行的解析器复杂。