C++:file.seekg()似乎不会返回当前位置
C++: file.seekg() does not appear to return current location
我正在尝试备份ifstream中的一行。file.tellg()
正在返回一个我没有预料到的值。在下面的例子中,在读取第一行(字符串长度为15个字符)后,我期望file.tellg()返回16。相反,它正在返回41。有人能就这种行为提供一些见解吗?
test.cpp
#include <fstream>
#include <ios>
#include <string>
#include <iostream>
using namespace std;
int main(){
ifstream file("sample.ics", ios::in);
string line;
string key0;
string key1;
string value0;
string value1;
getline(file, line, 'n');
cout << "line = " << line << endl;
cout << "line.length = " << line.length() << endl; // should be 15;
cout << "Attempt:" << endl;
int pos = file.tellg(); // should be 16;
cout << " pos = " << pos << endl;
getline(file, key0, ':');
getline(file, value0, 'n');
cout << " First:" << endl;
cout << " " << key0 << ":" << value0 << endl;
cout << " backing up..." << endl;
file.seekg(pos, ios_base::beg);
getline(file, key1, ':');
getline(file, value1, 'n');
cout << " Second:" << endl;
cout << " " << key1 << ":" << value1 << endl;
file.close();
}
输出:
line = BEGIN:VCALENDAR
line.length = 15
Attempt:
pos = 41
First:
CALSCALE:GREGORIAN
backing up...
Second:
ION:2.0
sample.ics
BEGIN:VCALENDAR
CALSCALE:GREGORIAN
VERSION:2.0
METHOD:PUBLISH
...
尝试以二进制模式打开文件,看看是否得到相同的结果:
ifstream文件("sample.ics",ios:binary);
相关文章:
- 这个返回元素位置的基于循环的函数有什么问题?
- 如何使此递归函数从给定的起始位置返回最小的整数?
- 返回员工和工资(给定位置子字符串)
- 函数,返回变量c++占用的内存位置的大小
- 将表示位置的整数分解为多个部分,然后再返回
- 提取字符后返回到文件中的特定位置
- NetUsergetLocalGroups()返回存储位置而不是LPWSTR字符串
- 在我的 BST 实现中,我的 findNode 函数没有将父位置返回给其调用函数
- qt如何连接QPushButton的2D数组并返回位置
- C/C++ 从根位置搜索文件夹并返回绝对路径
- 为什么在递增后使用 [] 运算符访问指针数组会返回错误地址的当前内存位置
- ifstream::is_open 返回 true,即使该位置不存在文件也是如此
- 在构造函数以外的任何其他位置访问相机时,我的相机指针返回 null
- 新位置的返回值
- GLSL属性位置返回-1
- 为什么GET_X_LPARAM确实返回鼠标滚轮上的绝对位置
- 返回从字符串末尾 (C++) 之后的位置开始的子字符串
- 删除返回字符*的位置
- c++抛出0xC0000005:写入位置0xFEEEFEEE的访问冲突.从main返回时
- 如何将位置返回为 x,y,z