C++从文件中提取英文单词的实现方法

Rosalia ·
更新时间:2024-09-21
· 20 次阅读

目录

思路:

一:读取一行,去除该行标点符号

二:截取单词

三:将拿到的每一个单词都放在链表中

首先,要准备好words.txt(英文文章)置于工程目录下

思路:

1.打开文件

2.读取每一行

3.找到特殊的标点符号的位置,进行删除。

4.根据空格截取单词 find(" ");

5.将拿到的每一个单词放在链表中

一:读取一行,去除该行标点符号 #include<iostream> using namespace std; #include<fstream> #include<string> #include<list> void test_word_split(); int main() { test_word_split(); return 0; } void test_word_split() { fstream fs; char filename[20] = {0}; cout<<"请输入打开的文件名:"; cin>>filename; //打开文件 fs.open(filename); cout<<"打开成功"<<filename<<endl; char buf[1024] = {0}; fs.getline(buf,1024);//读取每一行 cout<<buf<<endl; size_t pos; //找到位置 string line; //接替buf职责 line = buf; pos = line.find_first_of(",.;:'?!()/\""); //找特殊的标点符号 while(pos!=string::npos) { //删除单个字符 line.erase(pos,1); //再找下一个单个的字符 pos = line.find_first_of(",.;:'?!()/\""); } cout<<line.c_str()<<endl; //string 转char }

二:截取单词 #include<iostream> using namespace std; #include<fstream> #include<string> #include<list> void test_word_split(); int main() { test_word_split(); return 0; } void test_word_split() { fstream fs; char filename[20] = {0}; cout<<"请输入打开的文件名:"; cin>>filename; //打开文件 fs.open(filename); cout<<"打开成功"<<filename<<endl; char buf[1024] = {0}; fs.getline(buf,1024);//读取每一行 cout<<buf<<endl; size_t pos; string line,word; line = buf; pos = line.find_first_of(",.;:'?!()/\""); //找特殊的标点符号 while(pos!=string::npos) { //删除单个字符 line.erase(pos,1); //从什么位置开始删除多长的字符 //再找下一个单个的字符 pos = line.find_first_of(",.;:'?!()/\""); } cout<<line.c_str()<<endl; //string 转char //根据空格截取单词 find("") 111 222 333 pos = line.find(" "); while(pos!=string::npos) { //截取单词 word = line.substr(0,pos);//从0开始,一直截到空格所在位置 cout<<word<<endl; //把第一个单词以及空格删除 line.erase(0,pos+1); //从什么位置开始删除多长的字符(如删111 )因此pos+1 pos = line.find(" "); //寻找下一个空格 } }

三:将拿到的每一个单词都放在链表中 #include<iostream> using namespace std; #include<fstream> #include<string> #include<list> void test_word_split(); int main() { test_word_split(); return 0; } void test_word_split() { list<string> wordList;//链表 fstream fs; char filename[20] = {0}; cout<<"请输入打开的文件名:"; cin>>filename; fs.open(filename); cout<<"打开成功"<<filename<<endl; char buf[1024] = {0}; string line,word; //初始化定义 while(fs.getline(buf, 1024))//读取每一行 { size_t pos; //找到位置 line = buf; //接替buf职责 pos = line.find_first_of(",.;:'?!()/\""); while(pos!=string::npos)//!=npos就找到 { line.erase(pos,1); //从什么位置开始删除多长字符 pos = line.find_first_of(",.;:'?!()/\"");//寻找下一个标点符号 } pos = line.find(" "); //寻找空格所在位置 while(pos!=string::npos) { word = line.substr(0,pos);//从0开始,一直截到空格所在位置 wordList.push_back(word); //拿到的单词放在链表中 //把第一个单词以及空格删除 line.erase(0, pos+1);//从什么位置开始删除多长的字符(如删111 )因此pos+1 pos = line.find(" ");//寻找下一个空格 } } cout<<"验证一下"<<endl; list<string>::iterator it; for(it = wordList.begin();it!=wordList.end();it++) { cout<<(*it).c_str()<<endl; } cout<<"总的个数:"<<wordList.size(); fs.close(); }

最后的结果:

 到此这篇关于C++从文件中提取英文单词的实现方法的文章就介绍到这了,更多相关C++ 文件中提取英文单词内容请搜索软件开发网以前的文章或继续浏览下面的相关文章希望大家以后多多支持软件开发网!



c+ 方法 C++ 英文单词 单词

需要 登录 后方可回复, 如果你还没有账号请 注册新账号