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. Java设计模式05:常用设计模式之原型模式(创建型模式)

    1. Java之原型模式(Prototype Pattern)     原型模式属于对象的创建模式.通过给出一个原型对象来指明所有创建的对象的类型,然后用复制这个原型对象的办法创建出更多同类型的对象. ...

  2. css 权威指南笔记(一)

    零零散散接触css将近5年,俨然已经成为一个熟练工.如果不是换份工作,我不知道自己差的那么远:在qunar的转正review中我这种“知其然而不知其所以然” 的状况被标明,我才意识到我已停步不前近两年 ...

  3. error: device not found - waiting for device -

    执行 cocos run -p android 时报的这个错误 连接上 android 手机, 手机开启开发者模式.  设置--其他高级设置--开发者选项--USB 调试

  4. LINQ高级编程 笔记

    相关资料:http://www.cnblogs.com/lifepoem/archive/2011/12/16/2288017.html 1.什么是LINQ 语言集成查询是一系列标准查询操作符的集合, ...

  5. 脚本动态监控input

    Jquery $('input').bind('input propertychange', function() { //进行相关操作 }); JS if(isIE) { document.getE ...

  6. 利用抽象、多态实现无反射的绿色环保ORM框架

    最近一直在忙新公司的基础库建设,对系统架构.开发框架及快速开发平台的设计实施都积累了一定的实践经验. 一般的中小型的软件开发公司,如果按照技术储备来衡量软件项目的技术含量的评定依据是可行的.但如果光是 ...

  7. C# 异步操作

    在程序中,普通的方法是单线程的.但中途如果有大型的操作,比如读取大文件,大批量操作数据库,网络传输等,都会导致程序阻塞,表现在界面上就是程序卡或者死掉,界面元素不动了,不响应了.C#异步调用很好的解决 ...

  8. spring data jpa Specification 例子

    /** * 封装查询条件 * * @param baseQueryDTO * @return */ private Specification<ActivityBase> getSpeci ...

  9. Lua-C交互函数

    lua_gettable(lua_State * , tableIndex) //获取表的在key位置的值 过程:tableIndex为表在栈的位置,例:-2为第二个位置 , 此时会弹(出)栈作为参数 ...

  10. Web::Scraper 页面提取分析

    一组用来提取HTML文档中元素内容的工具集,它能够理解HTML和CSS选择器以及XPath表达式. 语法 use URI; use Web::Scraper; # First, create your ...