QT在进行文本读写时和C++一样,是基于文本流操作的。

QT在读取全部文本时,相对比较便捷。使用readAll()函数,配合split()进行分隔符的拆分(例如行结束符"\n"),可将拆分结果赋值给list,然后进行后续的数据处理。

ringRoadDistList = ringRoadDistStream.readAll().split("\n",QString::SkipEmptyParts);

在C++中也可以实现类似的效果:

    list<string> strList;
char line[];
while (false == staEnLane2Stream.eof())
{
staEnLane2Stream.getline(line, sizeof(line)); //c从staEnLane2Stream按行读取sizeof(line)个数到缓冲区line中(遇到"\n",将提前截止)
strList.push_back(line);
}

如果遇到换行符'\n'(第一种形式)或delim(第二种形式),则读取终止,'\n'或delim都不会被保存进s对应的数组中。

基于文本流的输出,两个类似:

ofstream resultStream;
resultStream.open(staEnLane3Path.c_str(), ios_base::out);
if (false == resultStream.is_open())
{
cerr << "error: unable to open output file: " << staEnLane3Path << endl;
return ;
}
while (lane2Map.end() != j)
{
cout << (*j).first << " " << (*j).second << endl;
resultStream << (*j).first << " " << (*j).second << endl;
++j;
}

举个栗子:

// ringRoadTime.cpp : 定义控制台应用程序的入口点。

#include "stdafx.h"
#include <string>
#include <cstring>
#include <cstdlib>
#include <fstream>
#include <iostream>
#include <list>
#include <map>
using namespace std; int _tmain(int argc, _TCHAR* argv[])
{
string rootPath = "E:/superHighway/ringRoadData/GC/GC_Entry/";
string staEnLane2Name = "GC_stationEntryLane2.csv";
string staEnLane3Name = "GC_stationEntryLane.csv";
string staEnLane2Path = rootPath + staEnLane2Name;
string staEnLane3Path = rootPath + staEnLane3Name;
//ifstream staEnLane2Stream(staEnLane2Path.c_str(), ios::in);
ifstream staEnLane2Stream;
ofstream resultStream; //文件打开,保存数据到list,关闭文件
staEnLane2Stream.open(staEnLane2Path.c_str(), ios_base::in);
if (false == staEnLane2Stream.is_open())
{
cerr << "error: unable to open input file: " << staEnLane2Path << endl;
return ;
}
list<string> strList;
char line[];
while (false == staEnLane2Stream.eof())
{
staEnLane2Stream.getline(line, sizeof(line));
strList.push_back(line);
}
staEnLane2Stream.close();
resultStream.open(staEnLane3Path.c_str(), ios_base::out);
if (false == resultStream.is_open())
{
cerr << "error: unable to open output file: " << staEnLane3Path << endl;
return ;
} //数据插入map中,进行匹配
map<string, string> lane2Map;
list<string>::iterator k = strList.begin();
for (; k != strList.end(); ++k)
{
size_t i = (*k).find_first_of(",");
lane2Map.insert(pair<string, string>((*k).substr(,i), (*k).substr(i+)));
}
map<string, string>::iterator j = lane2Map.begin();
while (lane2Map.end() != j)
{
cout << (*j).first << " " << (*j).second << endl;
resultStream << (*j).first << " " << (*j).second << endl; //基于文本流的数据写入
++j;
}
resultStream.close(); system("pause");
return ;
}

