您现在的位置是:首页 > 编程语言学习 > 前端编程语言 > 文章正文 前端编程语言
C++从文件中提取英文单词的实现方法
2022-06-16 11:32:53 前端编程语言
简介本文主要介绍了C++从文件中提取英文单词的实现方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要...
本文主要介绍了C++从文件中提取英文单词的实现方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧!
首先,要准备好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();
- }
最后的结果:
下一篇:最后一页