fread/fwrite

M

Mark Bluemel

*you* might not. It's a technical news group. if you ask a technical question
it's assumed you are interested in the answer! So you're just here to waste our time?

So it is b)...
that's because you're a cretin

No - if he's what I'm increasingly inclined to believe he is, then
he's rather clever, if somewhat perverse.
I don't believe you've learnt anything

He's learnt how to yank your chain. I suggest kill-filing him and
updating your kill-file as he mutates his identity.
 
I

Ian Collins

On 06/21/12 11:47 AM, Bill Cunningham wrote: [...]
I have read the man pages and googled them many times on fread and
fwrite. I have tried to go by that and failed.

The first hit I get for fread is

http://www.cplusplus.com/reference/clibrary/cstdio/fread/

Nit: I would probably avoid a domain called "cplusplus.com" if you were
looking for answers regarding C, and not "that other language". :)

Except where it is discussing the shared standard library!
 
J

James Kuyper

On 06/21/12 11:47 AM, Bill Cunningham wrote: [...]
I have read the man pages and googled them many times on fread and
fwrite. I have tried to go by that and failed.

The first hit I get for fread is

http://www.cplusplus.com/reference/clibrary/cstdio/fread/

Nit: I would probably avoid a domain called "cplusplus.com" if you were
looking for answers regarding C, and not "that other language". :)

Except where it is discussing the shared standard library!

Even there.

Section 2 of annex C of the C++ standard has 3 1/2 pages listing the
ways in which the C++ version of the C standard library differs from the
C version. Unless you're familiar with precisely what those differences
are, you shouldn't assume that information about the C++ version will be
valid for the C version. Since only C/C++ experts are likely to have
that information, it's generally not a good idea for a newbie to go to
the wrong place for such information.

The only difference that matters for stdio.h is that NULL is a required
to be a C++ null pointer constant, rather than a C null pointer
constant. The simplest consequence is that NULL cannot expand to an
expression with type void*. The other consequence is that NULL can
expand to any one of several types of things that would not qualify as
integer constant expressions in C, such as:

1. An expression referring to a non-volatile const-qualified object of
integral or enumeration type, initialized with a integer constant
expression.
2. A value of type std::nullptr_t, such as nullptr.
3. A call to a constexpr function or constructor.

Offhand I can't come up with any way for this difference to affect a
strictly conforming C program, unless it makes use of the new C2011
_Generic feature, which is not (yet?) a part of C++.
 
K

Keith Thompson

James Kuyper said:
Section 2 of annex C of the C++ standard has 3 1/2 pages listing the
ways in which the C++ version of the C standard library differs from the
C version. Unless you're familiar with precisely what those differences
are, you shouldn't assume that information about the C++ version will be
valid for the C version. Since only C/C++ experts are likely to have
that information, it's generally not a good idea for a newbie to go to
the wrong place for such information.

The only difference that matters for stdio.h is that NULL is a required
to be a C++ null pointer constant, rather than a C null pointer
constant. The simplest consequence is that NULL cannot expand to an
expression with type void*. The other consequence is that NULL can
expand to any one of several types of things that would not qualify as
integer constant expressions in C, such as:

1. An expression referring to a non-volatile const-qualified object of
integral or enumeration type, initialized with a integer constant
expression.
2. A value of type std::nullptr_t, such as nullptr.
3. A call to a constexpr function or constructor.
[...]

I wouldn't call that a difference in the library functions; rather
it's a difference in the languages, specifically in how they express
null pointer constants. The behavior of fread() and fwrite() when
given a null pointer as an argument is the same in both languages --
or, rather, it's undefined in both languages.
 
J

James Kuyper

James Kuyper said:
Section 2 of annex C of the C++ standard has 3 1/2 pages listing the
ways in which the C++ version of the C standard library differs from the
C version. Unless you're familiar with precisely what those differences
are, you shouldn't assume that information about the C++ version will be
valid for the C version. Since only C/C++ experts are likely to have
that information, it's generally not a good idea for a newbie to go to
the wrong place for such information.

The only difference that matters for stdio.h is that NULL is a required
to be a C++ null pointer constant, rather than a C null pointer
constant. The simplest consequence is that NULL cannot expand to an
expression with type void*. The other consequence is that NULL can
expand to any one of several types of things that would not qualify as
integer constant expressions in C, such as:

1. An expression referring to a non-volatile const-qualified object of
integral or enumeration type, initialized with a integer constant
expression.
2. A value of type std::nullptr_t, such as nullptr.
3. A call to a constexpr function or constructor.
[...]

I wouldn't call that a difference in the library functions; rather
it's a difference in the languages, specifically in how they express
null pointer constants. The behavior of fread() and fwrite() when
given a null pointer as an argument is the same in both languages --
or, rather, it's undefined in both languages.