C++文本处理_文件读写的更多相关文章

  1. 零基础逆向工程16_C语言10_宏定义_头文件_内存分配_文件读写

    #define 无参数的宏定义的一般形式为:#define 标识符 字符序列 如:#define TRUE 1 注意事项: 1.之作字符序列的替换工作,不作任何语法的检查 2.如果宏定义不当,错误要到 ...

  2. python基础操作_文件读写操作

    #文件读写# r只能读不能写,且文件必须存在,w只能写不能读,a只能写不能读# w+是写读模式,清空原文件内容# r+是读写模式,没有清空原文件内容,# 只要有r,文件必须存在,只要有w,都会清空原文 ...

  3. C#_文件读写常用类介绍

    首先要熟悉.NET中处理文件和文件夹的操作.File类和Directory类是其中最主要的两个类.了解它们将对后面功能的实现提供很大的便利.      本节先对和文件系统相关的两个.NET类进行简要介 ...

  4. day5_函数_文件读写_用一个函数来满足文件的读或者写_应用默认参数

    import json def op_file_tojson(filename,dic=None): #默认值参数,根据是否传dic字典来判断读还是写 if dic: #如果dic传了值,不是空的,则 ...

  5. 关于Windows文件读写_暗涌_新浪博客

    关于Windows文件读写_暗涌_新浪博客     这几天在研究怎么才能加快windows文件读写速度,搜了很多文章,MSDN也看了不少.稍微给大家分享一下.     限制windows文件读写速度的 ...

  6. Demo02_对结构体进行文件读写_张仕传_作业_

    #include <iostream> using namespace std; #define StructArrarySize 5 // 老师数量 #define StudentNum ...

  7. 7. Buffer_包描述文件_npm常用指令_fs文件读写_模块化require的规则

    1. Buffer 一个和数组类似的对象,不同是 Buffer 是专门用来保存二进制数据的. 特点: 大小固定: 在创建时就确定了,且无法调整 性能较好: 直接对计算机的内存进行操作 每个元素大小为1 ...

  8. QT_8_Qt中的事件处理_定时器事件_定时器类_事件分发器_事件过滤器_绘图事件_高级绘图事件_绘图设备_QFile 文件读写_QFileInfo文件信息

    Qt中的事件处理 1.1. 捕获QLabel中是鼠标事件 1.2. enterevent 鼠标进入 1.3. leaveevent 鼠标离开 1.4. 鼠标按下MyLabel::mousePressE ...

  9. Go语言学习之7 接口实例、终端文件读写、异常处理

    本节主要内容: 1. 终端读写2. 文件读写3. 命令行参数4. Json5. 自定义错误 1. 终端读写 操作终端相关文件句柄常量    os.Stdin:标准输入    os.Stdout:标准输 ...

随机推荐

  1. SQL多表查询,消除表中的重复的内容

    看到朋友再写一个SQL语句:两个表a1表中有SN.SN2.TN,b1表有SM.SM2.TN2,若a1的SN中的数据和b1的SM中的数据是一致的,那么将a1中对应的数据修改成b1中对应的数据. upda ...

  2. SQL添加维护 计划失败

    在sql要求数据库每天自动备份这个是大家都会遇到的问题,我遇到了这个问题如图: 是因为这个服务组件没有安装

  3. oracle 存储过程的用法

    create or replace procedure  myPro(inParams in number,outParams out number) is str varchar(200); beg ...

  4. 土地购买(bzoj 1597)

    Description 农夫John准备扩大他的农场,他正在考虑N (1 <= N <= 50,000) 块长方形的土地. 每块土地的长宽满足(1 <= 宽 <= 1,000, ...

  5. Lintcode 150.买卖股票的最佳时机 II

    ------------------------------------------------------------ 卧槽竟然连题意都没看懂,百度了才明白题目在说啥....我好方啊....o(╯□ ...

  6. 《Python数据分析》环境搭建之安装Jupyter工具(一)

    (免责声明:本文档是针对Python有经验的用户,如果您对Python了解很少,或者从未使用,建议官方教程用Anaconda安装) 前期准备:Python环境 虽然Jupyter可以运行多种编程语言, ...

  7. jq 模板

    菜鸟教程1.4.6版本angularJS <script src="http://apps.bdimg.com/libs/angular.js/1.4.6/angular.min.js ...

  8. google开发者工具调试技巧

    http://blog.sina.com.cn/s/blog_60a4fcef0102v3vt.html

  9. find out the neighbouring max D_value by counting sort in stack

    #include <stdio.h> #include <malloc.h> #define MAX_STACK 10 ; // define the node of stac ...

  10. JavaScript(二) DOM

    当网页被加载时,浏览器会创建页面的文档对象模型(Document Object Model)通过 HTML DOM,可访问 JavaScript HTML 文档的所有元素. 通过 id 查找 HTML ...