a trick in reading and storing file in the exact way!
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!的更多相关文章
- 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: ...
- Java – Reading a Large File Efficiently--转
原文地址:http://www.baeldung.com/java-read-lines-large-file 1. Overview This tutorial will show how to r ...
- 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 ...
- 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, ...
- load file within a jar
String examplejsPrefix = "example"; String examplejsSuffix = "js"; String exampl ...
- Python File I/O
File is a named location on disk to store related information. It is used to permanently store data ...
- File I/O
File I/O Introduction We'll start our discussion of the UNIX System by describing the functions ...
- awk -f program.file 功能使用
一.awk -f program.file 功能使用 一直没有使用过awk的-f功能,感觉鸡肋,不是很实用,更多的是因为没有需求的原因 下面介绍下awk -f的使用方法 awk可以指定默认的文件路径, ...
- Ubuntu下启动 Redis时, 提示 "Can't open the log file: Permission denied failed"
问题来源:在删除var目录下的log文件时,将redis文件夹删除了.然后在重启时:/etc/init.d/redis-server start,提示: Starting redis-server: ...
随机推荐
- java按照集合中元素的属性进行排序示例代码
public class Student { private String name; private int age; private int id; public Student() { sup ...
- unlocker208安装之后看不到Apple macos选项,解决办法.
前段时间升级了win10,最新的unlocker208安装以后看不到mac os的选项,为什么呢?我们在管理员允许win-install.cmd的过程中,会在cmd中看到这么一句话:LookupErr ...
- Rouh set 入门知识2(基础定义篇)
接上一篇,简单说明一下知识库的关系,设K1=(U,S1)和K2=(U,S2)为知识库 1.如果IND(S1)=IND(S2),即U/IND(S1)=U/IND(S2),则知识库K1与知识库K2是等价的 ...
- SQL常用的语句和函数
order by 的数值型灵活使用 select * from table_a where order by decode(函数,'asc',1,'desc',-1)*jsny; 控制试图的访问时间: ...
- React组件的生命周期各环节运作流程
'use strict'; React.createClass({ //1.创建阶段 getDefaultProps:function(){ //在创建类的时候被调用 console.log('get ...
- 【转】到底EJB是什么
[转]到底EJB是什么 到底EJB是什么?被口口相传的神神秘秘的,百度一番,总觉得没有讲清楚的,仍觉得一头雾水.百度了很久,也从网络的文章的只言片语中,渐渐有了头绪. 用通俗话说,EJB就是:&quo ...
- 脚本动态监控input
Jquery $('input').bind('input propertychange', function() { //进行相关操作 }); JS if(isIE) { document.getE ...
- 利用反射把数据集合转换成List
---ResultSet数据集 public static List toList(ResultSet rs, Class cls) { List list = new ArrayList(); tr ...
- asp.net mvc4 使用java异步提交form表单时出现[object object] has no method ajaxSubmit
最近接手了一个单子,说大不大,只是功能不少,开发过程中遇到该问题 先看脚本截图: 本以为是笔误,哪儿写错了,可是看来看去,都没发现有不合适的地方,对比过网上很多代码,都差不多,于是各种方式的,各种原因 ...
- PHP上传原理及应用
概要 1.FORM表现enctype属性 2.$_FILES系统函数 3.move_uploaded_file函数 4.is_uploaded_file函数 1.FORM标签的enctype属性 只有 ...