F
Fernando Barsoba
Hi all,
I'm trying to read from a binary file, and I'm using 'fread()' as
indicated in function 'getfile()'. The variable 'bytesread' shows that I
have read the 557,000 bytes from the file, but because the file has a 00
at the 17h position, when doing >>lenbuf2 = strlen(buf2), I get only 17
bytes. So, it's weird, because fread() seems to put 550,000 bytes in the
buf variable, but after returning from the function, only bytes up to
EOF are read..
Any idea?
Thanks,
FBM
main:
....
void *buf2;
buf2 = getfile(buf_trcd); // getfile() is defined below
int lenbuf2;
char buffer_length[20];
lenbuf2 = strlen(buf2);
sprintf(buffer_length, "%d", lenbuf2);
len = send(p, buffer_length, strlen(buffer_length), 0);
len = send(p, buf2, lenbuf2, 0);
void * getfile(char *pathname) {
FILE *fp;
void *buf;
long lSize;
int bytesread;
fp = open_file(pathname);
// obtain file size.
fseek (fp , 0 , SEEK_END);
lSize = ftell (fp);
rewind (fp);
// allocate memory to contain the whole file.
buf = malloc (lSize); // to do: free 'buf'
if (buf == NULL)
exit (1);
// copy the file into the buffer.
bytesread = fread (buf,1,lSize,fp);
printf(buf);
fclose(fp);
return buf;
}
I'm trying to read from a binary file, and I'm using 'fread()' as
indicated in function 'getfile()'. The variable 'bytesread' shows that I
have read the 557,000 bytes from the file, but because the file has a 00
at the 17h position, when doing >>lenbuf2 = strlen(buf2), I get only 17
bytes. So, it's weird, because fread() seems to put 550,000 bytes in the
buf variable, but after returning from the function, only bytes up to
EOF are read..
Any idea?
Thanks,
FBM
main:
....
void *buf2;
buf2 = getfile(buf_trcd); // getfile() is defined below
int lenbuf2;
char buffer_length[20];
lenbuf2 = strlen(buf2);
sprintf(buffer_length, "%d", lenbuf2);
len = send(p, buffer_length, strlen(buffer_length), 0);
len = send(p, buf2, lenbuf2, 0);
void * getfile(char *pathname) {
FILE *fp;
void *buf;
long lSize;
int bytesread;
fp = open_file(pathname);
// obtain file size.
fseek (fp , 0 , SEEK_END);
lSize = ftell (fp);
rewind (fp);
// allocate memory to contain the whole file.
buf = malloc (lSize); // to do: free 'buf'
if (buf == NULL)
exit (1);
// copy the file into the buffer.
bytesread = fread (buf,1,lSize,fp);
printf(buf);
fclose(fp);
return buf;
}