如何收集和存储tellp(),tellg()返回类型? [英] How to collect and store tellp(), tellg() return types?

查看:440
本文介绍了如何收集和存储tellp(),tellg()返回类型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在写一个程序,在一个文件中维护一个linked_list。所以我遍历文件,通过使用tellp()/ tellg()并将其添加到一个特定的长整数(可以看作是一个偏移量)到达新的位置。



一个简单的例子是

  long next_offset = sizeof(long)+ sizeof b $ b //像记录中所有元素的大小,等等

curr_node = out.seekg();
while(curr_node!= -1){
out.read(...);
** curr_node.seekg(curr_node.tellp()+ next_offset); **
out.read((char *)& curr_node,sizeof(long));
}

所以这里基本上我保存tellp另外,这是罚款?或者有一个机会,当pos_value变大时,我可能会丢失一些位

解决方案

tellg(streampos)是为了在你使用的平台中存储最大文件大小,所以不能保证它们能够容纳很长时间。实际上,它们可能不适合size_t,long long或最大的基本类型,因为磁盘存储空间通常比地址空间大几个数量级,而基本类型是随后定义的。 p>

但是IMO的实际做法是不要使用另一个更大的类型,除非你正在编写一个真正需要处理巨大文件(如DBMS等)的程序,这是罕见的情况。如果最大预期大小适合长期,只需在程序中设置安全措施,拒绝创建或处理大于该大小的文件即可。



另一方面,如果确实如此你可能必须处理巨大的文件,那么事情变得更复杂,而一个全面的答案将更长。


I am writing a program that maintains a linked_list in a file. So I traverse across the file, by using tellp()/tellg() and adding it to a particular long integer(can be seen as an offset) to get to the new location.

An simple example would be

   long next_offset =  sizeof(long) + sizeof(int) .... 
   //like size of all the elements in the record, etc

   curr_node = out.seekg();
   while(curr_node != -1) {
          out.read(...);
          **curr_node.seekg(curr_node.tellp() + next_offset);**
          out.read((char *)&curr_node,sizeof(long));
   }

so here basically I am saving tellp() value as long and dng an long addition, is this fine?? or is there a chance that I might lose some bits when the pos_value gets big???

解决方案

These values returned by tellp/tellg (streampos) are meant to store the maximum file size in the platform you're using, so there's no guarantee they will fit in a long. In fact, they may not fit in a size_t, long long, or whatever is the biggest fundamental type, since disk storage space is usually orders of magnitude larger than address space, and the fundamental types were defined with the later in mind.

But IMO the practical thing to do is not to use another, bigger type unless you're writing a program that really needs to handle huge files (like a DBMS or so), which is a rare case. If the maximum expected size fits in a long, simply put safeguards in your program to refuse to create or handle files bigger than that.

On the other hand, if that's indeed the case that you may have to handle enormous files, then things got a lot more complicated, and a comprehensive answer would be longer.

这篇关于如何收集和存储tellp(),tellg()返回类型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