C++從文件中提取英文單詞的實(shí)現(xiàn)方法
首先,要準(zhǔn)備好words.txt(英文文章)置于工程目錄下
思路:
1.打開文件
2.讀取每一行
3.找到特殊的標(biāo)點(diǎn)符號(hào)的位置,進(jìn)行刪除。
4.根據(jù)空格截取單詞 find(" ");
5.將拿到的每一個(gè)單詞放在鏈表中
一:讀取一行,去除該行標(biāo)點(diǎn)符號(hào)
#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<<"請(qǐng)輸入打開的文件名:";
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職責(zé)
line = buf;
pos = line.find_first_of(",.;:'?!()/\""); //找特殊的標(biāo)點(diǎn)符號(hào)
while(pos!=string::npos)
{ //刪除單個(gè)字符
line.erase(pos,1);
//再找下一個(gè)單個(gè)的字符
pos = line.find_first_of(",.;:'?!()/\"");
}
cout<<line.c_str()<<endl; //string 轉(zhuǎn)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<<"請(qǐng)輸入打開的文件名:";
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(",.;:'?!()/\""); //找特殊的標(biāo)點(diǎn)符號(hào)
while(pos!=string::npos)
{ //刪除單個(gè)字符
line.erase(pos,1); //從什么位置開始刪除多長(zhǎng)的字符
//再找下一個(gè)單個(gè)的字符
pos = line.find_first_of(",.;:'?!()/\"");
}
cout<<line.c_str()<<endl; //string 轉(zhuǎn)char
//根據(jù)空格截取單詞 find("") 111 222 333
pos = line.find(" ");
while(pos!=string::npos)
{
//截取單詞
word = line.substr(0,pos);//從0開始,一直截到空格所在位置
cout<<word<<endl;
//把第一個(gè)單詞以及空格刪除
line.erase(0,pos+1); //從什么位置開始刪除多長(zhǎng)的字符(如刪111 )因此pos+1
pos = line.find(" "); //尋找下一個(gè)空格
}
}

三:將拿到的每一個(gè)單詞都放在鏈表中
#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<<"請(qǐng)輸入打開的文件名:";
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職責(zé)
pos = line.find_first_of(",.;:'?!()/\"");
while(pos!=string::npos)//!=npos就找到
{
line.erase(pos,1); //從什么位置開始刪除多長(zhǎng)字符
pos = line.find_first_of(",.;:'?!()/\"");//尋找下一個(gè)標(biāo)點(diǎn)符號(hào)
}
pos = line.find(" "); //尋找空格所在位置
while(pos!=string::npos)
{
word = line.substr(0,pos);//從0開始,一直截到空格所在位置
wordList.push_back(word); //拿到的單詞放在鏈表中
//把第一個(gè)單詞以及空格刪除
line.erase(0, pos+1);//從什么位置開始刪除多長(zhǎng)的字符(如刪111 )因此pos+1
pos = line.find(" ");//尋找下一個(gè)空格
}
}
cout<<"驗(yàn)證一下"<<endl;
list<string>::iterator it;
for(it = wordList.begin();it!=wordList.end();it++)
{
cout<<(*it).c_str()<<endl;
}
cout<<"總的個(gè)數(shù):"<<wordList.size();
fs.close();
}最后的結(jié)果:

到此這篇關(guān)于C++從文件中提取英文單詞的實(shí)現(xiàn)方法的文章就介紹到這了,更多相關(guān)C++ 文件中提取英文單詞內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
C/C++實(shí)現(xiàn)crc碼計(jì)算和校驗(yàn)
循環(huán)冗余校驗(yàn)(Cyclic Redundancy Check, CRC)是一種根據(jù)網(wǎng)絡(luò)數(shù)據(jù)包或計(jì)算機(jī)文件等數(shù)據(jù)產(chǎn)生簡(jiǎn)短固定位數(shù)校驗(yàn)碼的一種信道編碼技術(shù)。本文主要介紹了C++實(shí)現(xiàn)crc碼計(jì)算和校驗(yàn)的方法,需要的可以參考一下2023-03-03
C++進(jìn)一步認(rèn)識(shí)類與對(duì)象
類是創(chuàng)建對(duì)象的模板,一個(gè)類可以創(chuàng)建多個(gè)對(duì)象,每個(gè)對(duì)象都是類類型的一個(gè)變量;創(chuàng)建對(duì)象的過程也叫類的實(shí)例化。每個(gè)對(duì)象都是類的一個(gè)具體實(shí)例(Instance),擁有類的成員變量和成員函數(shù)2021-10-10
C++如何實(shí)現(xiàn)字符串的部分復(fù)制
這篇文章主要介紹了C++如何實(shí)現(xiàn)字符串的部分復(fù)制問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-08-08
c++內(nèi)聯(lián)函數(shù)(inline)使用詳解
這篇文章主要介紹了c++內(nèi)聯(lián)函數(shù)(inline)使用詳解,需要的朋友可以參考下2014-04-04

