您现在的位置是:首页 > 编程语言学习 > 前端编程语言 > 文章正文 前端编程语言

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

2022-06-16 11:32:53 前端编程语言

简介本文主要介绍了C++从文件中提取英文单词的实现方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要...

本文主要介绍了C++从文件中提取英文单词的实现方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧!

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

思路:

1.打开文件

2.读取每一行

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

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

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

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

  1. #include<iostream> 
  2. using namespace std; 
  3. #include<fstream> 
  4. #include<string> 
  5. #include<list> 
  6. void test_word_split(); 
  7.   
  8. int main() 
  9.     test_word_split(); 
  10.     return 0; 
  11.   
  12. void test_word_split() 
  13.     fstream fs; 
  14.     char filename[20] = {0}; 
  15.     cout<<"请输入打开的文件名:"
  16.     cin>>filename; 
  17.     //打开文件 
  18.     fs.open(filename); 
  19.     cout<<"打开成功"<<filename<<endl; 
  20.     char buf[1024] = {0}; 
  21.     fs.getline(buf,1024);//读取每一行 
  22.     cout<<buf<<endl; 
  23.     size_t pos;   //找到位置 
  24.     string line;  //接替buf职责 
  25.     line = buf; 
  26.     pos = line.find_first_of(",.;:'?!()/\"");  //找特殊的标点符号 
  27.     while(pos!=string::npos) 
  28.     {   //删除单个字符 
  29.         line.erase(pos,1); 
  30.         //再找下一个单个的字符 
  31.        pos = line.find_first_of(",.;:'?!()/\"");  
  32.     } 
  33.     cout<<line.c_str()<<endl; //string 转char 
  34.   


二:截取单词

  1. #include<iostream> 
  2. using namespace std; 
  3. #include<fstream> 
  4. #include<string> 
  5. #include<list> 
  6. void test_word_split(); 
  7.   
  8. int main() 
  9.     test_word_split(); 
  10.     return 0; 
  11.   
  12. void test_word_split() 
  13.     fstream fs; 
  14.     char filename[20] = {0}; 
  15.     cout<<"请输入打开的文件名:"
  16.     cin>>filename; 
  17.     //打开文件 
  18.     fs.open(filename); 
  19.     cout<<"打开成功"<<filename<<endl; 
  20.     char buf[1024] = {0}; 
  21.     fs.getline(buf,1024);//读取每一行 
  22.     cout<<buf<<endl; 
  23.     size_t pos; 
  24.     string line,word; 
  25.     line = buf; 
  26.     pos = line.find_first_of(",.;:'?!()/\"");  //找特殊的标点符号 
  27.     while(pos!=string::npos) 
  28.     {   //删除单个字符 
  29.         line.erase(pos,1);   //从什么位置开始删除多长的字符 
  30.         //再找下一个单个的字符 
  31.         pos = line.find_first_of(",.;:'?!()/\"");  
  32.     } 
  33.     cout<<line.c_str()<<endl; //string 转char 
  34.     //根据空格截取单词 find("")  111 222 333 
  35.     pos = line.find(" "); 
  36.     while(pos!=string::npos) 
  37.     { 
  38.         //截取单词 
  39.         word = line.substr(0,pos);//从0开始,一直截到空格所在位置 
  40.         cout<<word<<endl;      
  41.         //把第一个单词以及空格删除 
  42.         line.erase(0,pos+1);  //从什么位置开始删除多长的字符(如删111 )因此pos+1 
  43.         pos = line.find(" "); //寻找下一个空格 
  44.     } 
  45.   

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

  1. #include<iostream> 
  2. using namespace std; 
  3. #include<fstream> 
  4. #include<string> 
  5. #include<list> 
  6. void test_word_split(); 
  7.   
  8. int main() 
  9.     test_word_split(); 
  10.     return 0; 
  11.   
  12. void test_word_split() 
  13.     list<string> wordList;//链表 
  14.     fstream fs; 
  15.     char filename[20] = {0}; 
  16.     cout<<"请输入打开的文件名:"
  17.     cin>>filename; 
  18.     fs.open(filename); 
  19.     cout<<"打开成功"<<filename<<endl; 
  20.     char buf[1024] = {0}; 
  21.     string line,word;  //初始化定义 
  22.     while(fs.getline(buf, 1024))//读取每一行 
  23.     { 
  24.         size_t pos;  //找到位置 
  25.         line = buf;  //接替buf职责 
  26.         pos = line.find_first_of(",.;:'?!()/\""); 
  27.         while(pos!=string::npos)//!=npos就找到 
  28.         { 
  29.             line.erase(pos,1);  //从什么位置开始删除多长字符 
  30.             pos = line.find_first_of(",.;:'?!()/\"");//寻找下一个标点符号 
  31.         } 
  32.         pos = line.find(" ");  //寻找空格所在位置 
  33.         while(pos!=string::npos) 
  34.         { 
  35.             word = line.substr(0,pos);//从0开始,一直截到空格所在位置 
  36.             wordList.push_back(word); //拿到的单词放在链表中 
  37.             //把第一个单词以及空格删除 
  38.             line.erase(0, pos+1);//从什么位置开始删除多长的字符(如删111 )因此pos+1 
  39.             pos = line.find(" ");//寻找下一个空格 
  40.         } 
  41.     } 
  42.     cout<<"验证一下"<<endl; 
  43.     list<string>::iterator it; 
  44.     for(it = wordList.begin();it!=wordList.end();it++) 
  45.     { 
  46.         cout<<(*it).c_str()<<endl; 
  47.     } 
  48.     cout<<"总的个数:"<<wordList.size(); 
  49.     fs.close(); 

最后的结果:

C++

相关文章

站点信息