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: ...
随机推荐
-
在Spring中轻松写日志
最近觉得写的一点代码(JAVA),还觉得颇为自得,贡献出来供大家参考. 首先,先上代码: @Controller public class Controller1{ @WriteLog(value = ...
-
Delphi系统变量:IsMultiThread对MM的影响
前几日,调试一BUG,过程先不说,最后调试到MM,即Debug dcu,然后进入到GetMem.inc中的Get/FreeMem函数处后,出现AV. 然后一通找...郁闷了N天,后来发现将MM切换到Q ...
-
【原创】解决jquery在ie中不能解析字符串类型xml结构的xml字符串的问题
$.fn.extend({ //此方法解决了ie中jquery不识别非xml的类型的xml字符串的问题 tony tan findX: function (name) { if (this & ...
-
何时使用hadoop fs、hadoop dfs与hdfs dfs命令(转)
hadoop fs:使用面最广,可以操作任何文件系统. hadoop dfs与hdfs dfs:只能操作HDFS文件系统相关(包括与Local FS间的操作),前者已经Deprecated,一般使用后 ...
-
Codeforces Round #331 (Div. 2)
水 A - Wilbur and Swimming Pool 自从打完北京区域赛,对矩形有种莫名的恐惧.. #include <bits/stdc++.h> using namespace ...
-
【官方文档】《暗黑世界V1.4》API说明!
指令号说明 账号注册 100 { username str 用户名 password str 密码 } 返回信息 { result bool 指令调用是否成功 ...
-
怎样解决xcode里开发cocos2dx改动lua脚本后不刷新的问题
用xcode来开发cocos2dx,结果发现一个非常纠结的问题,假设我一旦改动了一个Lua文件,我必须clean之后再build,否则改动的Lua文件不会体现出来.这是一个非常令纠结的结果,特别是我要 ...
-
PostgreSQL学习手册
事实上之前有很长一段时间都在纠结是否有必要好好学习它,但是始终都没有一个很好的理由说服自己.甚至是直到这个项目最终决定选用PostgreSQL 时,我都没有真正意识到学习它的价值,当时只是想反正和其它 ...
-
python 自动认证登录
import urllib import base64 import urllib2 def auto_login(urllink,username,password): authstr = 'Bas ...
-
Luogu1486郁闷的出纳员【Splay】
P1486 郁闷的出纳员 题目描述 OIER公司是一家大型专业化软件公司,有着数以万计的员工.作为一名出纳员,我的任务之一便是统计每位员工的工资.这本来是一份不错的工作,但是令人郁闷的是,我们的老板反 ...