size_t and size of

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Bill Cunningham

    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! =-----
  • Jens.Toerring@physik.fu-berlin.de

    #2
    Re: size_t and size of

    Bill Cunningham <nospam@net> wrote:[color=blue]
    > 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;[/color]

    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.
    [color=blue]
    > fflush(stdout);
    > FILE *fp;
    > fp=fopen(name," rb");[/color]

    fopen() expects a char pointer as its first argument and 'name'
    is neither a char pointer nor has it been initalized...
    [color=blue]
    > /*Here's where fread should go but k&r p 248 didn't help much */[/color]

    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
    --
    _ _____ _____
    | ||_ _||_ _| Jens.Toerring@p hysik.fu-berlin.de
    _ | | | | | |
    | |_| | | | | | http://www.physik.fu-berlin.de/~toerring
    \___/ens|_|homs|_|oe rring

    Comment

    • Barry Schwarz

      #3
      Re: size_t and size of

      On Sat, 1 Nov 2003 13:34:45 -0500, "Bill Cunningham" <nospam@net>
      wrote:
      [color=blue]
      >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);}
      >[/color]
      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>>

      Comment

      • Bill Cunningham

        #4
        Re: size_t and size of

        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! =-----

        Comment

        • Kelsey Bjarnason

          #5
          Re: size_t and size of

          On Sun, 02 Nov 2003 01:57:29 -0500, Bill Cunningham wrote:
          [color=blue]
          > 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?[/color]

          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.


          Comment

          • Irrwahn Grausewitz

            #6
            Re: size_t and size of

            Jens.Toerring@p hysik.fu-berlin.de wrote:

            <snip>[color=blue]
            > 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),[/color]

            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
            (irrwahn33@free net.de)

            Comment

            • Jens.Toerring@physik.fu-berlin.de

              #7
              Re: size_t and size of

              Bill Cunningham <nospam@net> wrote:[color=blue]
              > 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.[/color]

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

              I hope you mean

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

              "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
              --
              _ _____ _____
              | ||_ _||_ _| Jens.Toerring@p hysik.fu-berlin.de
              _ | | | | | |
              | |_| | | | | | http://www.physik.fu-berlin.de/~toerring
              \___/ens|_|homs|_|oe rring

              Comment

              • Jens.Toerring@physik.fu-berlin.de

                #8
                Re: size_t and size of

                Irrwahn Grausewitz <irrwahn33@free net.de> wrote:[color=blue]
                > Jens.Toerring@p hysik.fu-berlin.de wrote:[/color]
                [color=blue]
                > <snip>[color=green]
                >> 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),[/color][/color]
                [color=blue]
                > Nit-pick: sizeof (char) *always* equals 1 byte.[/color]

                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
                --
                _ _____ _____
                | ||_ _||_ _| Jens.Toerring@p hysik.fu-berlin.de
                _ | | | | | |
                | |_| | | | | | http://www.physik.fu-berlin.de/~toerring
                \___/ens|_|homs|_|oe rring

                Comment

                • Simon Biber

                  #9
                  Re: size_t and size of

                  "Bill Cunningham" <nospam@net> wrote:[color=blue]
                  > 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?[/color]

                  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.


                  Comment

                  • Irrwahn Grausewitz

                    #10
                    Re: size_t and size of

                    Jens.Toerring@p hysik.fu-berlin.de wrote:
                    [color=blue]
                    > Irrwahn Grausewitz <irrwahn33@free net.de> wrote:[color=green]
                    > > Jens.Toerring@p hysik.fu-berlin.de wrote:[/color]
                    >[color=green]
                    > > <snip>[color=darkred]
                    > >> 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),[/color][/color]
                    >[color=green]
                    > > Nit-pick: sizeof (char) *always* equals 1 byte.[/color]
                    >
                    > 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.[/color]

                    Err, no:

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

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

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

                    Regards
                    --
                    Irrwahn
                    (irrwahn33@free net.de)

                    Comment

                    • Bill Cunningham

                      #11
                      Re: size_t and size of

                      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! =-----

                      Comment

                      • Dan Pop

                        #12
                        Re: size_t and size of

                        In <3fa3fde7_8@cor p.newsgroups.co m> "Bill Cunningham" <nospam@net> writes:
                        [color=blue]
                        >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);}[/color]

                        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: Dan.Pop@ifh.de

                        Comment

                        Working...