Bill Cunningham said:
In fread, the type of the function is the typedef size_t.
I want to rewrite a program that read binary data of mp3s.
int main(){
printf("Enter name of file-> ");
char name;
Unless you have a C99 compliant compiler you must define all
your variables before the first executable statement. And
since you're going to use 'name' to store a file name, a
single character won't be enough.
fflush(stdout);
FILE *fp;
fp=fopen(name,"rb");
fopen() expects a char pointer as its first argument and 'name'
is neither a char pointer nor has it been initalized...
/*Here's where fread should go but k&r p 248 didn't help much */
fread() is declared as
size_t fread( void *ptr, size_t size, size_t nobj, FILE *stream )
'size_t' is simply an unsigned (i.e. it can't be less than 0) integer
type large enough to hold all possible size informations on your
machine. And fread() reads up to a number of 'nobj' objects, each of
size 'size' (in units of the size of a char, which often equals a byte),
from the input stream 'stream' (that's where you would use your 'fp'
FILE pointer) into a buffer pointed to by 'ptr'. When it returns it
tells you how many objects it has read.
As an example, let's assume you want to read 100 int's from a file.
Then you first need a buffer where fread() can later store them.
So you either need an array
int data[ 100 ];
or you need an dynamically allocated buffer of the same size
int *data;
if ( ( data = malloc( 100 * sizeof *data ) ) == NULL )
{
fprintf( stderr, "Running out of memory\n" );
exit( EXIT_FAILURE );
}
You also need a variable to hold the number of items the
fread() call is going to return, i.e.
size_t count;
Now let's also assume you already opened the file successfully,
and the return value of fopen() is stored in 'fp'. Then you can
read in your 100 int's as
count = fread( data, sizeof *data, 100, fp );
'data' is the buffer the data are going to be stored in,
'sizeof *data' is the size of a single int (you could also
write 'sizeof( int )', but then you have to change this if
you should later decide to read e.g. long int's from the
file instead of simple int's), 100 is the number of integers
you want to read and 'fp' is a FILE pointer top the file you
want to read from.
After the call of fread() the 'count' variable tells you how many
items (integers in this case) gort read from the file, it could
be less than 100 when e.g. there weren't as many int's stored in
the file as you expected.
Please note: in the real world there are several possible pitfalls
- binary data written by one machine might not mean a thing to a
different machine with e.g. a different architecture. For example,
one machine might have 4 byte int's while another one has 2 byte
int's, or one machine might store numbers in big-endian format,
while the other in small-endian. And for floating point numbers
it might get even worse... Thus when you do binary reads you must
be prepared to deal with all these possible problems.
Regards, Jens
--
_ _____ _____
| ||_ _||_ _| (e-mail address removed)-berlin.de
_ | | | | | |
| |_| | | | | |
http://www.physik.fu-berlin.de/~toerring
\___/ens|_|homs|_|oerring