练习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. 2019 沈阳网络赛 Fish eating fruit

    这题看了三个月,终于过了,第一次看的时候没学树形DP,想用点分治但是不会 后来学了二次扫描,就有点想法了.... 这东西也真就玄学了吧... #include<iostream> #inc ...

  2. cocos2dx 仿射变换

    AffineTransform __CCAffineTransformMake(float a, float b, float c, float d, float tx, float ty) { Af ...

  3. <算法><go实现>左括号补全-双栈法

    输入:1+2)*33-44)*555-666))) 输出:((1+2)*((33-44)*(555-666))) 代码实现及注释: package main import "fmt" ...

  4. JAVA字节码文件之结构

    开发工具:IEDA.JDK1.8.WinHex 一.字节码文件结构 源代码 package com.jalja.java.bytecode; /** * @Auther: XL * @Date: 20 ...

  5. Java容器知识总结

    剖析面试最常见问题之Java集合框架 说说List,Set,Map三者的区别? List(对付顺序的好帮手): List接口存储一组不唯一(可以有多个元素引用相同的对象),有序的对象 Set(注重独一 ...

  6. 开箱即用!使用Rancher 2.3 启用Istio初体验

    本文来自Rancher Labs Rancher的理念是Run Kubernetes Everywhere,Rancher 2.3中许多重大更新,让这一理念的实现又向前一步. 其中,最重要的两个特性是 ...

  7. Date类与日期格式

    Date类概述: 表示特定的瞬间,精确到毫秒. Date()分配 Date 对象并初始化此对象,以表示分配它的时间(精确到毫秒).Date(long date)分配 Date 对象并初始化此对象,以表 ...

  8. MariaDB的备份与主从、高可用实践

    1.编写脚本,支持让用户自主选择,使用mysqldump还是xtraback全量备份. [root@test-centos7-node1 scripts]# cat chose_backup_mysq ...

  9. 图解kubernetes服务打散算法的实现源码

    在分布式调度中为了保证服务的高可用和容灾需求,通常都会讲服务在多个区域.机架.节点上平均分布,从而避免单点故障引起的服务不可用,在k8s中自然也实现了该算法即SelectorSpread, 本文就来学 ...

  10. 初探ASP.NET Core 3.x (1) - 关于ASP.NET Core

    I 什么是ASP.NET Core ASP.NET is an open source web framework, created by Microsoft, for building modern ...