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: ...
随机推荐
- Cocos2d各版本搭建环境中的奇葩操作
#Version: Cocos2d-x 3.4 Android 将[Cocos2d-x解压目录]\cocos\platform\android\java\src中的com,org目录拷贝覆盖到[项目根 ...
- [转] 考验你的JavaScript底细
http://sentsin.com/ 尽管今日的JavaScript已经突飞猛进,但JS的许多特性仍然保留,以下题目并不是有意设坑,许多地方将验证你的JS底细,如果错了一半,请别告诉我你从事前端. ...
- Android Studio安装及首次运行遇到的问题
Android Studio,下载地址:http://developer.android.com/sdk/index.html.需要注意的是Android Studio需要JDK 1.7+才可以安装, ...
- mvc4+jquerymobile页面加载时无法绑定事件
问题:在view里写js,在页面第一次加载完成后,无法触发事件, 如:按钮click事件,已经在$(function(){ 添加了click });但就是无法触发,必须刷新下才可以. 原因分析: 主 ...
- Difference Between XML and XAML.
XML, or Extensible Markup Language, is a subset of the more complex SGML (Standard Generalized Mark ...
- 在eclipse下面搭建Clojure开发运行环境
打开eclipse,点击菜单栏“help->Install New Software...", 然后,点击”add“, 在Location处输入 http://ccw.cgrand.n ...
- ios 键盘弹起
#pragma mark 键盘弹起操作 - (void)keyboardWillShow:(NSNotification *)notification{ NSDictionary *info = ...
- 苹果App store 2015最新审核标准公布(2015.3)
苹果近日更新了AppStore审核指南的相关章节,对此前版本进行了修改和完善.除了增加应用截图.预览等限制外,使用ApplePay进行定期付款的应用程序必须展示每个阶段所需款额,费用归属以及如何取消. ...
- 图的遍历(bfs 和dfs)
BFS的思想: 从一个图的某一个顶点V0出发,首先访问和V0相邻的且未被访问过的顶点V1.V2.……Vn,然后依次访问与V1.V2……Vn相邻且未被访问的顶点.如此继续,找到所要找的顶点或者遍历完整个 ...
- Java学习笔记——动态代理
所谓动态,也就是说这个东西是可变的,或者说不是一生下来就有的.提到动态就不得不说静态,静态代理,个人觉得是指一个代理在程序中是事先写好的,不能变的,就像上一篇"Java学习笔记——RMI&q ...