B
Barry Schwarz
I wrote this small program to read a 512 block of binary data and write
the same to a file. My code compiled well. The only thing is when I ran the
compilers binary instead of a data file of 512 bytes I got one of 2048
bytes.
#include <stdio.h>
main(){
int buf[512];
FILE *fp;
fp=fopen("r.dsk","rb");
if (fp==NULL) {printf("Error"); exit(0);}
fread(buf,sizeof(int),512,fp);
fclose(fp);
fp=fopen("dat","wb");
if (fp==NULL) {printf("Error");}
fwrite(buf,sizeof(int),512,fp);
fclose(fp);}
Is it the code or some overhead from the compiler or linker?
Bill
The unix dd command copies exactly 512 bytes of I asked it too.
If your input file was exactly 512 bytes long, then it probably
contained 128 integers, not 512. There are very few home systems
where sizeof(int) is 1. On your system, sizeof(int) is apparently 4
and 512 int will require 2048 bytes.
You forgot to check the return from fread which would have told you
exactly how many int had been read (which you would then use as the
third argument to fwrite). Your code as written says:
Read up to 512 int
Regardless of how many were read, write 512 to the output.
If you really meant to process bytes, change buf to a char array and
change the second argument of both fread and fwrite to sizeof *buf.
Remove del for email