c++如何加/减tellg(), tellg()返回
C++ how to add/subtract tellp(), tellg() return
假设我想获得两个tell()输出之间的差值(以int表示)。
如果写入一个大文件,tell()的输出可能非常大,所以说将它存储在一个long long中是不安全的。是否有一种安全的方法来执行这样的操作:
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;
在这里,我知道end和start之间的区别完全可以放在int类型中。但是end and start本身可能非常大
从ofstream::tellp
(和ifstream::tellg
)返回类型为char_traits<char>::pos_type
。除非您确实需要最终结果是int
,否则您可能希望始终使用pos_type
。如果你确实需要最终结果为int
,你可能仍然希望将中间值存储在pos_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;
相关文章:
- 来自 std::list 的迭代器 .end() 按预期返回"0xcdcdcdcdcdcdcdcd"但 .begin()
- 什么时候在C++中返回常量引用是个好主意
- 你能重载对象变量名本身返回的内容吗
- 为什么 Serial.println(<char[]>);返回随机字符?
- C++映射:具有自定义类的运算符[]不起作用(总是返回0)
- 如何获取std::result_of函数的返回类型
- QueryWorkingSet总是返回false
- (C++)分析树以计算返回错误值的简单算术表达式
- 访问者访问变体并返回不同类型时出错
- 如何返回一个类的两个对象相加的结果
- 为什么函数 tellg() 没有返回好的结果?
- 查找二进制文件的大小,函数 tellg() 返回 -1
- tellg() 只对小文件返回 -1
- C 从文件中读取FSTREAM数据不会返回正确的值.inputfile.tellg返回-1
- GCC 4.7 istream::tellg() 在达到 EOF 后返回 -1
- 为什么 fstream::tellg() 返回值会因输入文本文件中的换行符数而增大,当文件针对 Windows (r
- 如何收集和存储 tellp(), tellg() 返回类型
- c++如何加/减tellg(), tellg()返回
- Istream::tellg()与我的自定义流buf类一起使用时返回-1
- C++ tellg() 返回类型