练习c++primer中关于输入输出流的操作。

任务是从固定格式的forreading.txt文档中读取相应的数据,转存到forwriting.txt中去。

forreading.txt 格式如下:

(12)(13)(34)(1099)
(23)(28)(29)(25)
(32)(45)
(123)
(120)(333)(1)(8)
(34)(45)
(90)(110)

希望读取其中数字,并以空格为间隔符号存在forreading当中,代码如下:

/*针对的数据格式如下*/
/*(23)(26) */
/*(34)(45)(67)(89) */
#include <iostream>
#include <string>
#include <fstream>
#include <sstream>
#include <vector>
#include <stdio.h>
#include <stdlib.h>
using namespace std; int main()
{
ifstream infile;
infile.open( "forreading.txt" );
ofstream outfile;
string line, word,temp_str;
vector<vector<string> > a;
bool flag_begin{ false }, flag_end{ false }; while (getline(infile, line))
{
vector<string> a_i;
istringstream stream(line);
while (stream >> word)
{
for (string::iterator iter = word.begin(); iter != word.end(); iter++)
{
char llabel = 40,rlabel=41;//这两个数值对应着“()”的asc码
if (*iter == llabel)
{
flag_begin = true; flag_end = false; continue;
}
if (*iter == rlabel)
{
flag_end = true; flag_begin = false;
} if ((flag_begin == true) && flag_end == false)
{
temp_str.push_back(*iter);
}
if ((flag_begin == false) && flag_end == true)
{
a_i.push_back(temp_str);//这里仍然无法实现将string转换成数字,而是以string的形式保存下来
temp_str.clear();
/*a = atoi(temp_str);*/
}
}
}
a.push_back(a_i);
a_i.clear();
}
outfile.open("forwriting.txt");
for (vector<vector<string> >::iterator iter_out = a.begin(); iter_out != a.end(); iter_out++)
{
for (vector<string>::iterator iter_iner = (*iter_out).begin(); iter_iner != (*iter_out).end(); iter_iner++)
{
cout << *iter_iner << " ";
outfile << *iter_iner << " ";
}
cout << endl;
outfile << "\n";
}
return 0;
}

在Ubuntu中makefile内容为:

edit:readtxtfile.o
g++ -o edit readtxtfile.o
readtxtfile.o:readtxtfile.cpp
g++ -c readtxtfile.cpp
clean :
rm readtxtfile.o

输出的forwriting.txt为:

12 13 34
23 28 29 25
32

总结:这里仍然要根据txt的具体组织格式去读取,并不能通用。另外,从string变量直接转换成int变量的方法还有待探究。个人感觉用c的处理方法可能会更方便一些,有时间可以做一下尝试。

