对于ifstream对象A,哪个语句将文件位置指针定位到与对象A关联的10字节文件的末尾?
A. A.seekg(0);
B. A.seekg(0, ios::end);
C. A.seekg(10, ios::end);
D. A.seekg(0, ios::beg);
不确定它是B还是C ......
答案 0 :(得分:1)
seekg(a, b)
会将阅读位置设置为b
偏移 a
字节。
因此:
A.seekg(0, ios::beg)
相当于A.seekg(0)
。A.seekg(0, ios::end)
相当于A.seekg(10)
,如果该文件是10个字节。A.seekg(10, ios::end)
相当于A.seekg(20)
。这不适用于10字节的文件。 documentation on seekg
会回答你的问题。
答案 1 :(得分:0)
谷歌搜索“c ++ seekg”(比在SO上输入问题更快,顺便提一下)在许多人中产生http://www.cplusplus.com/reference/istream/istream/seekg/。
从那里我们看到(大大删节):
istream& seekg (streamoff off, ios_base::seekdir way);
...
off: Offset value, relative to the way parameter.
way: ... any of the following constant values:
ios_base::beg beginning of the stream
ios_base::cur current position in the stream
ios_base::end end of the stream
从这一点来看,利用我们的演绎和批判性思维技能(在编程中有重要的一般技能,在编程之外有用),我们可以看到从<
提示:距离结尾的10个字节不是结束。