Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > fread() function and reading binary files

Reply
Thread Tools

fread() function and reading binary files

 
 
Fernando Barsoba
Guest
Posts: n/a
 
      10-17-2005
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;
}
 
Reply With Quote
 
 
 
 
Malcolm
Guest
Posts: n/a
 
      10-17-2005

"Fernando Barsoba" <(E-Mail Removed)> wrote
> 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.


 
Reply With Quote
 
 
 
 
Martin Ambuhl
Guest
Posts: n/a
 
      10-17-2005
Fernando Barsoba wrote:
> 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.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Newbie: working with binary files/extract png from a binary file Jim Ruby 6 12-24-2013 08:09 AM
reading binary files in vhdl. Use of read in a function. gfandango VHDL 1 02-10-2009 08:18 AM
Question about reading a big binary file and write it into several text (ascii) files Albert Tu Python 2 01-25-2005 03:56 AM
Reading and Writing to Binary Files Daniel Moree C++ 9 11-24-2004 10:20 PM
Reading/Writing pure binary files Daniel Gowans VHDL 2 06-12-2004 01:25 AM



Advertisments