473,396 Members | 1,734 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

size_t and size of

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

-----= Posted via Newsfeeds.Com, Uncensored Usenet News =-----
http://www.newsfeeds.com - The #1 Newsgroup Service in the World!
-----== Over 100,000 Newsgroups - 19 Different Servers! =-----
Nov 13 '05 #1
11 4253
Bill Cunningham <nospam@net> wrote:
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
--
_ _____ _____
| ||_ _||_ _| Je***********@physik.fu-berlin.de
_ | | | | | |
| |_| | | | | | http://www.physik.fu-berlin.de/~toerring
\___/ens|_|homs|_|oerring
Nov 13 '05 #2
On Sat, 1 Nov 2003 13:34:45 -0500, "Bill Cunningham" <nospam@net>
wrote:
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>>
Nov 13 '05 #3
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

-----= Posted via Newsfeeds.Com, Uncensored Usenet News =-----
http://www.newsfeeds.com - The #1 Newsgroup Service in the World!
-----== Over 100,000 Newsgroups - 19 Different Servers! =-----
Nov 13 '05 #4
On Sun, 02 Nov 2003 01:57:29 -0500, Bill Cunningham wrote:
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.
Nov 13 '05 #5
Je***********@physik.fu-berlin.de wrote:

<snip>
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
--
Irrwahn
(ir*******@freenet.de)
Nov 13 '05 #6
Bill Cunningham <nospam@net> wrote:
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
--
_ _____ _____
| ||_ _||_ _| Je***********@physik.fu-berlin.de
_ | | | | | |
| |_| | | | | | http://www.physik.fu-berlin.de/~toerring
\___/ens|_|homs|_|oerring
Nov 13 '05 #7
Irrwahn Grausewitz <ir*******@freenet.de> wrote:
Je***********@physik.fu-berlin.de wrote: <snip>
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.


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
--
_ _____ _____
| ||_ _||_ _| Je***********@physik.fu-berlin.de
_ | | | | | |
| |_| | | | | | http://www.physik.fu-berlin.de/~toerring
\___/ens|_|homs|_|oerring
Nov 13 '05 #8
"Bill Cunningham" <nospam@net> wrote:
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.

--
Simon.
Nov 13 '05 #9
Je***********@physik.fu-berlin.de wrote:
Irrwahn Grausewitz <ir*******@freenet.de> wrote:
Je***********@physik.fu-berlin.de wrote:
<snip>
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.


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
--
Irrwahn
(ir*******@freenet.de)
Nov 13 '05 #10
Oops. Yes I meant fopen.

Bill

-----= Posted via Newsfeeds.Com, Uncensored Usenet News =-----
http://www.newsfeeds.com - The #1 Newsgroup Service in the World!
-----== Over 100,000 Newsgroups - 19 Different Servers! =-----
Nov 13 '05 #11
In <3f********@corp.newsgroups.com> "Bill Cunningham" <nospam@net> writes:
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
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Nov 13 '05 #12

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Christopher | last post by:
can someone refresh my meory on what std::size_t is used for? I am wondering if my template container class should have it's capacity as a size_t or an int? , Christopher
11
by: javadesigner | last post by:
Hi: I am a bit new to C programming and am trying to write a wrapper around malloc. Since malloc takes size_t as it's parameter, I want to be able to see if my function recieved an argument...
40
by: Confused User | last post by:
I am curious what the origins of size_t are. I see that it is usually typedef'd to be the native integer size of a particular machine. I also see that routines like strncpy(char *t, const char *s,...
18
by: rayw | last post by:
I used to believe that size_t was something to do with integral types, and the std. Something along the lines of .. a char is 8 bits, a int >= a char a long >= int
12
by: Alex Vinokur | last post by:
Why was the size_t type defined in compilers in addition to unsigned int/long? When/why should one use size_t? Alex Vinokur email: alex DOT vinokur AT gmail DOT com...
23
by: bwaichu | last post by:
To avoid padding in structures, where is the best place to put size_t variables? According the faq question 2.12 (http://c-faq.com/struct/padding.html), it says: "If you're worried about...
318
by: jacob navia | last post by:
Rcently I posted code in this group, to help a user that asked to know how he could find out the size of a block allocated with malloc. As always when I post something, the same group of people...
22
by: subramanian100in | last post by:
Consider the following program #include <limits.h> #include <stddef.h> int main(void) { size_t size; size_t bytes = sizeof(size_t);
89
by: Tubular Technician | last post by:
Hello, World! Reading this group for some time I came to the conclusion that people here are split into several fractions regarding size_t, including, but not limited to, * size_t is the...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.