C++ C++;如何加/减tellp(),tellg()返回
假设我想得到两个tellp()输出之间的差值(int) 如果编写了一个大文件,那么tellp()输出可能会很大,因此说将其存储在长文件中是不安全的。是否有一种安全的方法来执行这样的操作: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
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();
foutlong
足够几个EB。您计划支持多大的文件?将其转换为长-长格式似乎非常难看,这种返回类型没有内置的加减法?文件的最大容量应为1GB。对于这一点,似乎一个long就足够了?对于1GB来说,long
确实足够了。1GB应该对每个人都足够了。