简体   繁体   English

C将输入文本文件解析为单词

[英]C parsing input text file into words

I am trying to parse input file (containing a text document with multiple lines and delimiters, ie "!,.?") into words. 我正在尝试将输入文件(包含具有多行和定界符的文本文档,即“!,。?”)解析为单词。 My function 'splitting function' is: 我的功能“拆分功能”是:

int splitInput(fp) {

    int i= 0;
    char  line[255];
    char *array[5000];
    int x;
    while (fgets(line, sizeof(line), fp) != NULL) {     
        array[i] = strtok(line, ",.!? \n");
        printf("Check print - word %i:%s:\n",i, array[i]);
        i++;
    }
    return 0;
}

Here's the corrected function [sorry for extra the style cleanup]: 这是更正后的函数[很抱歉,多余的样式清理]:

int
splitInput(fp)
{
    int i = 0;
    char *cp;
    char *bp;
    char line[255];
    char *array[5000];
    int x;

    while (fgets(line, sizeof(line), fp) != NULL) {
        bp = line;
        while (1) {
            cp = strtok(bp, ",.!? \n");
            bp = NULL;

            if (cp == NULL)
                break;
            array[i++] = cp;

            printf("Check print - word %i:%s:\n",i-1, cp);
        }
    }

    return 0;
}

Now, take a look at the man page for strtok to understand the bp trick 现在,看看strtok的手册页以了解bp技巧

If I understand your question correctly you want to read every line and split each line into words and add that into an array. 如果我正确理解了您的问题,则希望阅读每一行并将每一行拆分为单词,然后将其添加到数组中。

    array[i] = strtok(line, ",.!? \n");

That will not work for obvious reasons because it will only return the first word for each line and you never allocate memory. 由于明显的原因,这将无法工作,因为它将仅返回每行的第一个单词,并且您从不分配内存。

This is probably what you want. 这可能就是您想要的。

    char *pch;
    pch = strtok(line, ",.!? \n");
    while(pch != NULL) {
      array[i++] = strdup(pch); // put the content of pch into array at position i and increment i afterwards.
      pch = strtok(NULL, ",.!? \n"); // look for remaining words at the same line
    }

Don't forget to free your array elements afterwards though using free . 别忘了使用free数组元素。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM