Can someone explain this behavior ?

Discussion in 'C Programming' started by broli, Mar 15, 2008.

  1. broli

    broli Guest

    I wrote a simple program that copies input to output but this is done
    by using fread and fwrite.

    #include<stdio.h>

    int main(void)
    {

    FILE *fp;
    int foo[10];
    int a[10];


    fread(a, sizeof(int), 10, stdin);
    fwrite(a, sizeof(int), 10, stdout);

    return 0;

    }

    Here's my input(Consecutive ints separated by a single space)

    1 2 3 4 5 6 7 8 9 10

    The output (as I expected)

    1 2 3 4 5 6 7 8 9 10

    When consecutive ints are separated by tabs -
    i/p

    1(TAB)2(TAB)3(TAB)4(TAB)5(TAB)6(TAB)7(TAB)8(TAB)9(TAB)10

    o/p

    1(TAB)2(TAB)3(TAB)4(TAB)5(TAB)6(TAB)7(TAB)8(TAB)9(TAB)10

    ^^ Can anyone explain why this happened ??

    Also when consecutive ints are seperated by inconsistent blank spaces

    1 2 3 4 5 6 7 8 9 10

    then o/p is

    1 2 3 4 5 6 7


    or 1 2 3 4 5 6 7 8 9 10
    1 2 3 4 5 6 7 8 9 1
     
    broli, Mar 15, 2008
    #1
    1. Advertising

  2. On Sat, 15 Mar 2008 10:03:27 -0700, broli wrote:
    > I wrote a simple program that copies input to output but this is done
    > by using fread and fwrite.
    >
    > #include<stdio.h>
    >
    > int main(void)
    > {
    >
    > FILE *fp;
    > int foo[10];
    > int a[10];
    >
    >
    > fread(a, sizeof(int), 10, stdin);


    This reads 10 * sizeof(int) characters into a buffer.

    > fwrite(a, sizeof(int), 10, stdout);


    This writes 10 * sizeof(int) characters from a buffer.

    > return 0;
    >
    > }
    >
    > Here's my input(Consecutive ints separated by a single space)
    >
    > 1 2 3 4 5 6 7 8 9 10


    Let's count.

    1 2 3 4 5 6 7 8 9 10
    12345678901234567890

    Exactly 20 characters. If sizeof(int) happens to be 2 on your system,
    this will fit nicely into a buffer of 20 bytes.

    > The output (as I expected)
    >
    > 1 2 3 4 5 6 7 8 9 10


    Right. You write out what you read in.

    > When consecutive ints are separated by tabs - i/p
    >
    > 1(TAB)2(TAB)3(TAB)4(TAB)5(TAB)6(TAB)7(TAB)8(TAB)9(TAB)10
    >
    > o/p
    >
    > 1(TAB)2(TAB)3(TAB)4(TAB)5(TAB)6(TAB)7(TAB)8(TAB)9(TAB)10
    >
    > ^^ Can anyone explain why this happened ??


    You got back the 20 characters that you entered.

    > Also when consecutive ints are seperated by inconsistent blank spaces
    >
    > 1 2 3 4 5 6 7 8 9 10
    >
    > then o/p is
    >
    > 1 2 3 4 5 6 7


    Let's count again.

    1 2 3 4 5 6 7 8 9 10
    12345678901234567890

    When you read the first 20 characters, which characters will you read? So
    which characters will you write back?

    In short: fread and fwrite don't do what you think they do. Look at the
    *scanf family of functions if you want formatted input.
     
    Harald van Dijk, Mar 15, 2008
    #2
    1. Advertising

  3. broli wrote:
    > I wrote a simple program that copies input to output but this is done
    > by using fread and fwrite.
    >
    > #include<stdio.h>
    >
    > int main(void)
    > {
    >
    > FILE *fp;
    > int foo[10];
    > int a[10];
    >
    >
    > fread(a, sizeof(int), 10, stdin);


    This fread reads 10 lots of sizeof(int) bytes from stdin.
    Since sizeof(int) is two on your system ,you read 20 bytes.

    fread does not scan for objects. It reads blocks of data.
    You want the fgets followed by sscanf. Don't forget to check the result
    of the functions
     
    Mark McIntyre, Mar 15, 2008
    #3
  4. Mark McIntyre <> writes:
    > broli wrote:
    >> I wrote a simple program that copies input to output but this is done
    >> by using fread and fwrite.
    >>
    >> #include<stdio.h>
    >>
    >> int main(void)
    >> {
    >>
    >> FILE *fp;
    >> int foo[10];
    >> int a[10];
    >>
    >>
    >> fread(a, sizeof(int), 10, stdin);

    >
    > This fread reads 10 lots of sizeof(int) bytes from stdin.
    > Since sizeof(int) is two on your system ,you read 20 bytes.


    I don't think we can conclude that sizeof(int) is 2 on his system. It
    could well be 4. It's 4 on my system, and I get results that *look*
    very much like his.

    The fread call only reads 5 ints (20 bytes); since he doesn't check
    the returned value, the failure is silent.

    The fwrite call writes all 40 bytes of the buffer, but everything
    after the data read into the buffer is likely (but by no means
    certain) to be null characters, which are likely (but by no means
    certain) to have no visible effect on output. Displaying the values
    returned by the fread and fwrite calls, and viewing the output in some
    other manner (on Unix, by piping it through "cat -A") will show what's
    going on.

    > fread does not scan for objects. It reads blocks of data.


    That's an odd use of the word "objects".

    fread reads data, typically binary data, as a sequence of bytes. It
    *can* be used to read text data (you'll even get the proper new-line
    conversions if the file is in text mode), but it's clumsy, since it
    doesn't care about line boundaries.

    > You want the fgets followed by sscanf. Don't forget to check the
    > result of the functions


    There are drawbacks to the fgets/sscanf approach, but it's a good
    start -- certainly better in this context than fread/fwrite.

    --
    Keith Thompson (The_Other_Keith) <>
    Nokia
    "We must do something. This is something. Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"
     
    Keith Thompson, Mar 15, 2008
    #4
  5. Keith Thompson wrote:
    > Mark McIntyre <> writes:
    >> broli wrote:
    >>> I wrote a simple program that copies input to output but this is done
    >>> by using fread and fwrite.
    >>>
    >>> #include<stdio.h>
    >>>
    >>> int main(void)
    >>> {
    >>>
    >>> FILE *fp;
    >>> int foo[10];
    >>> int a[10];
    >>>
    >>>
    >>> fread(a, sizeof(int), 10, stdin);

    >> This fread reads 10 lots of sizeof(int) bytes from stdin.
    >> Since sizeof(int) is two on your system ,you read 20 bytes.

    >
    > I don't think we can conclude that sizeof(int) is 2 on his system. It
    > could well be 4. It's 4 on my system, and I get results that *look*
    > very much like his.


    You're right.

    > The fread call only reads 5 ints (20 bytes); since he doesn't check
    > the returned value, the failure is silent.


    Quite!

    >> fread does not scan for objects. It reads blocks of data.

    >
    > That's an odd use of the word "objects".


    Yes... I was struggling to find the words.
    "your fread call reads 10 things of sizeof(int)" apparently suggests to
    the OP that its looking for 10 integers, and ignoring whitespace.


    --
    Mark McIntyre

    CLC FAQ <http://c-faq.com/>
    CLC readme: <http://www.ungerhu.com/jxh/clc.welcome.txt>
     
    Mark McIntyre, Mar 16, 2008
    #5
  6. broli <> writes:

    > I wrote a simple program that copies input to output but this is done
    > by using fread and fwrite.
    >
    > #include<stdio.h>
    >
    > int main(void)
    > {
    > FILE *fp;
    > int a[10];
    >
    > fread(a, sizeof(int), 10, stdin);
    > fwrite(a, sizeof(int), 10, stdout);
    >
    > return 0;
    > }
    >
    > Here's my input(Consecutive ints separated by a single space)
    >
    > 1 2 3 4 5 6 7 8 9 10
    >
    > The output (as I expected)
    >
    > 1 2 3 4 5 6 7 8 9 10


    You've had lots of explanations, so I won't add my own, but I will say
    that symmetric input and output is a very bad way to test things --
    you often get good looking output even if the stuff in the middle was
    junk. It looks like you expect the code above to read the 10 integers
    into the 10 element array a. Had you printed out, say a[3] on its own
    using printf("%d\n", a[3]); you might have got a surprise earlier!

    --
    Ben.
     
    Ben Bacarisse, Mar 16, 2008
    #6
    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. Replies:
    1
    Views:
    384
    brucie
    Dec 3, 2003
  2. Michael Kent
    Replies:
    4
    Views:
    420
    David MacQuigg
    Jun 15, 2004
  3. Tim Peters
    Replies:
    2
    Views:
    317
    Mike C. Fletcher
    Jun 14, 2004
  4. Mike Kent
    Replies:
    9
    Views:
    340
    Mike Kent
    Jan 24, 2008
  5. Jesse Aldridge

    Can someone explain this behavior to me?

    Jesse Aldridge, Feb 26, 2009, in forum: Python
    Replies:
    5
    Views:
    290
    Jesse Aldridge
    Feb 27, 2009
Loading...

Share This Page