fread() function and reading binary files

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;
}
 
M

Malcolm

Fernando Barsoba said:
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..
C strings are null-terminated. You probably have your data, after the first
null.

What you wnat to do is print it out in a loop including putc(), one
character at a time.
Any non-printing characters will show up in strange ways - you have to
decide how to handle these in the final version.
 
M

Martin Ambuhl

Fernando said:
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.

strlen() returns the length of a string. A string is terminated by the
first 0 byte in it. It does not tell you the number of characters in
your buffer. If you want to fill a buffer of unknown length in the
function, have the number of characters as the return value or have a
pointer to the number of characters as a parameter do one of those two
with instead of simple number of characters variable a structure with a
pointer to the buffer and its size as members.

BTW, if your first 0 is at the 17h (0x17, 23, 027) position, you should
not be getting 17 as the value from strlen(). I assume that was a typo
for 17th.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,022
Latest member
MaybelleMa

Latest Threads

Top