read and write file is a very common operation regarding file mainuplation.

However, the powerfull getline only can read line by line(with new line character '\n' as delimiter).

Inorder to write the line back into file, we often have to add '\n' at the last of each line.

However, in this way we can add extra '\n' character compared to the original file.

To avoid this inaccuracy, may be not a big deal in a common situation, but I have tested that an extra '\n' at *.tgz file can infere the untar of it.

I suggest the following way to read and write file in exact way, without adding any extra character.

The key idea:

Since we should not add '\n' at the last line of reading file, we can avoid this by defering the time of add '\n' by using pre_line and buffer_line.

only this is a new line available(buffer_line), we append the '\n' character to the pre_line. Otherwise, it is the lat line, we should write it directly into the outstream without appeding the '\n' character.

coding sample:

ofstream out;

out.open(obj_path.c_str());

string pre_line;

string buffer_line;

getline(cin, pre_line);

while (1) {

if (getline(cin, buffer_line)) {

pre_line += '\n';  /*pre_line + '\n' if its next line is not the last line*/

out << pre_line;

pre_line = buffer_line;

} else{

out << pre_line;/*the pre_line is the last new, no need to add '\n'*/

break;

}

}

out.close();

a trick in reading and storing file in the exact way!的更多相关文章

  1. Reading Lines from File in C++

    Reading Lines from File in C++ In C++, istringstream has been used to read lines from a file. code: ...

  2. Java – Reading a Large File Efficiently--转

    原文地址:http://www.baeldung.com/java-read-lines-large-file 1. Overview This tutorial will show how to r ...

  3. Analysis about different methods for reading and writing file in Java language

    referee:Java Programming Tutorial Advanced Input & Output (I/O) JDK 1.4+ introduced the so-calle ...

  4. Apache POI – Reading and Writing Excel file in Java

    来源于:https://www.mkyong.com/java/apache-poi-reading-and-writing-excel-file-in-java/ In this article, ...

  5. load file within a jar

    String examplejsPrefix = "example"; String examplejsSuffix = "js"; String exampl ...

  6. Python File I/O

    File is a named location on disk to store related information. It is used to permanently store data ...

  7. File I/O

    File I/O Introduction     We'll start our discussion of the UNIX System by describing the functions ...

  8. awk -f program.file 功能使用

    一.awk -f program.file 功能使用 一直没有使用过awk的-f功能,感觉鸡肋,不是很实用,更多的是因为没有需求的原因 下面介绍下awk -f的使用方法 awk可以指定默认的文件路径, ...

  9. Ubuntu下启动 Redis时, 提示 "Can't open the log file: Permission denied failed"

    问题来源:在删除var目录下的log文件时,将redis文件夹删除了.然后在重启时:/etc/init.d/redis-server start,提示: Starting redis-server: ...

随机推荐

  1. Cocos2d-x游戏开发中的消息机制:CCNotificationCenter的使用

    在HTML5游戏开发中,js可以使用Event对象的addEventListener(添加事件监听).dispatchEvent(触发事件)实现监听机制,如果在coocos2d-x中,去实现这种机制该 ...

  2. git常见指令

    master : 默认开发分支: origin : 默认远程版本库 初始化操作    $ git config -global user.name <name> #设置提交者名字    $ ...

  3. Red Hat Enterprise Linux 6安装步骤

    首先,准备安装环境,此次实验是在VMware Workstation虚拟机环境下来实现的,下面就开始安装: 点击Create a New Vitrual Machine来新建一个虚拟机,选择自定义安装 ...

  4. Shell - 文件运算符

    文件运算符  文件运算符  描述 -b file  检测 file 是否为块设备文件 -c file  检测 file 是否为字符设备文件  -d file  检测 file 是否为目录 -e fil ...

  5. Android端上传图片到后台,存储到数据库中 详细代码

    首先点击头像弹出popwindow,点击相册,相机,调用手机自带的裁剪功能,然后异步任务类访问服务器,上传头像,保存到数据库中, 下面写出popwindow的代码 //设置popwindow publ ...

  6. VS2015使用OSChina的git功能

    好长时间没有写博了,把今天的新的记录一下. 最近开始使用vs2015,vs2015支持git平台和TF功能,因为....,我选择了OSChina的git.一开始学习的此篇文章http://my.osc ...

  7. OpenCart 之registry功用

    1. “Registry”设计模式 在OpenCart中,Registry是整个系统的信息中枢. Registry是一个单例(Singleton),在index.php起始页面中, 首先作为构造函数参 ...

  8. Orace数据库锁表的处理与总结<摘抄与总结二>

    当Oracle数据库发生TX锁等待时,如果不及时处理常常会引起Oracle数据库挂起,或导致死锁的发生,产生ORA-60的错误. TX锁等待的分析 Oracle数据库中一般使用行级锁. 当Oracle ...

  9. js做全选,用一个checkbox复选框做多个checkbox复选框的全选按钮,有一个复选框未被选择时,全选按钮的checked就为false

    用一个checkbox复选框做多个checkbox复选框的全选按钮,有一个复选框未被选择时,全选按钮的checked就为false,当所有checkbox都被选中时,全选按钮也被选中. 详解: 有两种 ...

  10. Linux nohup命令详解

    nohup命令及其输出文件                                                                                       ...