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. ios从相册:摄像头中获取视频

    ios从相册/摄像头中获取视频 如何从相册中获取视频 使用的是一个和获取照片相同的类UIImagePickerController //相册中获取视频 - (IBAction)clickViedoOF ...

  2. [转] JavaScript 原型理解与创建对象应用

    这段时间把之前的 JavaScript 的笔记复习了一遍,又学习了一些新的内容,所以把自己的学习笔记加上个人理解在这里总结一下,并提供一个简单的应用示例,希望能帮助一些刚入门的朋友.主 要参考< ...

  3. 给Sublime Text2安装轻量级代码提示插件:SublimeCodeIntel

    步骤: 1.下载SublimeCodeIntel(地址https://github.com/SublimeCodeIntel/SublimeCodeIntel): 2.将下载的压缩包解压,并放置在Pa ...

  4. App上线基本流程

    还可参考的:http://www.cocoachina.com/bbs/read.php?tid=330302 iOS项目上传前期准备材料: 1.已有开发者账号 2.已有发布证书 3.一张1024*1 ...

  5. gulp安装

    1. npm install gulp -g    全局安装  npm install gulp --save-dev  安装文件内,纪录于package.json     接著安装插件,完成下列任务 ...

  6. Spring中@Autowired注解与自动装配

    1 使用配置文件的方法来完成自动装配我们编写spring 框架的代码时候.一直遵循是这样一个规则:所有在spring中注入的bean 都建议定义成私有的域变量.并且要配套写上 get 和 set方法. ...

  7. cocos2dx JAVA,C++互相调用函数

    C++调用JAVA 例子 #if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID) #include "platform/android/jni/Jni ...

  8. IOS DLNA PlatinumKit库的使用

    前段时间进行了IOS DLNA的开发,使用的是PlatinumKit库.网上查了很多资料都未果,经过自己的摸索,遂将如何使用PlatinumKit进行DLNA的开发分享给大家. 1.PlatinumK ...

  9. SGU 122.The book (哈密顿回路)

    题目描述 有一群人从1到N标号,而且这群人中每个人的朋友个数不少于 (N+1)/2 个. 编号为1的人有一本其他人都想阅读的书. 写一个程序,找到一种传阅顺序使得书本只经过每个人手中一次,并且一个人只 ...

  10. 如何使一个input文本框随其中内容而变化长度。

    第一:<input type="text" onkeydown="this.onkeyup();" onkeyup="this.size=(th ...