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: ...
随机推荐
- C#压缩文件为zip格式
Vercher C#压缩文件为zip格式 需要ICSharpCode.SharpZipLib.dll,网上下载的到. 代码是从网上找来的: 1 public class ZipClass 2 { ...
- int? 参数是这个的时候 是可以传入null的 而int的就不行
such as pager.CurrentPageIndex = (page != null ? (int)page : 1);
- Java-分页实例
1.PageModel.java package com.javaweb; import java.util.List; public class PageModel<E> { priva ...
- idea+maven
使用IntelliJ IDEA开发SpringMVC网站(一)开发环境 http://my.oschina.net/gaussik/blog/385697 使用IntelliJ IDEA开发Sprin ...
- java simple check whether a file or directory.
Ref: check whether a file or directory First, make sure the path exists by using: new File(path).ex ...
- 在使用Kettle的集群排序中 Carte的设定——(基于Windows)
本片文章主要是关于使用Kettle的UI界面: Spoon来实现基于集群的对数据库中的数据表数据进行排序的试验. 以及在实验过程中所要开启的Carte服务的一些配置文件的设置, 还有基于Windows ...
- CentOS 6.5下搭建NFS文件服务器
环境介绍:服务器: 192.168.0.1客户机: 192.168.0.2安装软件包:服务器和客户机都要安装nfs 和 rpcbind 软件包:yum -y install nfs-utils rpc ...
- WCF存储图片到指定文件夹下
string path = System.IO.Directory.GetCurrentDirectory() + @"\POIImages\"; Guid imgid = Gui ...
- Unity Manual 用户手册
unity3d 文档的中文网址: http://game.ceeger.com/Manual/
- ios里面如何压缩图片
在iOS里面,压缩图片跟在其他环境里面差不多,都和累死, 就是对当前图片从新画图,制定一个尺寸的问题 UIImage* image = [UIImage imageNamed:@"cat.j ...