ababeel
ababeel

Reputation: 435

Writing to c++ file till a specified file size is reached

I am writing a c++ program to write to a text file of a specified size. I initially create a text file of size specified say 2 KB, I want to keep writing to this file till the 2 KB limit is reached and that point notify the user. I am not sure what the best way is. I am looking for a cross platform solution. Would something like libevent (http://libevent.org/) be good for this or am I missing something simpler.

Any advice/help greatly appreciated.

Thanks

Upvotes: 0

Views: 925

Answers (1)

sehe
sehe

Reputation: 393674

#include <fstream>

int main()
{
    std::ofstream ofs("output.img", std::ios::binary | std::ios::out);
    ofs.seekp((2<<10) - 1);
    ofs.write("", 1);
}
  • If you want to detect filepos, you could use std::ofstream::tellp()
  • alternatively, sync + stat will be able to get up-to-date filesize without re-opening the file

Upvotes: 4

Related Questions