Create file with a given size

I am interested in creating a file of a given size and then randomly accessing the file to populate it. Is there a way to quickly create, for instance, a 4 GByte file in C++, initially populated with garbage?
The C++ program would have to call the API of the operating system. There are already other (perhaps not C++) programs that do the same thing. For example:
http://superuser.com/questions/609020/how-can-i-create-a-file-with-a-specific-size-from-a-command-line

You can either (1) use those programs as is, (2) look at their source code to find out how they do it and rewrite it in your program, or (3) read the API documentation of your SO and write the code.
A typically acceptable way to do this is to seek to whatever offset you want (past EOF), then write a single byte. That should work on most if not all implementations.
The seek worked like a charm. I didn't think you could seekp() past the end of a file. I learn something new every day.

That's what makes this forum great.
Although I like Disch solution for it's simplicity and OS agnostic approach, I wanted to add to what keskiverto was saying. The way to do this on the command line in Windows is
fsutil file createnew file_size_in_bytes
Topic archived. No new replies allowed.