0
0
0142016-03-02 13:59:03
C++ / C#
014, 2016-03-02 13:59:03

Why does the tellp() method return a different value than expected?

I open a file that occupies 141 bytes on disk (that is, not zero). I open for writing to the end, reading, in binary mode. I write the line "test" into it, everything is correct, it is written to the end of the file, since ios:: app. It is logical to assume that the record file pointer should also remain at the end of the file and return an absolute value, that is, return, in fact, the size of the file.
That is, tellp() should return 141 + 5 (test + '\0') = 146, but I didn’t seem to understand it so well and tellp() returns 5, as if it’s not an absolute value, but a local one, that is, how much I wrote down and returned. Tell me what I missed or misunderstood?

#include <iostream>
#include <fstream>
using namespace std;

int main()
{
    fstream distFile;
    distFile.open("test.dat", ios::binary | ios::app | ios::out | ios::in);
    distFile.write("test", 5);

    cout<<"tellp = "<<distFile.tellp()<<endl;

    return 0;
}

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Axian Ltd., 2016-03-02
@014

Input output is buffered flush IMHO you need to do

V
Vladimir Martyanov, 2016-03-02
@vilgeforce

"I write the line "test" into it, everything is correct, it is written to the end of the file" - did you check this?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question