473,471 Members | 2,089 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

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

Mar 15 '08 #1
5 1341
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.
Mar 15 '08 #2
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
Mar 15 '08 #3
Mark McIntyre <ma**********@spamcop.netwrites:
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) <ks***@mib.org>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Mar 15 '08 #4
Keith Thompson wrote:
Mark McIntyre <ma**********@spamcop.netwrites:
>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>
Mar 16 '08 #5
broli <Br*****@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(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.
Mar 16 '08 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: John Laco | last post by:
Dear Gentlemen (and Ladies of course), I am facing an interesting problem (which may be not be a problem). I am running win2000 server IIS5 (with newest SP). This is our development server and I...
14
by: bo | last post by:
And why and where one should use one vs. the other? Verbally, it seems like semantics to me--but obviously there is some actual difference that makes references different and or preferable over...
25
by: REH | last post by:
Someone more articulate than me please explain to Ioannis Vranos why the following two programs both exhibit undefined behavior: > #include <iostream> > #include <cstring> > > class SomeClass...
22
by: Jaspreet | last post by:
I was recently asked this question in an interview. Unfortunately I was not able to answer it and the interviewer made a decision on my C strengths (or weekness) based on this single question and...
20
by: nicolas.riesch | last post by:
I try to understand strict aliasing rules that are in the C Standard. As gcc applies these rules by default, I just want to be sure to understand fully this issue. For questions (1), (2) and...
4
by: Richard L Rosenheim | last post by:
Using Visual Studio 2003 running under Windows XP Pro w/SP 1, when I create a new form, the form's title bar has the Windows 2000 look. Yet, in the various Microsoft demos, the form's title bar...
9
by: M. Posseth | last post by:
i have 3 forms Public Class Form1 Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Dim frm As New Form2 frm.Show(Me) End Sub...
1
by: silentscreams22 | last post by:
I need this converted. <marquee behavior="scroll" direction="down" scrollamount="3" style="position:absolute; left:110px; top:150px; width:16px; height:483px; z-index:1;"><span...
7
by: Mike Kent | last post by:
It's often useful for debugging to print something to stderr, and to route the error output to a file using '2>filename' on the command line. However, when I try that with a python script, all...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.