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: ...
随机推荐
- win主机用web.config和httpd.ini实现301重定向
当你准备好好看这篇文章的时候,你应该已经知道了301重定向的作用与意义了,那么这里就不多加解释了. 那么我唯一想提的就是关于域名带与不带www的区别,并且301重定在其中的意义,详情:域名带与不带ww ...
- 一致性哈希(Consistent Hash)
http://blog.csdn.net/cywosp/article/details/23397179/ http://www.codeproject.com/Articles/56138/Cons ...
- java编程思想-异常
DynamicFields类的setField方法里面的getField方法抛出的异常NoSuchFieldException 为什么是throw new RuntimeException(e);
- 在oracle中怎么把一张表的数据插入到另一张表中
把table2表的数据插入到table1中 insert into table1 select * from table2
- JavaScript HTML DOM 元素(节点)
JavaScript HTML DOM 元素(节点) 创建新的 HTML 元素 创建新的 HTML 元素 如需向 HTML DOM 添加新元素,您必须首先创建该元素(元素节点),然后向一个已存在的元素 ...
- 使用highlight.js高亮你的代码
在逛别人的博客的时候,看见别人的代码的例子使用了高亮的语法,无论是java,js还是php等等语言,都会自动的对关键字进行高亮. 于是在前几天自己写了一个博客,遇到code时,自然就想到了别人网站如何 ...
- X-Plane飞行模拟器购买安装
要玩起X-Plane第一个步骤当然是购买了,要购买其实非常简单,只需要一张能够支持MasterCard或者其他外币结算的信用卡,在http://www.x-plane.com/官网上购买即可,比逛淘宝 ...
- Adapter 模式
在实际软件系统设计和开发中,会经常遇到这种问题:我们为了完成某项工作购买了一个第三方的库来加快开发. 这就带来了一个问题: 我们在应用程序中已经设计好了接口,与这个第三方提供的接口不一致,为了使得这些 ...
- xml程序 个人练习1
package cn.gdpe.xml2; import java.io.File;import java.io.FileOutputStream;import java.util.List; imp ...
- windows core audio apis
这个播放流程有一次当初不是很理解,做个记录,代码中的中文部分,原文档是有解释的:To move a stream of rendering data through the endpoint buff ...