how many bytes actually written by ostream::write?

12,959

Solution 1

You can use .tellp() to know the output position in the stream to compute the number of bytes written as:

size_t before = file.tellp(); //current pos

if(file.write(&buf[0], buf.size())) //enter the if-block if write fails.
{
  //compute the difference
  size_t numberOfBytesWritten = file.tellp() - before;
}

Note that there is no guarantee that numberOfBytesWritten is really the number of bytes written to the file, but it should work for most cases, since we don't have any reliable way to get the actual number of bytes written to the file.

Solution 2

I don't see any equivalent to gcount(). Writing directly to the streambuf (with sputn()) would give you an indication, but there is a fundamental problem in your request: write are buffered and failure detection can be delayed to the effective writing (flush or close) and there is no way to get access to what the OS really wrote.

Share:
12,959
Aviad Rozenhek
Author by

Aviad Rozenhek

Updated on July 20, 2022

Comments

  • Aviad Rozenhek
    Aviad Rozenhek almost 2 years

    suppose I send a big buffer to ostream::write, but only the beginning part of it is actually successfully written, and the rest is not written

    int main()
    {
       std::vector<char> buf(64 * 1000 * 1000, 'a'); // 64 mbytes of data
       std::ofstream file("out.txt");
       file.write(&buf[0], buf.size()); // try to write 64 mbytes
       if(file.bad()) {
         // but suppose only 10 megabyte were available on disk
         // how many were actually written to file???
       }
       return 0;
    }
    

    what ostream function can tell me how many bytes were actually written?

    • matth
      matth over 11 years
      Unrelated to your question: You mustn't use void main(), and you have the order of arguments to vector::vector() backwards.
    • Aviad Rozenhek
      Aviad Rozenhek over 11 years
      thanks @Robᵩ fixed code to concentrate efforts on the question.
  • AProgrammer
    AProgrammer over 11 years
    How does that answer the question which is about write?
  • Aviad Rozenhek
    Aviad Rozenhek over 11 years
    @AProgrammer is the answer still valid if the ostream writes to a socket rather than a file?
  • Nawaz
    Nawaz over 11 years
    @AviadRozenhek: it is no reliable even when you write to a file. but .tellp() is all that we get in C++.
  • Aviad Rozenhek
    Aviad Rozenhek over 11 years
    @Nawaz the issue is [I think] that with many implementations of ostream as wrapper to a socket, tellp() returns -1 always ...
  • Aviad Rozenhek
    Aviad Rozenhek over 11 years
    @Nawaz does using flush() help in any way?
  • Nawaz
    Nawaz over 11 years
    @AviadRozenhek: I've no idea what would flush() do, once write fails.
  • James Kanze
    James Kanze over 11 years
    Once write fails, all further operations on the stream are no-ops, until the error is cleared. Including tellp, which is required to return -1 if any previous operation has failed.
  • James Kanze
    James Kanze over 11 years
    In fact, the only solution is to find out the size of the file (from the system, using system specific calls) before the write, then after the write, close the file and use the same system specific call to find the new size.\
  • Sam Ginrich
    Sam Ginrich about 2 years
    Again an example of STL not getting simple things straight :(