tellp
및 seekg
함수에 대한 몇 가지 오해가 있습니다. 예를 들어, 다음 코드를 실행할 때.C++에서 tellp 사용
#include <iostream>
#include <ostream>
#include <fstream>
using namespace std;
int main(){
string s("in Georgia we are friends to each other");
ofstream out("output.txt");
for (int i=0; i<s.length(); i++){
cout<<"file pointer"<<out.tellp();
out.put(s[i]);
cout << " " << s[i] << endl;
}
out.close();
return 0;
}
결과는 다음과 같습니다.
pointer 0
pointer 1 i
pointer2 n
pointer 3
pointer 4 -g
........
등등. 알다시피, 첫 번째 포인터 0 그것은 포인터 그냥 파일 다음 문자열의 첫 번째 문자를 가리킨다.
이제 다음 코드를 고려하십시오.
#include <fstream>
using namespace std;
int main() {
long pos;
ofstream outfile;
outfile.open ("test.txt");
outfile.write ("This is an apple",16);
pos = outfile.tellp();
outfile.seekp (pos-7);
outfile.write (" sam",4);
outfile.close();
return 0;
}
결과는 다음과 같습니다
This is a sample
이 줄, pos = outfile.tellp();
, 나는 첫 번째 예에서와 같이 제로에 POS를 얻을 생각합니다. 이 부분에서 outfile.seekp (pos-7)
은 무엇을 의미합니까? -7 또는?
출력이 문자열 상수 코드와 일치하지 않습니다. 게시 할 때 전사 오류가있을 수 있습니까? – wallyk
@wallyk : 나를 위해 출력물이 코드와 일치합니다. – bjskishore123
이것은 http://www.cplusplus.com/reference/iostream/ostream/tellp/의 동일한 예제입니다. – Chubsdad