Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/129.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/silverlight/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ C++;如何加/减tellp(),tellg()返回_C++_Iostream_Ofstream - Fatal编程技术网

C++ C++;如何加/减tellp(),tellg()返回

C++ C++;如何加/减tellp(),tellg()返回,c++,iostream,ofstream,C++,Iostream,Ofstream,假设我想得到两个tellp()输出之间的差值(int) 如果编写了一个大文件,那么tellp()输出可能会很大,因此说将其存储在长文件中是不安全的。是否有一种安全的方法来执行这样的操作: ofstream fout; fout.open("test.txt",ios::out | ios::app); int start = fout.tellp(); fout<<"blah blah "<<100<<","<<3.14; int end = fo

假设我想得到两个tellp()输出之间的差值(int)

如果编写了一个大文件,那么tellp()输出可能会很大,因此说将其存储在长文件中是不安全的。是否有一种安全的方法来执行这样的操作:

ofstream fout;
fout.open("test.txt",ios::out | ios::app);
int start = fout.tellp();
fout<<"blah blah "<<100<<","<<3.14;
int end = fout.tellp();
int difference = end-start;
流源的
;
fout.open(“test.txt”,ios::out | ios::app);
int start=fout.tellp();

foutstream::tellp(和
ifstream::tellg
)的
返回类型是一个
char\u traits::pos\u类型。除非您真的需要最终结果是
int
,否则您可能希望在整个过程中使用
pos\u type
。如果您确实需要最终结果作为
int
,您可能仍然希望将中间值存储在
pos\u type
s中,然后执行减法并将结果转换为
int

typedef std::char_traits<char>::pos_type pos_type;

ofstream fout;
fout.open("test.txt",ios::out | ios::app);
pos_type start = fout.tellp();
fout<<"blah blah "<<100<<","<<3.14;
pos_type end = fout.tellp();
int difference = int(end-start);
// or: pos_type difference = end-start;
typedef std::char_traits::pos_type pos_type;
流式流量计;
fout.open(“test.txt”,ios::out | ios::app);
pos_type start=fout.tellp();

fout
long
足够几个EB。您计划支持多大的文件?将其转换为长-长格式似乎非常难看,这种返回类型没有内置的加减法?文件的最大容量应为1GB。对于这一点,似乎一个long就足够了?对于1GB来说,
long
确实足够了。1GB应该对每个人都足够了。