Velocity Reviews > Can someone explain this behavior ?

# Can someone explain this behavior ?

broli
Guest
Posts: n/a

 03-15-2008
I wrote a simple program that copies input to output but this is done

#include<stdio.h>

int main(void)
{

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

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

Harald van Dĳk
Guest
Posts: n/a

 03-15-2008
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];
>
>

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.

Mark McIntyre
Guest
Posts: n/a

 03-15-2008
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];
>
>

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

Keith Thompson
Guest
Posts: n/a

 03-15-2008
Mark McIntyre <(E-Mail Removed)> 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];
>>
>>

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

> 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) <(E-Mail Removed)>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"

Mark McIntyre
Guest
Posts: n/a

 03-16-2008
Keith Thompson wrote:
> Mark McIntyre <(E-Mail Removed)> 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];
>>>
>>>

>> 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.
the OP that its looking for 10 integers, and ignoring whitespace.

--
Mark McIntyre

CLC FAQ <http://c-faq.com/>

Ben Bacarisse
Guest
Posts: n/a

 03-16-2008
broli <(E-Mail Removed)> 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];
>
> 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.