Can someone explain this behavior ?

B

broli

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
 
H

Harald van Dijk

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

Mark McIntyre

broli said:
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
 
K

Keith Thompson

Mark McIntyre said:
broli said:
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.
 
M

Mark McIntyre

Keith said:
Mark McIntyre said:
broli said:
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!


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

Ben Bacarisse

broli said:
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!
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,767
Messages
2,569,571
Members
45,045
Latest member
DRCM

Latest Threads

Top