本篇内容主要讲解“C++如何从文件中提取英文单词”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“C++如何从文件中提取英文单词”吧!
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++如何从文件中提取英文单词”有了更深的了解,不妨来实际操作一番吧!这里是亿速云网站,更多相关内容可以进入相关频道进行查询,关注我们,继续学习!
亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。