I was pointing out that it's a difference in the standard headers.
#include <stdio.h> is permitted in both languages, but the #definition
of NULL that results from that #inclusion can be a very weird animal,
from a C point of view, if you're using C++ to compile it. I believe
that the main point of defining nullptr was to allow

#define NULL nullptr

I wasn't addressing the specific issue of fread/fwrite, but the more
general issue of whether it's a good idea to rely upon a C++ source for
information about the C standard library. There are differences; there
aren't many, and they aren't major, but they do exist.
 
K

Keith Thompson

James Kuyper said:
I wasn't addressing the specific issue of fread/fwrite, but the more
general issue of whether it's a good idea to rely upon a C++ source for
information about the C standard library. There are differences; there
aren't many, and they aren't major, but they do exist.

Agreed.
 
G

Guest

Bill Cunningham said:
I have read the man pages and googled them many times on fread and
fwrite. I have tried to go by that and failed. That's why I asked for
an example. In other places people readily showed me one with no
problem and I see now what I might have been doing wrong. The man
pages I looked at gave no example. Only parameters and a somewhat
cryptic mention of ferror and feof.

Here's an example (not my code):

#include <stdio.h>
#include <stdlib.h>

int main(void)
{
char buf [2048];
FILE *fp_in;
FILE *fp_out;
size_t ch_read;
if ((fp_in = fopen ("as.exe", "rb")) == NULL)
{
fprintf (stderr, "failed to open input file\n");
exit (EXIT_FAILURE);
}
if ((fp_out = fopen ("a.exe", "wb")) == NULL)
{
fprintf (stderr, "failed to open output file\n");
fclose (fp_in);
exit (EXIT_FAILURE);
}
ch_read = sizeof(buf);
while (ch_read == sizeof(buf))
{
ch_read = fread (buf, 1, sizeof(buf), fp_in);
if ((ch_read != sizeof(buf) && ferror(fp_in))
{
fprintf (stderr, "read error\n");
fclose (fp_in);
fclose (fp_out);
exit (EXIT_FAILURE);
}
if (fwrite (buf, 1, ch_read, fp_out) != ch_read)
{
fprintf (stderr, "write error\n");
fclose (fp_in);
fclose (fp_out);
exit (EXIT_FAILURE);
}
}
fclose (fp_in);
fclose (fp_out);
return 0;
}


It's Nick Keighley's example from 2008 -- simplified to remove a couple
of possibly confusing C idioms.

Source is Message-ID:
<657c2320-02d8-4413-9281-dee4a63d1583@f63g2000hsf.googlegroups.com>

good grief.
 
G

Guest

So it is b)...


No - if he's what I'm increasingly inclined to believe he is, then
he's rather clever, if somewhat perverse.


He's learnt how to yank your chain. I suggest kill-filing him and
updating your kill-file as he mutates his identity.

he certainly has. And someone has kindly posted an example =of mine=
from 4 years ago on the use of fread(). So it's not only Bill who is a
slow learner with a poor memory! Even if he isn't a troll (as many
people have suggested over the years) he has many of their attributes.

It was the suggestion that people don't even attempt to be helpful on
clc that finally blew the fuse.
 
B

Bill Cunningham

 I have read the man pages and googled them many times on fread and
fwrite. I have tried to go by that and failed. That's why I asked for
an example. In other places people readily showed me one with no
problem and I see now what I might have been doing wrong. The man
pages I looked at gave no example. Only parameters and a somewhat
cryptic mention of ferror and feof.

I learned how to use fread/fwrite while using Turbo C++ 1.01 [or
whatever the heck it was] which came free with a copy of "Type and
learn C" that I got when I was 10 [20 some odd years ago...]

Are you saying you're not as smart as a ten year old?

Tom

Four years ago? On the Klonopin I was taking... Not as smart as a
five year old.

B
 
B

Bill Cunningham

he certainly has. And someone has kindly posted an example =of mine=
from 4 years ago on the use of fread(). So it's not only Bill who is a
slow learner with a poor memory! Even if he isn't a troll (as many
people have suggested over the years) he has many of their attributes.

It was the suggestion that people don't even attempt to be helpful on
clc that finally blew the fuse.- Hide quoted text -

Now that I'm using google groups and figured out how to get Bill
Cunningham in the name section I am seeing where people can type in a
posters name and get old posts! And btw I have saved your example and
I'm studing it. I don't know about 4 years ago but the first thing I
wanted to change was insert a do while. I guess I didn't understand
that 4 years ago.

B
 

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

Similar Threads

fread/fwrite 2 18
fread/fwrite 34
H&S fread/fwrite 7
streams binary and text 4
fgetc() vs. fread() 8
Can not read VCD file in Linux 8
stream functions 23
strdup + fread = fail? 5

Members online

Forum statistics

Threads
474,079
Messages
2,570,574
Members
47,205
Latest member
ElwoodDurh

Latest Threads

Top