使用c++标准IO库实现txt文本文件的读与写操作的更多相关文章

  1. 18、标准IO库详解及实例

    标准IO库是由Dennis Ritchie于1975年左右编写的,它是Mike Lestbain写的可移植IO库的主要修改版本,2010年以后, 标准IO库几乎没有进行什么修改.标准IO库处理了很多细 ...

  2. [APUE]标准IO库(下)

    一.标准IO的效率 对比以下四个程序的用户CPU.系统CPU与时钟时间对比 程序1:系统IO 程序2:标准IO getc版本 程序3:标准IO fgets版本 结果: [注:该表截取自APUE,上表中 ...

  3. [APUE]标准IO库(上)

    一.流和FILE对象 系统IO都是针对文件描述符,当打开一个文件时,即返回一个文件描述符,然后用该文件描述符来进行下面的操作,而对于标准IO库,它们的操作则是围绕流(stream)进行的. 当打开一个 ...

  4. ca75a_c++_标准IO库-利用流对象把文件内容读取到向量-操作文件

    /*ca75a_c++_标准IO库习题练习习题8.3,8.4,8.6习题8.9.8.10 ifstream inFile(fileName.c_str());1>d:\users\txwtech ...

  5. 文件IO函数和标准IO库的区别

    摘自 http://blog.chinaunix.net/uid-26565142-id-3051729.html 1,文件IO函数,在Unix中,有如下5个:open,read,write,lsee ...

  6. C++ Primer 读书笔记: 第8章 标准IO库

    第8章 标准IO库 8.1 面向对象的标准库 1. IO类型在三个独立的头文件中定义:iostream定义读写控制窗口的类型,fstream定义读写已命名文件的类型,而sstream所定义的类型则用于 ...

  7. 高级UNIX环境编程5 标准IO库

    标准IO库都围绕流进进行的 <stdio.h><wchar.h> memccpy 一般用汇编写的 ftell/fseek/ftello/fseeko/fgetpos/fsetp ...

  8. c++ primer 学习杂记3【标准IO库】

    第8章 标准IO库 发现书中一个错误,中文版p248 流状态的查询和控制,举了一个代码例子: int ival; // read cin and test only for EOF; loop is ...

  9. C++Primer,C++标准IO库阅读心得

    IO 标准库类型和头文件 iostream istream 从流中读取 ostream 写到流中去 iostream 对流进行读写:从 istream 和 ostream 派生而来fstream if ...

随机推荐

  1. jquery中报错Uncaught ReferenceError: $ is not defined的解决办法

    jquery中报错提示为:Uncaught ReferenceError: $ is not defined 这个错误的原因就是你没有引入jquery库文件或者引入的路径不对造成的

  2. 使用git pull拉取代码的时候,无法拉取最新代码,报"unable to update local ref"错误。

    使用git pull拉取代码的时候,无法拉取最新代码,报"unable to update local ref"错误. 除了重新clone一份代码外,还可以使用如下解决方案: .切 ...

  3. 洛谷$P$3746 [六省联考2017]组合数问题 $dp$+矩乘+组合数学

    正解:$dp$+矩乘+组合数学 解题报告: 传送门! 首先不难发现这个什么鬼无穷就是个纸老虎趴,,,最多在$\binom{n\cdot k+r}{n\cdot k}$的时候就已经是0了后面显然不用做下 ...

  4. 第 426 期 Python 周刊

    文章,教程和讲座 端到端机器学习:从数据收集到模型部署 链接: https://ahmedbesbes.com/end-to-end-ml.html 在本文中,我们将完成构建和部署机器学习应用程序的必 ...

  5. 关于Mac VMFusion Centos7虚拟机网络的配置

    1.环境配置: 创建完快照后启动虚拟机,使用root用户和root密码登录系统 1.1 停止防火墙 #停止防火墙 [root@localhost ~]#systemctl stop firewalld ...

  6. 有哪些让人相见恨晚的Python库(一)

    对于我这个经常用python倒腾数据的人来说,下面这个库是真·相见恨晚 记得有一次我在服务器上处理数据时,为了解决Pandas读取超过2000W条数据就内存爆炸的问题,整整用了两天时间来优化.最后通过 ...

  7. 小程序中的pick

    picker:从底部弹起的滚动选择器. 属性:model  string类型  说明:选择器类型 : selector  普通选择器 multiSelector   多列选择器 time   时间选择 ...

  8. poj 2689 区间素数筛

    The branch of mathematics called number theory is about properties of numbers. One of the areas that ...

  9. Mybatis中jdbcType的类型

    具体支持的类型参见:org.apache.ibatis.type.JdbcType ARRAY, BIT, TINYINT, SMALLINT, INTEGER, BIGINT, FLOAT, REA ...

  10. 【智能合约】编写复杂业务场景下的智能合约——可升级的智能合约设计模式(附Demo)

    智能合约的现状 以太坊在区块链上实现了智能合约的概念,用于:同质化通证发行(ERC-20).众筹.投票.存证取证等等,共同点是:合约逻辑简单,只是业务流程中的关键节点,而非整个业务流程.而智能合约想解 ...