fread()

Discussion in 'C Programming' started by h03Ein, Jan 23, 2008.

  1. h03Ein

    h03Ein Guest

    Hi!
    how many characters will be printed in output if our input file has
    just 3 characters?
    Sinppet Code :
    #include<stdio.h>
    main()
    {
    char buffer[1024];
    char input[] = "some file"
    FILE *fp = fopen(input , "r");
    int c = fread(buffer , 1 , 1024, fp);
    int i = 0;
    for(i = 0 ; buffer!=EOF ; ++i)
    printf("%c" , buffer);
    fclose(fp);
    }
    the result for me is my 3 input characters in file plus lots of
    strange characters why?
    does fread consider EOF while reading from file?
    Thanks!
    h03Ein, Jan 23, 2008
    #1
    1. Advertising

  2. h03Ein

    santosh Guest

    h03Ein wrote:

    > Hi!
    > how many characters will be printed in output if our input file has
    > just 3 characters?
    > Sinppet Code :
    > #include<stdio.h>
    > main()
    > {
    > char buffer[1024];
    > char input[] = "some file"
    > FILE *fp = fopen(input , "r");


    I hope you actually check that the above call succeeded?

    > int c = fread(buffer , 1 , 1024, fp);
    > int i = 0;


    Mixed declarations and code are new to C99, which isn't implemented
    widely yet.

    > for(i = 0 ; buffer!=EOF ; ++i)
    > printf("%c" , buffer);
    > fclose(fp);
    > }
    > the result for me is my 3 input characters in file plus lots of
    > strange characters why?
    > does fread consider EOF while reading from file?


    No. It returns the count of characters that it actually read, which may
    be less than what you asked it for or zero. To find out if it was
    end-of-file or an error that was responsible for the short count you
    must use feof() or ferror() after the fread() call.

    Specifically in the above for loop you must consider only the first 'i'
    characters of buffer as valid.
    santosh, Jan 23, 2008
    #2
    1. Advertising

  3. "h03Ein" <> wrote in message
    > Hi!
    > how many characters will be printed in output if our input file has
    > just 3 characters?
    > Sinppet Code :
    > #include<stdio.h>
    > main()
    > {
    > char buffer[1024];
    > char input[] = "some file"
    > FILE *fp = fopen(input , "r");
    > int c = fread(buffer , 1 , 1024, fp);
    > int i = 0;
    > for(i = 0 ; buffer!=EOF ; ++i)
    > printf("%c" , buffer);
    > fclose(fp);
    > }
    > the result for me is my 3 input characters in file plus lots of
    > strange characters why?
    > does fread consider EOF while reading from file?
    > Thanks!
    >

    fread() will stop reading when it encounters EOF.
    The return value is the number of items read successfully, and anything
    afterwards is garbage - in your case, strange characters.
    The EOF is not itself placed into the buffer and is in any case an integer.
    If you think about it, it is necessary for EOF to be wider than a character,
    if every character is to be representable.

    --
    Free games and programming goodies.
    http://www.personal.leeds.ac.uk/~bgy1mm
    Malcolm McLean, Jan 23, 2008
    #3
  4. h03Ein

    santosh Guest

    Malcolm McLean wrote:

    >
    > "h03Ein" <> wrote in message
    >> Hi!
    >> how many characters will be printed in output if our input file has
    >> just 3 characters?
    >> Sinppet Code :
    >> #include<stdio.h>
    >> main()
    >> {
    >> char buffer[1024];
    >> char input[] = "some file"
    >> FILE *fp = fopen(input , "r");
    >> int c = fread(buffer , 1 , 1024, fp);
    >> int i = 0;
    >> for(i = 0 ; buffer!=EOF ; ++i)
    >> printf("%c" , buffer);
    >> fclose(fp);
    >> }
    >> the result for me is my 3 input characters in file plus lots of
    >> strange characters why?
    >> does fread consider EOF while reading from file?
    >> Thanks!
    >>

    > fread() will stop reading when it encounters EOF.


    <pedantic>
    When it encounters end-of-file condition. EOF is the object like macro
    used to signal end-of-file /or/ error to user code, and it is /not/
    used by fread().
    </pedantic>

    <snip>
    santosh, Jan 23, 2008
    #4
  5. h03Ein

    Army1987 Guest

    h03Ein wrote:

    > Hi!
    > how many characters will be printed in output if our input file has
    > just 3 characters?
    > Sinppet Code :
    > #include<stdio.h>
    > main()
    > {
    > char buffer[1024];
    > char input[] = "some file"
    > FILE *fp = fopen(input , "r");

    You should verify that it succeeds:
    if (fp == NULL) {
    perror("Cannot open file");
    exit(EXIT_FAILURE);
    }
    or whatever is appropriate. exit and EXIT_FAILURE are declared/defined in
    stdlib.h.

    > int c = fread(buffer , 1 , 1024, fp);

    fread returns a size_t. You're using 1024 bytes, and int is required to
    hold numbers to 32767, so, in this case, it is OK, but, in general, I
    would use a size_t. Also, decimal constants ("magic numbers") tend to make
    programs harder to understand. Use sizeof buffer in place of 1024 on that
    line. Or use #define BUFFERSIZE 1024, declare buffer as `char
    buffer[BUFFERSIZE]`, and use fread(buffer, 1, BUFFERSIZE, fp);

    > int i = 0;
    > for(i = 0 ; buffer!=EOF ; ++i)

    Make it: (see below)
    for (i = 0; i < c; i++)
    > printf("%c" , buffer);

    Why? putchar(buffer) would do that. Do you use a hammer to plant
    drawing pins?
    BTW, you could just use fwrite(buffer, 1, c, stdout); to do the same thing.
    > fclose(fp);
    > }
    > the result for me is my 3 input characters in file plus lots of strange
    > characters why?

    EOF is a negative integer. If char is signed, that loop will stop when
    hitting a valid character, e.g. ÿ. If it is unsigned, it will go on until
    the end of the memory you're allowed to read, and then the behavior is
    undefined. See www.c-faq.com, section 12.

    > does fread consider EOF while reading from file?

    It returns the numbers of characters successfully read, so the loop should
    be changed as above.

    --
    Army1987 (Replace "NOSPAM" with "email")
    Army1987, Jan 23, 2008
    #5
  6. h03Ein

    Guest

    In article <fn846t$amn$>,
    santosh <> wrote:
    >h03Ein wrote:
    >
    >> Hi!
    >> how many characters will be printed in output if our input file has
    >> just 3 characters?
    >> Sinppet Code :
    >> #include<stdio.h>
    >> main()
    >> {
    >> char buffer[1024];
    >> char input[] = "some file"
    >> FILE *fp = fopen(input , "r");

    >
    >I hope you actually check that the above call succeeded?
    >
    >> int c = fread(buffer , 1 , 1024, fp);
    >> int i = 0;

    >
    >Mixed declarations and code are new to C99, which isn't implemented
    >widely yet.


    I don't see any mixed declarations and code here. A bunch of
    initializations that would be better done after the declarations, yes,
    but all the executable code above this point is in the
    initializations.

    If he was mixing declarations and code, he'd have a program that was
    not valid under any C standard, since he's implicit-inting main (which
    went away in C99).


    dave

    --
    Dave Vandervies dj3vande at eskimo dot com
    Why does all this bother you with parachutes and not with cars? Many cars
    can take you past terminal velocity.
    --Maarten Wiltink in the scary devil monastery
    , Jan 23, 2008
    #6
  7. h03Ein

    CBFalconer Guest

    h03Ein wrote:
    >
    > how many characters will be printed in output if our input file
    > has just 3 characters?
    >
    > Sinppet Code :
    > #include<stdio.h>
    > main() {
    > char buffer[1024];
    > char input[] = "some file"
    > FILE *fp = fopen(input , "r");
    > int c = fread(buffer , 1 , 1024, fp);
    > int i = 0;
    > for(i = 0 ; buffer!=EOF ; ++i)
    > printf("%c" , buffer);
    > fclose(fp);
    > }
    > the result for me is my 3 input characters in file plus lots of
    > strange characters why? does fread consider EOF while reading
    > from file?


    Yes, but it is a characteristic of the streamed device, not of the
    char stream. The value in c should be 3 if only 3 chars were
    read. What you have in buffer is not a string, because it lacks a
    final '\0'.

    --
    [mail]: Chuck F (cbfalconer at maineline dot net)
    [page]: <http://cbfalconer.home.att.net>
    Try the download section.



    --
    Posted via a free Usenet account from http://www.teranews.com
    CBFalconer, Jan 24, 2008
    #7
    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. news.amnet.net.au

    Equivalent of "fread" C function in Java

    news.amnet.net.au, Jan 15, 2004, in forum: Java
    Replies:
    2
    Views:
    4,710
  2. Patrick
    Replies:
    6
    Views:
    845
    Roedy Green
    Jul 13, 2004
  3. Axel
    Replies:
    12
    Views:
    639
  4. Replies:
    3
    Views:
    4,848
    Sharad Kala
    May 4, 2004
  5. Brady

    problem using fread, fwrite, and fsetpos

    Brady, Jul 17, 2003, in forum: C Programming
    Replies:
    8
    Views:
    925
    Dave Thompson
    Jul 21, 2003
Loading...

Share This Page