如何将文本文件拆分成单词? [英] How to split a text file into words?
问题描述
我正在做一个作业,我应该读取一个文件,并计算行数,同时计数其中的单词。
我在while循环中尝试了getline和strtok的组合,但是没有工作。
I am working on a assignment where I am supposed to read a file and count the number of lines and at the same time count the words in it. I tried a combination of getline and strtok inside a while loop, which did not work.
file:example.txt(要读取的文件)。
file:example.txt (the file to be read).
您好,您好感到惊喜。
欢迎来到这个地方。
您可以在这里度过愉快的假期。
¥b $ b(3行和一些字)。
Hi, hello what a pleasant surprise.
Welcome to this place.
May you have a pleasant stay here.
(3 lines, and some words).
Readfile.cpp
Readfile.cpp
#include <iostream>
#include <fstream>
#include<string>
using namespace std;
int main()
{
ifstream in("example.txt");
int count = 0;
if(!in)
{
cout << "Cannot open input file.\n";
return 1;
}
char str[255];
string tok;
char * t2;
while(in)
{
in.getline(str, 255);
in>>tok;
char *dup = strdup(tok.c_str());
do
{
t2 = strtok(dup," ");
}while(t2 != NULL);
cout<<t2<<endl;
free (dup);
count++;
}
in.close();
cout<<count;
return 0;
}
推荐答案
删除所有不必要的代码。
Just got this right!! Just removed all unnecessary code.
int main()
{
ifstream in("example.txt");
int LineCount = 0;
char* str = new char[500];
while(in)
{
LineCount++;
in.getline(str, 255);
char * tempPtr = strtok(str," ");
while(tempPtr)
{
AddWord(tempPtr, LineCount);
tempPtr = strtok(NULL," ,.");
}
}
in.close();
delete [] str;
cout<<"Total No of lines:"<<LineCount<<endl;
showData();
return 0;
}
原始问题语句是创建一个索引程序,文件并创建所有单词的行索引。
BTW the original problem statement was to create a index program that would accept a user file and create an line-index of all words.
这篇关于如何将文本文件拆分成单词?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!