raj said:
Hi friends,
In an interview I was asked to write a C program to create a large file
of 8GB
The first 4GB is filled with "Hello"
and the secod 4GB is filled with "World"
Sorry to say that I don't know how to do that in an elegant way. I think
it is a trick question depending on if size_t is 32 bits or 64 bits.
Does anybody know how?
Output 800000000 copies of "Hello", then output 800000000
copies of "World". Finally, use ferror() to see whether any
I/O errors occurred, and make sure fclose() succeeds before
your program declares success.
Notes:
1) The symbol "4GB" usually means 4294967296 to computer
people, but the task would be impossible if that were the
case in this instance: both "Hello" and "World" are five
bytes long, and 4294967296 is not divisible by five. Therefore
the prefix "G" presumably denotes its meaning under international
standards, namely, 1000000000. The assignment therefore calls
for 4000000000 bytes to be filled with each word, not 4294967296.
Besides making the task possible, this observation will make your
program run about seven percent faster; be sure to point this
out to the interviewer, who will be impressed with your devotion
to efficiency.
2) Since the task does not mention writing any newline
characters, the output cannot be a well-formed text stream
because each line of such a stream ends with a '\n'. (Even
on systems where an unterminated line is allowed, the length
of the generated line would exceed the portable limit.) So
we conclude that the output is to be a binary stream; keep
this in mind when you call fopen().