Can someone explain this behavior ?

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

    Can someone explain this behavior ?

    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(Consecuti ve 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(T AB)4(TAB)5(TAB) 6(TAB)7(TAB)8(T AB)9(TAB)10

    o/p

    1(TAB)2(TAB)3(T AB)4(TAB)5(TAB) 6(TAB)7(TAB)8(T AB)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

  • Harald van =?UTF-8?b?RMSzaw==?=

    #2
    Re: Can someone explain this behavior ?

    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(Consecuti ve 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
    123456789012345 67890

    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(T AB)4(TAB)5(TAB) 6(TAB)7(TAB)8(T AB)9(TAB)10
    >
    o/p
    >
    1(TAB)2(TAB)3(T AB)4(TAB)5(TAB) 6(TAB)7(TAB)8(T AB)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
    123456789012345 67890

    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.

    Comment

    • Mark McIntyre

      #3
      Re: Can someone explain this behavior ?

      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

      Comment

      • Keith Thompson

        #4
        Re: Can someone explain this behavior ?

        Mark McIntyre <markmcintyre@s pamcop.netwrite s:
        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_Keit h) <kst-u@mib.org>
        Nokia
        "We must do something. This is something. Therefore, we must do this."
        -- Antony Jay and Jonathan Lynn, "Yes Minister"

        Comment

        • Mark McIntyre

          #5
          Re: Can someone explain this behavior ?

          Keith Thompson wrote:
          Mark McIntyre <markmcintyre@s pamcop.netwrite s:
          >broli wrote:
          >>I wrote a simple program that copies input to output but this is done
          >>by using fread and fwrite.
          >>>
          >>#include<stdi o.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 >

          Comment

          • Ben Bacarisse

            #6
            Re: Can someone explain this behavior ?

            broli <Broli00@gmail. comwrites:
            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(Consecuti ve 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.

            Comment

            Working...