在C语言中将文本文件拆分为单词

3
我有两种文本需要拆分成单词。
第一种文本文件只是由换行符分隔的单词。
Milk
Work
Chair
...

第二种文本文件是来自书籍的纯文本,只包含空格。(没有逗号、问号等符号。)
And then she tried to run 
but she was stunned by the view of 
...

请问您知道最佳的方法是什么吗?

我尝试了以下2种方式,但好像出现了分段问题。

对于第一种文本类型,我使用:

while(fgets(line,sizeof(line),wordlist) != NULL)
{
    /* Checks Words |
    printf("%s",line);*/
    InsertWord(W,line);/*Function that inserts the word to a tree*/
}

对于第二类文本,我使用:

while(fgets(line,sizeof(line),out) != NULL)
{
    bp = line ;
    while(1)
    {
        cp = strtok(bp," ");
        bp = NULL ;

        if(cp == NULL)
            break;

        /*printf("Word by Word : %s \n",cp);*/
        CheckWord(Words, cp);/*Function that checks if the word from the book is the same with one in a tree */
    }
}

您能否提供更好的建议或者纠正我对以下问题的理解?

编辑:(关于segm.fault)

InsertWord是一个将单词插入到树中的函数。当我使用以下代码时:

for (i = 0 ; i <=2 ; i++)
{
    if (i==0)
        InsertWord(W,"A");
    if (i==1)
        InsertWord(W,"B");
    if (i==2)
        InsertWord(W,"c");
}*/

树成功地插入了单词并将它们打印出来,这意味着我的树工作正常,并且它的函数(也是由我们的老师给出的)。 但是当我尝试像这样做时:
char this_word[15];
while (fscanf(wordlist, "%14s", this_word) == 1) 
{
    printf("Latest word that was read: '%s'\n", this_word);
    InsertWord(W,this_word);
}

我从树结构中得到了错误信息。因此,我猜测这是某种分段错误。你有什么想法吗?


你在这两个函数中都得到了“segmentations”吗?那么我们需要查看 InsertWord 和/或 CheckWord - Jongware
你的第二个示例中输入文件名为 out,这似乎很奇怪。这是一个错误还是不幸选择的名称?但是你真的需要展示一个完整但最小化的示例来展示你的问题。否则我们只能猜测。 - Thomas Padron-McCarthy
修改了我的问题,最小示例会有点复杂,但我可以尝试提供。 - Social Programmer
我的猜测是你的树代码有问题,但在你展示一个完整(且最小化!)的程序之前,这只是一个猜测。 - Thomas Padron-McCarthy
嗯,好的,你是对的。我应该在一个新帖子中发布我的树还是编辑这个帖子? - Social Programmer
显示剩余3条评论
3个回答

3
这是fscanf%s所设计的输入类型:
char this_word[15];
while (fscanf(tsin, "%14s", this_word) == 1) {
    printf("Latest word that was read: '%s'.\n", this_word);
    // Process the word...
}

修改了我的问题,你的函数运行良好...请帮忙 :o - Social Programmer

2

如果你想从文件中读取内容,可以使用fgets()函数。

如果你想按照分隔符(空格)将字符串分割为多个片段,可以使用strtok()函数。


因此,你可以这样做:

#include <stdio.h>
#include <string.h>

int main(void)
{
   FILE * pFile;
   char mystring [100];
   char* pch;

   pFile = fopen ("text_newlines.txt" , "r");
   if (pFile == NULL) perror ("Error opening file");
   else {
     while ( fgets (mystring , 100 , pFile) != NULL )
       printf ("%s", mystring);
     fclose (pFile);
   }

   pFile = fopen ("text_wspaces.txt" , "r");
   if (pFile == NULL) perror ("Error opening file");
   else {
     while ( fgets (mystring , 100 , pFile) != NULL ) {
       printf ("%s", mystring);
       pch = strtok (mystring," ");
       while (pch != NULL)
       {
         printf ("%s\n",pch);
         pch = strtok (NULL, " ");
       }
     }
     fclose (pFile);
   }

   return 0;
}

输出:

linux25:/home/users/grad1459>./a.out
Milk
Work
Chair
And then she tried to run 
And
then
she
tried
to
run


but she was stunned by the view of
but
she
was
stunned
by
the
view
of
//newline here as well

我按照那样做了,输出结果也是正确的。我的问题是我试图将它们插入到一棵树中,然后打印这棵树。当我使用fgets时,树在插入这些单词时出现错误,尽管当我在插入单词树和打印树时放置字符串"A","B"时,树可以正常工作。唉,我想不出发生了什么...(你是希腊人吗?) - Social Programmer
我在这里发布了问题:https://dev59.com/PZffa4cB1Zd3GeqP4jeW,但仍然无法解决。 - Social Programmer

0

最简单的方法可能是逐个字符地进行:

char word[50];
char *word_pos = word;

// Discard characters until the first word character
while ((ch = fgetch(out)) != EOF &&
        ch != '\n' &&
        ch != ' ');

do {
    if (ch == '\n' || ch == ' ') {
        *word_pos++ = '\0';
        word_pos = word;
        CheckWord(Words, word);

        while ((ch = fgetch(out)) != EOF &&
                ch != '\n' &&
                ch != ' ');
    }

    *word_pos++ = ch;
} while ((ch = fgetch(out)) != EOF);

你受到word大小的限制,需要将每个停止字符添加到whileif条件中。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接