Tag:

将文本文件拆分为C中的单词

我有两种类型的文本,我想将它们分成单词。 第一种类型的文本文件只是由换行符分隔的单词。 Milk Work Chair … 第二种类型的文本文件是书中的文本,它只有空格。 (没有昏迷,问号等) And then she tried to run but she was stunned by the view of … 你知道哪种方法最好吗? 我尝试了以下两种方式,但似乎我正在进行分割。 对于我使用的第一种文本: 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) { […]

在C中使用linux wc命令计算文件的单词

我正在尝试编写类似于Linux命令wc的东西,以计算任何类型文件中的单词,新行和字节,我只能使用C函数读取。 我写了这段代码,我得到了换行符和字节的正确值,但我得不到计数单词的正确值。 int bytes = 0; int words = 0; int newLine = 0; char buffer[1]; int file = open(myfile,O_RDONLY); if(file == -1){ printf(“can not find :%s\n”,myfile); } else{ char last = ‘c’; while(read(file,buffer,1)==1){ bytes++; if(buffer[0]==’ ‘ && last!=’ ‘ && last!=’\n’){ words++; } else if(buffer[0]==’\n’){ newLine++; if(last!=’ ‘ && last!=’\n’){ words++; } } last […]