2

I'm writing a program that generates a large log file (+10GB) on completion. To split this up, in my logging function I redirect output to a new file name once n lines have been written.

However, some lines in this file can be huge (+100k chars) and some are no more than 10 chars, and the large ones can be clustered. I would much rather split the output by size in bytes rather than by line.

What's the most lightweight method for keeping an ongoing track of the number of bytes that have been sent to a c++ stream?

1 Answer 1

3

If you're using a std::ostream, have a look at the tellp() member function, which returns the current position of the put area. You can use it as a proxy for the number of bytes written. E.g.:

std::ofstream file(name); auto i = file.tellp(); // write large log file. auto j = file.tellp(); if (i != -1 && j != -1) // tellp returns -1 on error. std::cout << "bytes written: " << j - i << std::endl; 

Also note though that tellp flushes the tied output streams since C++11.

Sign up to request clarification or add additional context in comments.

5 Comments

would you expect the file size to be equivalent to file.tellp() * sizeof(char)?
Yes, I would expect so but still write a little test to convince myself.
ok, thanks for pointing me in the right direction, this works nicely.
that's just wrong. if writing fails (which is what OP tries to check) then tellp is required to return -1
Sounds easy to fix by checking first whether tellp returned -1, no?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.