c++ 文本处理

1、使用sstream版本

(1)功能:截取第一列为1以后的数据,如下图,截取第5行(包括第5行)以后的数据,前面4行数据丢弃。



(2)代码:textProc.cc

#include <iostream>
#include <fstream>
#include <sstream>
#include <string> using namespace std; int main(int argc, char *argv[]) {// *argv[] 是一个指针数组,也就是一个字符串数组。
string dir = "/data/"; string old_file = dir + argv[1] + "/input/txdsp_input_old.dat";
string gen_file = dir + argv[1] + "/input/txdsp_input.dat";
string cmd = "mv ";
cmd = cmd + gen_file + " " + old_file; if(system(cmd.c_str()) == 0) {
cout<<"file move ok !"<<endl;
} else {
cout<<"file move failed !"<<endl;
} ifstream is(old_file);
ofstream os(gen_file);
istringstream iss;
string line, word; while(getline(is, line)) {
iss.str(line); // 刷新string流中的字符串
iss.clear(); // 清除string流中的状态,如果string流读完了failbit会拉起,导致刷新了新数据也不能读取。
iss>>word; // 利用string流一个一个单词的读取, 这里只读一个帧头标识。 if(word == "1") { // 将找到帧头后的所以数据都输出。
os<<line<<endl;
break;
}
} while(getline(is, line)) {
os<<line<<endl;
} os.close();
is.close();
return 0;
}

2、regex版本

#include <iostream>
#include <fstream>
#include <regex>
#include <string> using namespace std; int main(int argc, char *argv[]) {// *argv[] 是一个指针数组,也就是一个字符串数组。
string dir = "/data/"; string old_file = dir + argv[1] + "/input/txdsp_input_old.dat";
string gen_file = dir + argv[1] + "/input/txdsp_input.dat";
string cmd = "mv ";
cmd = cmd + gen_file + " " + old_file; if(system(cmd.c_str()) == 0) {
cout<<"file move ok !"<<endl;
} else {
cout<<"file move failed !"<<endl;
} ifstream is(old_file);
ofstream os(gen_file);
string line;
smatch results; regex re("^1.*");
while(getline(is, line)) {
if(regex_search(line, results, re)) { // 将找到帧头后的所以数据都输出。
os<<line<<endl;
break;
}
} while(getline(is, line)) {
os<<line<<endl;
} regex re2("^tel:(\\d{11}),addr:(\\w+)"); // 正则表达式相对于python的要多加一个反斜杠“\”,应该\在C是特殊字符
string str = "tel:15688886666,addr:sichuan";
regex_search(str, results, re2); //传给regex_search的字符串可以是string,char,wchar等4种类型,对应的使用smatch,cmatch、wcmatch等4中类型。为了简单,一般只使用string+smatch。
cout<<results.str()<<endl;           //打印整个str
cout<<results.str(1)<<endl;       //打印第一个匹配的括号(\\d{11})
cout<<results.str(2)<<endl;     //打印第二个匹配的括号(\\w+) os.close();
is.close();
return 0;
}

输出:

最新文章

  1. Android笔记——数据库升级与降级
  2. IDEA 将已有项目添加到git
  3. C99标准的新特性
  4. Javascript 笔记与总结(1-5)闭包
  5. Struts2重定向
  6. RAC分解步骤之一,在oracle linux 4u4上安装oracle 10.2.0.1.0操作日志
  7. LeetCode解题报告:Reorder List
  8. oop实现方法与属性继承
  9. Trafic
  10. Ceph,TFS,FastDFS,MogileFS,MooseFS,GlusterFS 对比
  11. 用SpriteBuilder简化&quot;耕牛遍地走&quot;的动画效果(一)
  12. dnmp(docker的lnmp)安装WordPress之后图片上传问题 问题:图片上传大小问题解决和 报错413 Request Entity Too Large
  13. 2019年年初iOS招人心得笔记(附面试题)
  14. where are you?
  15. python之使用位运算符实现加法运算
  16. LOJ2250 [ZJOI2017] 仙人掌【树形DP】【DFS树】
  17. 安装原版Win8.1并激活
  18. [luogu4264][USACO18FEB]Teleportation
  19. php安装及配置笔记
  20. 「功能笔记」Spacemacs+Evil备忘录

热门文章

  1. 第10组 Alpha冲刺 (5/6)
  2. 通过了解Servlet和Http之间的关系,了解web中http通信使用(二)
  3. vue3.0+vue-cli3.0项目搭建
  4. HDURomantic
  5. HDU 1863 畅通工程 (并查集)
  6. HDU 1106 (1.3.5) 排序 (C语言描述)
  7. 最完整的springboot2.2.x.RELEASE整合springDataElasticsearch 7.6.2
  8. Java中运算符及其优先级、自动类型提升、类型转化
  9. GUI系统
  10. 读书笔记http之第一章