Here is some additional practice using binary I/O. Some of these might take a little bit of thinking, but this practice is here to help you get more comfortable with the material.
Assume big-endian notation.
int
created in C is 4 bytes (32 bits) in size. How would the integer
2
be stored in binary in memory?Answer:
00000000 00000000 00000000 00000010
2
in binary to a file, what data
would be in the file? Write your answer in binary.Answer:
00000000 00000000 00000000 00000010
output.bin
contain after execution? Again, write your answer in binary.
Assume all necessary libraries are included and all files are
opened/closed properly.struct cool_struct
{
int x;
};
int main(void)
{
struct cool_struct basically_just_an_int;
FILE *ofp = fopen("output.bin", "wb");
.x = 2;
basically_just_an_int
(&basically_just_an_int, sizeof(struct cool_struct), 1, ofp);
fwrite
(ofp);
fclose
return 0;
}
Answer:
00000000 00000000 00000000 00000010