When I changed size of ofstream
buffer with pubsetbuf(...)
, everything works fine, except when I put to ofstream
single string longer then 1023
(in the code below). Is it correct behavior or I did something wrong?
int main(){
std::vector<char> rawBuf;
std::ofstream stream;
rawBuf.resize(20000);
stream.rdbuf()->pubsetbuf(&rawBuf[0], 20000);
stream.open("file.txt", std::ios_base::app);
std::string data(1499, 'b');
for(int i = 0; i < 10; i++)
{
stream << data.substr(0, 1024) << "\n"; //1023-length string works great
sleep(1);
}
stream.flush();
stream.close();
return 0;
}
when there is 1024-length string strace ./program
shows something like this:
writev(3, [{iov_base=NULL, iov_len=0}, {iov_base="bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb"..., iov_len=1024}], 2) = 1024
nanosleep({tv_sec=1, tv_nsec=0}, 0x7ffcf3889ac0) = 0
writev(3, [{iov_base="\n", iov_len=1}, {iov_base="bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb"..., iov_len=1024}], 2) = 1025
nanosleep({tv_sec=1, tv_nsec=0}, 0x7ffcf3889ac0) = 0
... and so on 10x
when there is 1023-length string, everything seems ok:
nanosleep({tv_sec=1, tv_nsec=0}, 0x7fff8e13a980) = 0
nanosleep({tv_sec=1, tv_nsec=0}, 0x7fff8e13a980) = 0
... 10x
and then:
write(3, "bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb"..., 10240) = 10240
Why here is single write and earlier is not?
edit:
gcc version 7.3.0 (Ubuntu 7.3.0-16ubuntu3)