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. Android推送技术研究

    前言 最近研究Android推送的实现, 研究了两天一夜, 有了一点收获, 写下来既为了分享, 也为了吐槽. 需要说明的是有些东西偏底层硬件和通信行业, 我对这些一窍不通, 只能说说自己的理解. 为什 ...

  2. Shell - 文件运算符

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

  3. sql常用的日期函数与应用

    --本周第一天 ),getdate()) --or ,) --本周第一天 ,) --上月第一天 ),,,) --上月最后一天 ),,,)),)+' 23:59:59' --本月第一天 ,getdate ...

  4. C#中的Dictionary简介

    简介在C#中,Dictionary提供快速的基于兼职的元素查找.当你有很多元素的时候可以使用它.它包含在System.Collections.Generic名空间中. 在使用前,你必须声明它的键类型和 ...

  5. index full scan/index fast full scan/index range scan

    **************************1************************************* 索引状态:          valid.      N/A .    ...

  6. iOS定位问题解决方案

    在需要用到定位服务时,需在info文件中加入: 1.NSLocationWhenInUseUsageDescription(类型为:string,值为:”我们需要通过您的地理位置信息获取您周边的相关数 ...

  7. C# div布局

    本文讲解使用DIV+CSS布局最基本的内容,读完本文你讲会使用DIV+CSS进行简单的页面布局. 转载请标明:http://www.kwstu.com/ArticleView/divcss_20139 ...

  8. JS实现页面跳转重定向的几种方式

    1.重定向 <script language="javascript"type="text/javascript">  window.locatio ...

  9. Codeforces Round #287 D.The Maths Lecture

    The Maths Lecture 题意:求存在后缀Si mod k =0,的n位数的数目.(n <=1000,k<=100); 用f[i][j]代表 长为i位,模k等于j的数的个数. 可 ...

  10. 【HDU3487】【splay分裂合并】Play with Chain

    Problem Description YaoYao is fond of playing his chains. He has a chain containing n diamonds on it ...