0

在以下代码中:

#include <iostream>
#include <fstream>
using namespace std;
int main(){
    fstream obj; // Hello Hi Girik Garg
    obj.open("test1.txt",ios::in);
    if (!obj){
        cout << "File Not opened" <<endl;
        exit(0);
    }
    obj.seekg(2,ios::beg);
    cout << "Position of get pointer is " << obj.tellg() <<endl;
    cout << "Position of put pointer is " << obj.tellp() <<endl;
    char a[100];
    obj.getline(a,99);
    cout << a;
    return 0;
}

文件“test1.txt”的内容是 Hello Hi Girik Garg。这个程序的输出是:

Position of get pointer is 2

Position of put pointer is 2

llo Hi Girik Garg

我对put指针的位置有疑问,因为我没有使用seekp,所以seekp的位置不应该是0而不是2吗?

请用适当的理由解释。

4

0 回答 0