size_t and size of

  • Thread starter Bill Cunningham
  • Start date
B

Bill Cunningham

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;
fflush(stdout);
FILE *fp;
fp=fopen(name,"rb");
/*Here's where fread should go but k&r p 248 didn't help much */
fclose(fp);}
 
J

Jens.Toerring

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
 
B

Barry Schwarz

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;
fflush(stdout);
FILE *fp;
fp=fopen(name,"rb");
/*Here's where fread should go but k&r p 248 didn't help much */
fclose(fp);}
fread returns a size_t as the count of bytes read. This has nothing
else to do with the actual data being transferred to the buffer.
fread will read a binary file just fine.


<<Remove the del for email>>
 
B

Bill Cunningham

You say fread expects a pointer to a char as it's first argument. Does that
mean it will except chars too?
or I guess I should say strings maybe? I've seen a lot of this.

fp=fread("hello.exe","rb");
The first argument is accepted as a string not a char, or a pointer to a
char. Right?

Bill
 
K

Kelsey Bjarnason

You say fread expects a pointer to a char as it's first argument. Does that
mean it will except chars too?
or I guess I should say strings maybe? I've seen a lot of this.

fp=fread("hello.exe","rb");
The first argument is accepted as a string not a char, or a pointer to a
char. Right?

Last I checked, fread() took four parameters: a buffer to store the data
into, a number of elements, an element size, and a file pointer. You've
got two parameters. Of those two, zero are correct.

The buffer parameter - where the data gets stored - is actually passed as
a void *; it could be an array of chars, it could be a long double,
whatever's correct for the code (and the file). What it cannot be,
however, is a non-modifiable string constant such as "hello.exe".

If you expect the line you wrote to open "hello.exe" in binary mode and
read some data from it... it ain't gonna happen.
 
I

Irrwahn Grausewitz

(e-mail address removed)-berlin.de wrote:

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),

Nit-pick: sizeof (char) *always* equals 1 byte.

References: ISO/IEC 9899:TC1 6.5.3.4p2 & p3,
ANSI C89 3.3.3.4

Regards
 
J

Jens.Toerring

Bill Cunningham said:
You say fread expects a pointer to a char as it's first argument. Does that
mean it will except chars too?
or I guess I should say strings maybe? I've seen a lot of this.

No, I wrote that fopen() expects a char pointer as it's first argument.
fp=fread("hello.exe","rb");

I hope you mean

fp=fopen("hello.exe","rb");
The first argument is accepted as a string not a char, or a pointer to a
char. Right?

"hello.exe" is a literal string and what the function gets is the pointer
to the first character of the string. A string is nothing else than an
array of characters, terminated by a '\0' - there's no special 'string'
type in C.
Regards, Jens
--
_ _____ _____
| ||_ _||_ _| (e-mail address removed)-berlin.de
_ | | | | | |
| |_| | | | | | http://www.physik.fu-berlin.de/~toerring
\___/ens|_|homs|_|oerring
 
S

Simon Biber

Bill Cunningham said:
You say fread expects a pointer to a char as it's first
argument. Does that mean it will except chars too?
or I guess I should say strings maybe? I've seen a lot of this.

fp=fread("hello.exe","rb");
The first argument is accepted as a string not a char, or a
pointer to a char. Right?

The string "hello.exe" is an array of 10 chars. When used as a function
argument like that, an array is automatically converted into a pointer
to the first element. So, the string "hello.exe" is converted into a
pointer to the 'h' character at the beginning of the array. It is that
pointer to char, which is passed to the fread function.
 
J

Jens.Toerring

Irrwahn Grausewitz said:
(e-mail address removed)-berlin.de wrote:
Nit-pick: sizeof (char) *always* equals 1 byte.

The point I wanted to get accoss was that a char is the smallest
unit, which I think is equivalent to saying "sizeof( char ) == 1".
Beside it would have looked stupid to write "in units of 1" and I
wanted to avoid writing "in units of bytes", which would have been
wrong. Probably it would have been better to say "where size means
the value you get from the sizeof operator when applied to the
object". Sorry if I wasn't clear enough ;-)

Regards, Jens
--
_ _____ _____
| ||_ _||_ _| (e-mail address removed)-berlin.de
_ | | | | | |
| |_| | | | | | http://www.physik.fu-berlin.de/~toerring
\___/ens|_|homs|_|oerring
 
I

Irrwahn Grausewitz

The point I wanted to get accoss was that a char is the smallest
unit, which I think is equivalent to saying "sizeof( char ) == 1".
Beside it would have looked stupid to write "in units of 1" and I
wanted to avoid writing "in units of bytes", which would have been
wrong.

Err, no:

C99 6.5.3.4 The sizeof operator
[...]
2 The sizeof operator yields the size (in bytes) of its operand,
[...]
Probably it would have been better to say "where size means
the value you get from the sizeof operator when applied to the
object".

Or just:
"... (in units of the size of a char, which equals one byte), ..."
Sorry if I wasn't clear enough ;-)

As I said, I was just picking nit. ;-D

Regards
 
D

Dan Pop

In 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;
fflush(stdout);
FILE *fp;
fp=fopen(name,"rb");
/*Here's where fread should go but k&r p 248 didn't help much */
fclose(fp);}

This is complete garbage, showing that you're not familiar with the most
basic elements of the language. Since you've been posting newbie
questions for far too many months, I have to conclude that either C is
too difficult for you or you're not making the slightest effort to learn
it properly.

Therefore, please stop wasting our time with your questions.

Dan
 

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

Similar Threads


Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,578
Members
45,052
Latest member
LucyCarper

Latest Threads

Top