fread/fwrite

Discussion in 'C Programming' started by empriser, May 17, 2007.

  1. empriser

    empriser Guest

    How to use fread/fwrite copy a file.
    When reach file's end, fread return 0, I don't konw how many bytes
    in buf.
     
    empriser, May 17, 2007
    #1
    1. Advertising

  2. empriser

    Chris Dollin Guest

    empriser wrote:

    > How to use fread/fwrite copy a file.
    > When reach file's end, fread return 0, I don't konw how many bytes
    > in buf.


    None.

    Don't you have documentation for these functions? Or man pages? Or
    access to Google?

    --
    "Reaching out for mirrors hidden in the web." - Renaissance, /Running Hard/

    Hewlett-Packard Limited registered no:
    registered office: Cain Road, Bracknell, Berks RG12 1HN 690597 England
     
    Chris Dollin, May 17, 2007
    #2
    1. Advertising

  3. On 17 May 2007 00:52:29 -0700, empriser <> wrote:

    >How to use fread/fwrite copy a file.
    >When reach file's end, fread return 0, I don't konw how many bytes
    >in buf.


    It depends on how code the fread. If you specify 1 block of n
    characters and there is less than n left in the file, then it will
    return 0. However, if you specify n blocks of 1 character each, then
    it will return the number of blocks read (which will be exactly the
    same as the number of characters read).


    Remove del for email
     
    Barry Schwarz, May 17, 2007
    #3
  4. empriser

    empriser Guest

    On May 17, 8:25 pm, Barry Schwarz <> wrote:
    > On 17 May 2007 00:52:29 -0700, empriser <> wrote:
    >
    > >How to use fread/fwrite copy a file.
    > >When reach file's end, fread return 0, I don't konw how many bytes
    > >in buf.

    >
    > It depends on how code the fread. If you specify 1 block of n
    > characters and there is less than n left in the file, then it will
    > return 0. However, if you specify n blocks of 1 character each, then
    > it will return the number of blocks read (which will be exactly the
    > same as the number of characters read).
    >
    > Remove del for email


    Yes my buffer size is n (n > 1).
    How do I know there are how many bytes in buffer, after last call
    fread as if return 0.
     
    empriser, Jul 5, 2007
    #4
  5. empriser

    Eric Sosman Guest

    empriser wrote:
    > On May 17, 8:25 pm, Barry Schwarz <> wrote:
    >> On 17 May 2007 00:52:29 -0700, empriser <> wrote:
    >>
    >>> How to use fread/fwrite copy a file.
    >>> When reach file's end, fread return 0, I don't konw how many bytes
    >>> in buf.

    >> It depends on how code the fread. If you specify 1 block of n
    >> characters and there is less than n left in the file, then it will
    >> return 0. However, if you specify n blocks of 1 character each, then
    >> it will return the number of blocks read (which will be exactly the
    >> same as the number of characters read).
    >>
    >> Remove del for email

    >
    > Yes my buffer size is n (n > 1).
    > How do I know there are how many bytes in buffer, after last call
    > fread as if return 0.


    fread returns the number of elements that were read and
    stored in the buffer. If fread returns 10, it successfully
    read and stored 10 elements. If fread returns 0, it read
    and stored ... <wait for it> ... no elements.

    --
    Eric Sosman
    lid
     
    Eric Sosman, Jul 5, 2007
    #5
  6. On Thu, 05 Jul 2007 05:00:26 -0000, empriser <>
    wrote:

    >On May 17, 8:25 pm, Barry Schwarz <> wrote:
    >> On 17 May 2007 00:52:29 -0700, empriser <> wrote:
    >>
    >> >How to use fread/fwrite copy a file.
    >> >When reach file's end, fread return 0, I don't konw how many bytes
    >> >in buf.

    >>
    >> It depends on how code the fread. If you specify 1 block of n
    >> characters and there is less than n left in the file, then it will
    >> return 0. However, if you specify n blocks of 1 character each, then
    >> it will return the number of blocks read (which will be exactly the
    >> same as the number of characters read).
    >>
    >> Remove del for email

    >
    >Yes my buffer size is n (n > 1).
    >How do I know there are how many bytes in buffer, after last call
    >fread as if return 0.


    If you don't show your code, everything is just a guess. As long as
    we are guessing, I guess that the number of bytes read is equal to the
    square root of the address of fread (when cast to an unsigned short).


    Remove del for email
     
    Barry Schwarz, Jul 6, 2007
    #6
  7. Eric Sosman <> wrote:
    > empriser wrote:
    > > ...
    > > Yes my buffer size is n (n > 1).
    > > How do I know there are how many bytes in buffer,
    > > after last call fread as if return 0.

    >
    > fread returns the number of elements that were
    > read and stored in the buffer. If fread returns 10,
    > it successfully read and stored 10 elements. If
    > fread returns 0, it read and stored ... <wait for
    > it> ... no elements.


    In other words, code as...

    static char buf[1024];
    size_t r = fread(buf, 1, sizeof buf, fp);

    ....rather than...

    static char buf[1024];
    size_t r = fread(buf, sizeof buf, 1, fp);

    Assuming no read errors, if there are 123 characters
    left in the stream, the former will return 0 into r,
    whereas the latter will return 123. Both calls will
    read the same number of characters though.

    --
    Peter
     
    Peter Nilsson, Jul 6, 2007
    #7
  8. empriser

    empriser Guest

    OK I write a easy program to show it.
    fread( buf, sizeof(buf), 1, rf );
    fwrite( buf, sizeof(buf), 1, wf );

    If I don't change to fread( buf, 1, sizeof(buf), rf ); how to copy a
    file.

    int main( int argc, char **argv )
    {
    int i;
    char buf[1024];
    FILE *rf, *wf;

    rf = fopen( argv[1], "r" );
    wf = fopen( argv[2], "w" );

    i = fread( buf, sizeof(buf), 1, rf );
    while( i > 0 ){
    fwrite( buf, sizeof(buf), 1, wf );
    i = fread( buf, sizeof(buf), 1, rf );
    }

    fclose( rf );
    fclose( wf );
    return 0;
    }
     
    empriser, Jul 7, 2007
    #8
  9. On Sat, 07 Jul 2007 03:04:46 -0000, empriser <>
    wrote:

    >OK I write a easy program to show it.
    >fread( buf, sizeof(buf), 1, rf );
    >fwrite( buf, sizeof(buf), 1, wf );
    >
    >If I don't change to fread( buf, 1, sizeof(buf), rf ); how to copy a
    >file.


    By changing fread to fgetc and fwrite to fputc.

    What is with the spate of "I've got this broken code I don't want to
    change. How do I make it work?" messages. The only way to fix broken
    code is to change it. It you don't change it, it remains broken.

    If I insist on putting water in my gas tank, how do I get my car to
    run?

    >
    >int main( int argc, char **argv )
    >{
    > int i;
    > char buf[1024];
    > FILE *rf, *wf;
    >
    > rf = fopen( argv[1], "r" );
    > wf = fopen( argv[2], "w" );
    >
    > i = fread( buf, sizeof(buf), 1, rf );
    > while( i > 0 ){
    > fwrite( buf, sizeof(buf), 1, wf );
    > i = fread( buf, sizeof(buf), 1, rf );
    > }
    >
    > fclose( rf );
    > fclose( wf );
    > return 0;
    >}



    Remove del for email
     
    Barry Schwarz, Jul 7, 2007
    #9
  10. empriser

    Joe Wright Guest

    empriser wrote:
    > OK I write a easy program to show it.
    > fread( buf, sizeof(buf), 1, rf );
    > fwrite( buf, sizeof(buf), 1, wf );
    >
    > If I don't change to fread( buf, 1, sizeof(buf), rf ); how to copy a
    > file.
    >
    > int main( int argc, char **argv )
    > {
    > int i;
    > char buf[1024];
    > FILE *rf, *wf;
    >
    > rf = fopen( argv[1], "r" );
    > wf = fopen( argv[2], "w" );
    >
    > i = fread( buf, sizeof(buf), 1, rf );
    > while( i > 0 ){
    > fwrite( buf, sizeof(buf), 1, wf );
    > i = fread( buf, sizeof(buf), 1, rf );
    > }
    >
    > fclose( rf );
    > fclose( wf );
    > return 0;
    > }
    >

    Maybe this way..

    #include <stdio.h>
    #include <stdlib.h>

    int main(int argc, char *argv[]) {
    FILE *in, *out;
    char buf[100];
    size_t size;

    if ((in = fopen("cp.c", "rb")) == NULL)
    puts("Can't open cp.c"), exit(EXIT_FAILURE);

    if ((out = fopen("cp.x", "wb")) == NULL)
    puts("Can't make cp.x"), exit(EXIT_FAILURE);

    while ((size = fread(buf, 1, sizeof buf, in)) > 0)
    fwrite(buf, 1, size, out);

    fclose(in);
    fclose(out);
    return 0;
    }

    Note that cp.c is the name of this source file.

    --
    Joe Wright
    "Everything should be made as simple as possible, but not simpler."
    --- Albert Einstein ---
     
    Joe Wright, Jul 7, 2007
    #10
  11. empriser

    Guru Jois Guest

    On Jul 5, 6:42 pm, Peter Nilsson <> wrote:
    > Eric Sosman <> wrote:
    > > empriser wrote:
    > > > ...
    > > > Yes my buffer size is n (n > 1).
    > > > How do I know there are how many bytes in buffer,
    > > > after last call fread as if return 0.

    >
    > > fread returns the number of elements that were
    > > read and stored in the buffer. If fread returns 10,
    > > it successfully read and stored 10 elements. If
    > > fread returns 0, it read and stored ... <wait for
    > > it> ... no elements.

    >
    > In other words, code as...
    >
    > static char buf[1024];
    > size_t r = fread(buf, 1, sizeof buf, fp);
    >
    > ...rather than...
    >
    > static char buf[1024];
    > size_t r = fread(buf, sizeof buf, 1, fp);
    >
    > Assuming no read errors, if there are 123 characters
    > left in the stream, the former will return 0 into r,
    > whereas the latter will return 123. Both calls will
    > read the same number of characters though.
    >
    > --
    > Peter


    Is size_t and all such stuffs portable in c? If not what to make it
    so?


    Guru Jois
     
    Guru Jois, Jul 8, 2007
    #11
  12. empriser

    santosh Guest

    Guru Jois wrote:

    > On Jul 5, 6:42 pm, Peter Nilsson <> wrote:
    > > Eric Sosman <> wrote:
    > > > empriser wrote:
    > > > > ...
    > > > > Yes my buffer size is n (n > 1).
    > > > > How do I know there are how many bytes in buffer,
    > > > > after last call fread as if return 0.

    > >
    > > > fread returns the number of elements that were
    > > > read and stored in the buffer. If fread returns 10,
    > > > it successfully read and stored 10 elements. If
    > > > fread returns 0, it read and stored ... <wait for
    > > > it> ... no elements.

    > >
    > > In other words, code as...
    > >
    > > static char buf[1024];
    > > size_t r = fread(buf, 1, sizeof buf, fp);
    > >
    > > ...rather than...
    > >
    > > static char buf[1024];
    > > size_t r = fread(buf, sizeof buf, 1, fp);
    > >
    > > Assuming no read errors, if there are 123 characters
    > > left in the stream, the former will return 0 into r,
    > > whereas the latter will return 123. Both calls will
    > > read the same number of characters though.

    >
    > Is size_t and all such stuffs portable in c? If not what to make it
    > so?


    size_t is defined in the Standard, so it's certainly portable. New
    users to C tend to have some confusion over using size_t objects with
    printf and family. Generally using the lu format specifier with a cast
    to the appropriate type is recommended. If you can, and want to, use
    C99's additions the zu specifier is specifically there for size_t.
     
    santosh, Jul 8, 2007
    #12
  13. empriser

    CBFalconer Guest

    Guru Jois wrote:
    >

    .... snip ...
    >
    > Is size_t and all such stuffs portable in c? If not what to make
    > it so?


    Yes. That is, in part, why this newsgroup limits the topic to
    STANDARD C, and considers references to system dependent things as
    being off-topic. Read the standard.

    --
    <http://www.cs.auckland.ac.nz/~pgut001/pubs/vista_cost.txt>
    <http://www.securityfocus.com/columnists/423>
    <http://www.aaxnet.com/editor/edit043.html>
    cbfalconer at maineline dot net



    --
    Posted via a free Usenet account from http://www.teranews.com
     
    CBFalconer, Jul 8, 2007
    #13
  14. empriser

    Guru Jois Guest

    On Jul 8, 4:03 am, CBFalconer <> wrote:
    > Guru Jois wrote:
    >
    > ... snip ...
    >
    > > Is size_t and all such stuffs portable in c? If not what to make
    > > it so?

    >
    > Yes. That is, in part, why this newsgroup limits the topic to
    > STANDARD C, and considers references to system dependent things as
    > being off-topic. Read the standard.
    >
    > --
    > <http://www.cs.auckland.ac.nz/~pgut001/pubs/vista_cost.txt>
    > <http://www.securityfocus.com/columnists/423>
    > <http://www.aaxnet.com/editor/edit043.html>
    > cbfalconer at maineline dot net
    >
    > --
    > Posted via a free Usenet account fromhttp://www.teranews.com


    How and where can I read to understand the perfect knowledge about C
    standards.
    Any docs/links to learn pure portable programs for C??? Docs
    prefered..

    Guru Jois
     
    Guru Jois, Jul 9, 2007
    #14
  15. empriser

    santosh Guest

    Guru Jois wrote:

    > On Jul 8, 4:03 am, CBFalconer <> wrote:
    > > Guru Jois wrote:
    > >
    > > ... snip ...
    > >
    > > > Is size_t and all such stuffs portable in c? If not what to make
    > > > it so?

    > >
    > > Yes. That is, in part, why this newsgroup limits the topic to
    > > STANDARD C, and considers references to system dependent things as
    > > being off-topic. Read the standard.

    >
    > How and where can I read to understand the perfect knowledge about C
    > standards.
    > Any docs/links to learn pure portable programs for C??? Docs
    > prefered..


    As far as online tutorials are concerned I'll recommend Steve Summit's
    one:

    <http://www.eskimo.com/~scs/cclass/>

    Also he maintains the very useful C FAQ:

    <http://www.c-faq.com/>

    There's also a clc "wiki":

    <http://clc-wiki.net/>

    Other resources include:

    <http://www.lysator.liu.se/c/>
    <http://www.dinkumware.com/manuals/>
    <http://www-ccs.ucsd.edu/c/>
    <http://www.open-std.org/jtc1/sc22/wg14/> - Site for the draft
    Standard.
    <http://www.knosof.co.uk/cbook/cbook.html>
    <http://www.cpax.org.uk/prg/portable/c/index.php>
     
    santosh, Jul 9, 2007
    #15
  16. santosh said:

    <snip>

    > As far as online tutorials are concerned I'll recommend Steve Summit's
    > one:
    >
    > <http://www.eskimo.com/~scs/cclass/>


    Please allow me to add the Tom Torfs tutorial URL:

    <http://cprog.tomsweb.net/cintro.html>

    Tom used to be a regular (and highly respected) comp.lang.c contributor.
    Then, one day, he asked a question about C99, and it transpired that he
    was writing a C99 preprocessor. He has not been seen since.

    --
    Richard Heathfield <http://www.cpax.org.uk>
    Email: -www. +rjh@
    Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
    "Usenet is a strange place" - dmr 29 July 1999
     
    Richard Heathfield, Jul 9, 2007
    #16
  17. empriser

    ¬a\\/b Guest

    On Sun, 08 Jul 2007 02:36:55 -0700, santosh wrote:
    >Guru Jois wrote:
    >> On Jul 5, 6:42 pm, Peter Nilsson <> wrote:
    >> > Eric Sosman <> wrote:
    >> > > empriser wrote:
    >> > > > ...
    >> > > > Yes my buffer size is n (n > 1).
    >> > > > How do I know there are how many bytes in buffer,
    >> > > > after last call fread as if return 0.
    >> >
    >> > > fread returns the number of elements that were
    >> > > read and stored in the buffer. If fread returns 10,
    >> > > it successfully read and stored 10 elements. If
    >> > > fread returns 0, it read and stored ... <wait for
    >> > > it> ... no elements.
    >> >
    >> > In other words, code as...
    >> >
    >> > static char buf[1024];
    >> > size_t r = fread(buf, 1, sizeof buf, fp);
    >> >
    >> > ...rather than...
    >> >
    >> > static char buf[1024];
    >> > size_t r = fread(buf, sizeof buf, 1, fp);
    >> >
    >> > Assuming no read errors, if there are 123 characters
    >> > left in the stream, the former will return 0 into r,
    >> > whereas the latter will return 123. Both calls will
    >> > read the same number of characters though.

    >>
    >> Is size_t and all such stuffs portable in c? If not what to make it
    >> so?

    >
    >size_t is defined in the Standard, so it's certainly portable. New
    >users to C tend to have some confusion over using size_t objects with
    >printf and family. Generally using the lu format specifier with a cast
    >to the appropriate type is recommended. If you can, and want to, use
    >C99's additions the zu specifier is specifically there for size_t.


    size_t is not portable because you all don't know the size it has
    only uint8_t uint16_t uint32_t, uint64_t are portable
     
    ¬a\\/b, Jul 9, 2007
    #17
  18. ¬a\/b said:

    > On Sun, 08 Jul 2007 02:36:55 -0700, santosh wrote:


    <snip>

    >>size_t is defined in the Standard, so it's certainly portable. New
    >>users to C tend to have some confusion over using size_t objects with
    >>printf and family. Generally using the lu format specifier with a cast
    >>to the appropriate type is recommended. If you can, and want to, use
    >>C99's additions the zu specifier is specifically there for size_t.

    >
    > size_t is not portable because you all don't know the size it has


    Nonsense. It is exactly sizeof(size_t) bytes in size.

    > only uint8_t uint16_t uint32_t, uint64_t are portable


    My implementation has never heard of them, and gives me compilation
    errors when I try to use them. So much for "portable".

    But my implementation understands size_t just fine, thanks.

    --
    Richard Heathfield <http://www.cpax.org.uk>
    Email: -www. +rjh@
    Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
    "Usenet is a strange place" - dmr 29 July 1999
     
    Richard Heathfield, Jul 9, 2007
    #18
  19. empriser

    Flash Gordon Guest

    ¬a\/b wrote, On 09/07/07 07:42:
    > On Sun, 08 Jul 2007 02:36:55 -0700, santosh wrote:
    >> Guru Jois wrote:
    >>> On Jul 5, 6:42 pm, Peter Nilsson <> wrote:
    >>>> Eric Sosman <> wrote:
    >>>>> empriser wrote:
    >>>>>> ...
    >>>>>> Yes my buffer size is n (n > 1).
    >>>>>> How do I know there are how many bytes in buffer,
    >>>>>> after last call fread as if return 0.
    >>>>> fread returns the number of elements that were
    >>>>> read and stored in the buffer. If fread returns 10,
    >>>>> it successfully read and stored 10 elements. If
    >>>>> fread returns 0, it read and stored ... <wait for
    >>>>> it> ... no elements.
    >>>> In other words, code as...
    >>>>
    >>>> static char buf[1024];
    >>>> size_t r = fread(buf, 1, sizeof buf, fp);
    >>>>
    >>>> ...rather than...
    >>>>
    >>>> static char buf[1024];
    >>>> size_t r = fread(buf, sizeof buf, 1, fp);
    >>>>
    >>>> Assuming no read errors, if there are 123 characters
    >>>> left in the stream, the former will return 0 into r,
    >>>> whereas the latter will return 123. Both calls will
    >>>> read the same number of characters though.
    >>> Is size_t and all such stuffs portable in c? If not what to make it
    >>> so?

    >> size_t is defined in the Standard, so it's certainly portable. New
    >> users to C tend to have some confusion over using size_t objects with
    >> printf and family. Generally using the lu format specifier with a cast
    >> to the appropriate type is recommended. If you can, and want to, use
    >> C99's additions the zu specifier is specifically there for size_t.

    >
    > size_t is not portable because you all don't know the size it has
    > only uint8_t uint16_t uint32_t, uint64_t are portable


    Rubbish. There is no guarantee that the types you are suggesting are
    available (even assuming C99) so they are less portable than size_t
    which is *guaranteed* to exist. size_t is also the type returned by
    fread (the function being called above), so whatever it is a variable of
    type size_t is *guaranteed* to be large enough.
    --
    Flash Gordon
     
    Flash Gordon, Jul 9, 2007
    #19
  20. empriser

    ¬a\\/b Guest

    On Mon, 09 Jul 2007 08:19:40 +0100, Flash Gordon
    <> wrote:

    >¬a\/b wrote, On 09/07/07 07:42:
    >> On Sun, 08 Jul 2007 02:36:55 -0700, santosh wrote:
    >>> Guru Jois wrote:
    >>>> On Jul 5, 6:42 pm, Peter Nilsson <> wrote:
    >>>>> Eric Sosman <> wrote:
    >>>>>> empriser wrote:
    >>>>>>> ...
    >>>>>>> Yes my buffer size is n (n > 1).
    >>>>>>> How do I know there are how many bytes in buffer,
    >>>>>>> after last call fread as if return 0.
    >>>>>> fread returns the number of elements that were
    >>>>>> read and stored in the buffer. If fread returns 10,
    >>>>>> it successfully read and stored 10 elements. If
    >>>>>> fread returns 0, it read and stored ... <wait for
    >>>>>> it> ... no elements.
    >>>>> In other words, code as...
    >>>>>
    >>>>> static char buf[1024];
    >>>>> size_t r = fread(buf, 1, sizeof buf, fp);
    >>>>>
    >>>>> ...rather than...
    >>>>>
    >>>>> static char buf[1024];
    >>>>> size_t r = fread(buf, sizeof buf, 1, fp);
    >>>>>
    >>>>> Assuming no read errors, if there are 123 characters
    >>>>> left in the stream, the former will return 0 into r,
    >>>>> whereas the latter will return 123. Both calls will
    >>>>> read the same number of characters though.
    >>>> Is size_t and all such stuffs portable in c? If not what to make it
    >>>> so?
    >>> size_t is defined in the Standard, so it's certainly portable. New
    >>> users to C tend to have some confusion over using size_t objects with
    >>> printf and family. Generally using the lu format specifier with a cast
    >>> to the appropriate type is recommended. If you can, and want to, use
    >>> C99's additions the zu specifier is specifically there for size_t.

    >>
    >> size_t is not portable because you all don't know the size it has
    >> only uint8_t uint16_t uint32_t, uint64_t are portable

    >
    >Rubbish. There is no guarantee that the types you are suggesting are
    >available (even assuming C99) so they are less portable than size_t
    >which is *guaranteed* to exist. size_t is also the type returned by
    >fread (the function being called above), so whatever it is a variable of
    >type size_t is *guaranteed* to be large enough.


    each data definition that not fixed the size is unportable
    each operation to data operand not well definited is unportable
     
    ¬a\\/b, Jul 9, 2007
    #20
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Brady

    problem using fread, fwrite, and fsetpos

    Brady, Jul 17, 2003, in forum: C Programming
    Replies:
    8
    Views:
    966
    Dave Thompson
    Jul 21, 2003
  2. Guest

    fread / fwrite error

    Guest, Aug 1, 2003, in forum: C Programming
    Replies:
    9
    Views:
    1,070
    Emmanuel Delahaye
    Aug 7, 2003
  3. CBFalconer

    Re: fread/fwrite Bits

    CBFalconer, Jan 3, 2004, in forum: C Programming
    Replies:
    0
    Views:
    861
    CBFalconer
    Jan 3, 2004
  4. mazsx
    Replies:
    2
    Views:
    875
    Toni Uusitalo
    Nov 11, 2005
  5. fread fwrite struct

    , Mar 30, 2006, in forum: C Programming
    Replies:
    4
    Views:
    478
    Michael Mair
    Mar 31, 2006
Loading...

Share This Page