服务器之家:专注于VPS、云服务器配置技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|VB|R语言|JavaScript|易语言|vb.net|

服务器之家 - 编程语言 - C/C++ - C++从文件中提取英文单词的实现方法

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

2022-12-25 16:12想吃读研的苦 C/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
}

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

 

二:截取单词

#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(" "); //寻找下一个空格
	}
}

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

 

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

#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++ 文件中提取英文单词内容请搜索服务器之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持服务器之家!

原文链接:https://blog.csdn.net/m0_56051805/article/details/124395841

延伸 · 阅读

精彩推荐