Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c > #22371 > unrolled thread

fread/fwrite

Started by"billcun@nspam.invalid" <billcun@suddenlink.net>
First post2012-06-18 13:45 -0700
Last post2012-06-20 09:53 -0700
Articles 20 on this page of 52 — 16 participants

Back to article view | Back to comp.lang.c


Contents

  fread/fwrite "billcun@nspam.invalid" <billcun@suddenlink.net> - 2012-06-18 13:45 -0700
    Re: fread/fwrite Ian Collins <ian-news@hotmail.com> - 2012-06-19 09:28 +1200
    Re: fread/fwrite Eric Sosman <esosman@ieee-dot-org.invalid> - 2012-06-18 17:47 -0400
      Re: fread/fwrite billcun@suddenlink.net - 2012-06-18 15:48 -0700
        Re: fread/fwrite Ike Naar <ike@iceland.freeshell.org> - 2012-06-18 23:37 +0000
        Re: fread/fwrite Eric Sosman <esosman@ieee-dot-org.invalid> - 2012-06-18 21:56 -0400
      Re: fread/fwrite billcun@suddenlink.net - 2012-06-19 04:54 -0700
        Re: fread/fwrite Eric Sosman <esosman@ieee-dot-org.invalid> - 2012-06-19 09:43 -0400
          Re: fread/fwrite billcun@suddenlink.net - 2012-06-19 09:51 -0700
            Re: fread/fwrite nick_keighley_nospam@hotmail.com - 2012-06-20 00:05 -0700
              Re: fread/fwrite James Kuyper <jameskuyper@verizon.net> - 2012-06-20 06:45 -0400
                Re: fread/fwrite nick_keighley_nospam@hotmail.com - 2012-06-20 04:20 -0700
                  Re: fread/fwrite tom st denis <tom@iahu.ca> - 2012-06-20 07:54 -0700
              Re: fread/fwrite billcun@suddenlink.net - 2012-06-20 08:13 -0700
                Re: fread/fwrite nick_keighley_nospam@hotmail.com - 2012-06-21 00:17 -0700
        Re: fread/fwrite nick_keighley_nospam@hotmail.com - 2012-06-19 08:17 -0700
          Re: fread/fwrite Mark Bluemel <mark.bluemel@googlemail.com> - 2012-06-20 01:05 -0700
            Re: fread/fwrite billcun@suddenlink.net - 2012-06-20 08:17 -0700
              Re: fread/fwrite Johannes Bauer <dfnsonfsduifb@gmx.de> - 2012-06-20 17:33 +0200
                Re: fread/fwrite billcun@suddenlink.net - 2012-06-20 08:50 -0700
                Re: fread/fwrite James Kuyper <jameskuyper@verizon.net> - 2012-06-21 07:45 -0400
              Re: fread/fwrite Kenneth Brody <kenbrody@spamcop.net> - 2012-06-20 18:38 -0400
                Re: fread/fwrite billcun@suddenlink.net - 2012-06-20 15:59 -0700
                  Re: fread/fwrite "BartC" <bc@freeuk.com> - 2012-06-21 00:24 +0100
                  Re: fread/fwrite Ian Collins <ian-news@hotmail.com> - 2012-06-21 11:40 +1200
                    Re: fread/fwrite Bill Cunningham <billcun@suddenlink.net> - 2012-06-20 16:47 -0700
                      Re: fread/fwrite Ian Collins <ian-news@hotmail.com> - 2012-06-21 11:56 +1200
                        Re: fread/fwrite Bill Cunningham <billcun@suddenlink.net> - 2012-06-20 17:11 -0700
                          Re: fread/fwrite Ian Collins <ian-news@hotmail.com> - 2012-06-21 12:28 +1200
                            Re: fread/fwrite Bill Cunningham <billcun@suddenlink.net> - 2012-06-20 17:51 -0700
                        Re: fread/fwrite Kenneth Brody <kenbrody@spamcop.net> - 2012-06-21 15:05 -0400
                          Re: fread/fwrite Ian Collins <ian-news@hotmail.com> - 2012-06-22 07:17 +1200
                            Re: fread/fwrite James Kuyper <jameskuyper@verizon.net> - 2012-06-21 16:01 -0400
                              Re: fread/fwrite Keith Thompson <kst-u@mib.org> - 2012-06-21 15:08 -0700
                                Re: fread/fwrite James Kuyper <jameskuyper@verizon.net> - 2012-06-21 18:36 -0400
                                  Re: fread/fwrite Keith Thompson <kst-u@mib.org> - 2012-06-21 16:49 -0700
                      Re: fread/fwrite Ben Bacarisse <ben.usenet@bsb.me.uk> - 2012-06-21 01:30 +0100
                        Re: fread/fwrite Bill Cunningham <billcun@suddenlink.net> - 2012-06-20 17:51 -0700
                          Re: fread/fwrite Ben Bacarisse <ben.usenet@bsb.me.uk> - 2012-06-21 02:22 +0100
                        Re: fread/fwrite nick_keighley_nospam@hotmail.com - 2012-06-22 00:13 -0700
                      Re: fread/fwrite tom st denis <tom@iahu.ca> - 2012-06-21 04:22 -0700
                        Re: fread/fwrite Bill Cunningham <billcun@suddenlink.net> - 2012-06-22 12:14 -0700
                  Re: fread/fwrite nick_keighley_nospam@hotmail.com - 2012-06-21 00:33 -0700
                Re: fread/fwrite Bill Cunningham <billcun@suddenlink.net> - 2012-06-20 16:27 -0700
              Re: fread/fwrite nick_keighley_nospam@hotmail.com - 2012-06-21 00:21 -0700
                Re: fread/fwrite Mark Bluemel <mark.bluemel@googlemail.com> - 2012-06-21 07:17 -0700
                  Re: fread/fwrite nick_keighley_nospam@hotmail.com - 2012-06-22 00:22 -0700
                    Re: fread/fwrite Bill Cunningham <billcun@suddenlink.net> - 2012-06-22 12:17 -0700
    Re: fread/fwrite John Gordon <gordon@panix.com> - 2012-06-18 22:05 +0000
      Re: fread/fwrite billcun@suddenlink.net - 2012-06-20 09:03 -0700
        Re: fread/fwrite John Gordon <gordon@panix.com> - 2012-06-20 16:28 +0000
          Re: fread/fwrite billcun@suddenlink.net - 2012-06-20 09:53 -0700

Page 2 of 3 — ← Prev page 1 [2] 3  Next page →


#22479

FromJames Kuyper <jameskuyper@verizon.net>
Date2012-06-21 07:45 -0400
Message-ID<jrv1gf$ugs$1@dont-email.me>
In reply to#22439
I've got Bill Cunningham killfiled, but when I saw his comments quoted
by other people I felt I had to respond. Because he's on my killfile, I
can't respond to his message directly, so this will be marked as if it's
a response to Johannes's Bauer's message. Sorry for the mis-attribution.

On Wed, 20 Jun 2012 08:50:53 -0700 (PDT), billcun@suddenlink.net wrote:
> Wow. Keep up the good work if it works for you. I have garnered
> questions from tutorials such as on linked lists. I brought some
> things up here and received numerous responses. The main one is "GIYF"
> and then "Do your own homework" and "try comp.programming or another
> NG"(concerning linked lists) I have *NEVER* got a good explanation. ...

You've received lots of good explanations over the past decade. People
with a wide variety of explanatory styles have done their best to
explain things in a way that you might be able to understand, but you've
never shown any sign of understanding any of them.

My own style is very verbose, detailed, and pedantic; for some people
that works, others find it either confusing or off-putting. I'm
disappointed, but not surprised, when any particular person fails to
understand any particular point I'm trying to make. But no one seems to
have a style that works with you.

> ... At
> best some clues or told to read C FAQs. clc is a very rude and blunt
> NG and that's its nature I have been warned and I can handle that. I
> have found personally Beej's guides to be very informative and learned
> about dereferencing too. I am beginning to learn pointers and write in
> structs and pointer members. *No* thanks to clc but another source. ...

What's particularly depressing is your periodic claims that you have
learned something about C, almost always immediately followed by
statements that make it quite clear that you have not. If you have
indeed found someone who does know how to explain things in a way that
you can understand, then I'm happy for you - but based upon previous
experience I suspect you're deluding yourself again.

> ... In
> a month very helpful people took me step by step. My suspicion is that
> clc is full or professional programmers that don't have the time or
> patience to work with amateurs like me anyway and there may be other

A large fraction of the people who post here regularly do so precisely
because we enjoy explaining how to use C to people who are eager to
learn it, even amateurs and hobbyists. Dozens, possibly hundreds of
people have been very patient with you for years. The problem is that,
sooner or later, you exhaust everyone's patience, by failing to
understand even the very clearest explanations of even the simplest
concepts, and by seldom ever following any of the good advice you've
been given.
-- 
James Kuyper

[toc] | [prev] | [next] | [standalone]


#22445

FromKenneth Brody <kenbrody@spamcop.net>
Date2012-06-20 18:38 -0400
Message-ID<042dnYUB0J5GzX_SnZ2dnUVZ_qudnZ2d@bestweb.net>
In reply to#22438
On 6/20/2012 11:17 AM, billcun@suddenlink.net wrote:
> On Jun 20, 4:05 am, Mark Bluemel <mark.blue...@googlemail.com> wrote:
>> On Jun 19, 4:17 pm, nick_keighley_nos...@hotmail.com wrote:
[...]
>> I can't decide whether he's a) simply incapable of learning or b) a
>> subtle troll pretending to be a), though I'm beginning to lean towards
>> b). Either way, I don't believe there is much point attempting to
>> teach him.
>
>    Things are not *taught* on clc. You do not learn anything.

I have been programming in C for about 29 years, and I've learned things 
about C in this group.  True, most people would probably consider those 
things "obscure", such as why my attempt to #define the name of a header 
file wasn't working:

     Compile with "-DMYHEADER=myheader.h"
     ...
     #include <MYHEADER>

because of the way "<MYHEADER>" is parsed.  Instead, I can use this, which 
does work:

     Compile with "-DMYHEADER='<myheader.h>'"
     ...
     #include MYHEADER

Then there are techniques that are "obvious" in today's C world, but which 
weren't consistently available in K&R1 days, and so I often use "antiquated" 
means of doing things, simply because that's the way I learned how to do 
them.  Seeing things mentioned here reminds me at times that there are 
better ways of doing things.

(I learned C along with the need to write with multiple platforms in mind. 
In some ways, starting with the need to support both big-endian 32-bit Unix 
systems and little-endian 16-bit MS-DOS systems taught me a lot about 
portability.  I definitely never had the "all the world's a VAX" attitude.)

> It is a C social group.

At times, yes.

> I can count on one hand what I've been *taught* on clc.

Perhaps the problem isn't the teacher?

> If you want to learn something on clc the response is generally...
> "GIYF" or "I don't do homework."

I rarely see "GIYF".  And the "I don't do homework" responses are to people 
who don't want to be taught, but rather want to be given the answer, often 
to a "problem" that is a typical homework-type assignment.

> You do not learn C here. You talk
> about it or your experience with C. I've learned more about writing C
> on private mail groups in 1 month more than years on clc.

No comment.

-- 
Kenneth Brody

[toc] | [prev] | [next] | [standalone]


#22446

Frombillcun@suddenlink.net
Date2012-06-20 15:59 -0700
Message-ID<00fdd2d6-aa03-4af6-be62-d3e7091c6463@a16g2000vby.googlegroups.com>
In reply to#22445
On Jun 20, 6:38 pm, Kenneth Brody <kenbr...@spamcop.net> wrote:
> On 6/20/2012 11:17 AM, bill...@suddenlink.net wrote:
>
> > On Jun 20, 4:05 am, Mark Bluemel <mark.blue...@googlemail.com> wrote:
> >> On Jun 19, 4:17 pm, nick_keighley_nos...@hotmail.com wrote:
> [...]
> >> I can't decide whether he's a) simply incapable of learning or b) a
> >> subtle troll pretending to be a), though I'm beginning to lean towards
> >> b). Either way, I don't believe there is much point attempting to
> >> teach him.
>
> >    Things are not *taught* on clc. You do not learn anything.
>
> I have been programming in C for about 29 years, and I've learned things
> about C in this group.  True, most people would probably consider those
> things "obscure", such as why my attempt to #define the name of a header
> file wasn't working:
>
>      Compile with "-DMYHEADER=myheader.h"
>      ...
>      #include <MYHEADER>
>
> because of the way "<MYHEADER>" is parsed.  Instead, I can use this, which
> does work:
>
>      Compile with "-DMYHEADER='<myheader.h>'"
>      ...
>      #include MYHEADER
>
> Then there are techniques that are "obvious" in today's C world, but which
> weren't consistently available in K&R1 days, and so I often use "antiquated"
> means of doing things, simply because that's the way I learned how to do
> them.  Seeing things mentioned here reminds me at times that there are
> better ways of doing things.
>
> (I learned C along with the need to write with multiple platforms in mind.
> In some ways, starting with the need to support both big-endian 32-bit Unix
> systems and little-endian 16-bit MS-DOS systems taught me a lot about
> portability.  I definitely never had the "all the world's a VAX" attitude.)
>
> > It is a C social group.
>
> At times, yes.
>
> > I can count on one hand what I've been *taught* on clc.
>
> Perhaps the problem isn't the teacher?

There is a simple and straight foreward way to put things. Long drawn
out explainations to hobbyists about macro expansion (I know nothing
about) from a PhD's doctoral really don't help. I'm not a computer
scientist.
>
> > If you want to learn something on clc the response is generally...
> > "GIYF" or "I don't do homework."
>
> I rarely see "GIYF".

I see it every other response post. To my postings. Also on another
note I have not seen this told to others but I have been in a hurry
and mispelled simple words that are readable and been mocked and
"quickley dropped from tutoring." As I said before I believe there are
alot of serious programmers on this group computer scientists.
Patience is not common.

And the "I don't do homework" responses are to people
> who don't want to be taught, but rather want to be given the answer

"Given the answer" What does that mean? I don't do homework for
school. I try to learn. I see alot of unreadable code that when shown
to others who are patient. I've been able to grasp.

, often
> to a "problem" that is a typical homework-type assignment.
>
> > You do not learn C here. You talk
> > about it or your experience with C. I've learned more about writing C
> > on private mail groups in 1 month more than years on clc.
>
> No comment.
>
I think everyone's in a hurry to get back to the lab and write
commercial software.

B

[toc] | [prev] | [next] | [standalone]


#22448

From"BartC" <bc@freeuk.com>
Date2012-06-21 00:24 +0100
Message-ID<jrtm4l$i27$1@dont-email.me>
In reply to#22446
<billcun@suddenlink.net> wrote in message 
news:00fdd2d6-aa03-4af6-be62-d3e7091c6463@a16g2000vby.googlegroups.com...

> I see it every other response post. To my postings. Also on another
> note I have not seen this told to others but I have been in a hurry
> and mispelled simple words that are readable and been mocked and
> "quickley dropped from tutoring." As I said before I believe there are
> alot of serious programmers on this group computer scientists.
> Patience is not common.

The problem is you've been asking the same questions repeatedly over a 
decade or so, and never seem to learn.

Up to a point, any answers given are sometimes useful to other readers of 
the group, but you can understand that people can lose patience.

-- 
Bartc 

[toc] | [prev] | [next] | [standalone]


#22450

FromIan Collins <ian-news@hotmail.com>
Date2012-06-21 11:40 +1200
Message-ID<a4f5baF7l4U6@mid.individual.net>
In reply to#22446
On 06/21/12 10:59 AM, billcun@suddenlink.net wrote:
> On Jun 20, 6:38 pm, Kenneth Brody<kenbr...@spamcop.net>  wrote:
>> On 6/20/2012 11:17 AM, bill...@suddenlink.net wrote:
>>
>>> If you want to learn something on clc the response is generally...
>>> "GIYF" or "I don't do homework."
>>
>> I rarely see "GIYF".
>
> I see it every other response post. To my postings.

As others have pointed out, you have been asking the same or similar 
questions here for many years.

Your original question on this thread could easily have been answered 
with a quick search.  Quite often the search results for your questions 
include the thread from the previous time(s) you asked it!

-- 
Ian Collins

[toc] | [prev] | [next] | [standalone]


#22451

FromBill Cunningham <billcun@suddenlink.net>
Date2012-06-20 16:47 -0700
Message-ID<41c3a10f-4e64-4d58-a4d8-7e653a739d7e@l17g2000vbj.googlegroups.com>
In reply to#22450
On Jun 20, 7:40 pm, Ian Collins <ian-n...@hotmail.com> wrote:

> As others have pointed out, you have been asking the same or similar
> questions here for many years.
>
> Your original question on this thread could easily have been answered
> with a quick search.  Quite often the search results for your questions
> include the thread from the previous time(s) you asked it!

 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.

B

[toc] | [prev] | [next] | [standalone]


#22452

FromIan Collins <ian-news@hotmail.com>
Date2012-06-21 11:56 +1200
Message-ID<a4f69dF7l4U7@mid.individual.net>
In reply to#22451
On 06/21/12 11:47 AM, Bill Cunningham wrote:
> On Jun 20, 7:40 pm, Ian Collins<ian-n...@hotmail.com>  wrote:
>
>> As others have pointed out, you have been asking the same or similar
>> questions here for many years.
>>
>> Your original question on this thread could easily have been answered
>> with a quick search.  Quite often the search results for your questions
>> include the thread from the previous time(s) you asked it!
>
>   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/

which has a clear explanation and example.

The first hit for "c fread example" also has a simple example.

I'm sure there are plenty more.

-- 
Ian Collins

[toc] | [prev] | [next] | [standalone]


#22453

FromBill Cunningham <billcun@suddenlink.net>
Date2012-06-20 17:11 -0700
Message-ID<3f284be7-d1a3-44ba-851e-d6ff297f5843@f7g2000yqh.googlegroups.com>
In reply to#22452
On Jun 20, 7:56 pm, Ian Collins <ian-n...@hotmail.com> wrote:
> On 06/21/12 11:47 AM, Bill Cunningham wrote:
>
> > On Jun 20, 7:40 pm, Ian Collins<ian-n...@hotmail.com>  wrote:
>
> >> As others have pointed out, you have been asking the same or similar
> >> questions here for many years.
>
> >> Your original question on this thread could easily have been answered
> >> with a quick search.  Quite often the search results for your questions
> >> include the thread from the previous time(s) you asked it!
>
> >   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/
>
> which has a clear explanation and example.
>
> The first hit for "c fread example" also has a simple example.
>
> I'm sure there are plenty more.
>
<cut example code>

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

int main () {
  FILE * pFile;
  long lSize;
  char * buffer;
  size_t result;

  pFile = fopen ( "myfile.bin" , "rb" );
  if (pFile==NULL) {fputs ("File error",stderr); exit (1);}


  Below is not really clear to me. I haven't got to fseek yet

// obtain file size:
  fseek (pFile , 0 , SEEK_END);
  lSize = ftell (pFile);
  rewind (pFile);

  // allocate memory to contain the whole file:
  buffer = (char*) malloc (sizeof(char)*lSize);

 Why is there a cast above when returns a generic pointer?

  if (buffer == NULL) {fputs ("Memory error",stderr); exit (2);}

  // copy the file into the buffer:
  result = fread (buffer,1,lSize,pFile);
  if (result != lSize) {fputs ("Reading error",stderr); exit (3);}

  /* the whole file is now loaded in the memory buffer. */

  // terminate
  fclose (pFile);
  free (buffer);
  return 0;
}

I only googled "man fread" and expected an example there.
I wanted and example with fwrite too.

[toc] | [prev] | [next] | [standalone]


#22454

FromIan Collins <ian-news@hotmail.com>
Date2012-06-21 12:28 +1200
Message-ID<a4f84sF7l4U8@mid.individual.net>
In reply to#22453
On 06/21/12 12:11 PM, Bill Cunningham wrote:
> On Jun 20, 7:56 pm, Ian Collins<ian-n...@hotmail.com>  wrote:
>> On 06/21/12 11:47 AM, Bill Cunningham wrote:
>>
>>> On Jun 20, 7:40 pm, Ian Collins<ian-n...@hotmail.com>    wrote:
>>
>>>> As others have pointed out, you have been asking the same or similar
>>>> questions here for many years.
>>
>>>> Your original question on this thread could easily have been answered
>>>> with a quick search.  Quite often the search results for your questions
>>>> include the thread from the previous time(s) you asked it!
>>
>>>    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/
>>
>> which has a clear explanation and example.
>>
>> The first hit for "c fread example" also has a simple example.
>>
>> I'm sure there are plenty more.
>>
> <cut example code>
>
> #include<stdio.h>
> #include<stdlib.h>
>
> int main () {
>    FILE * pFile;
>    long lSize;
>    char * buffer;
>    size_t result;
>
>    pFile = fopen ( "myfile.bin" , "rb" );
>    if (pFile==NULL) {fputs ("File error",stderr); exit (1);}
>
>
>    Below is not really clear to me. I haven't got to fseek yet
>
> // obtain file size:
>    fseek (pFile , 0 , SEEK_END);
>    lSize = ftell (pFile);
>    rewind (pFile);
>
>    // allocate memory to contain the whole file:
>    buffer = (char*) malloc (sizeof(char)*lSize);
>
>   Why is there a cast above when returns a generic pointer?

So it will compile as C++ (note the name of the site!).

>    if (buffer == NULL) {fputs ("Memory error",stderr); exit (2);}
>
>    // copy the file into the buffer:
>    result = fread (buffer,1,lSize,pFile);
>    if (result != lSize) {fputs ("Reading error",stderr); exit (3);}
>
>    /* the whole file is now loaded in the memory buffer. */
>
>    // terminate
>    fclose (pFile);
>    free (buffer);
>    return 0;
> }
>
> I only googled "man fread" and expected an example there.
> I wanted and example with fwrite too.

Try improving your google searches.

-- 
Ian Collins

[toc] | [prev] | [next] | [standalone]


#22456

FromBill Cunningham <billcun@suddenlink.net>
Date2012-06-20 17:51 -0700
Message-ID<c2a772ca-e20f-4d2d-a358-6e782d33386a@f7g2000yqh.googlegroups.com>
In reply to#22454
On Jun 20, 8:28 pm, Ian Collins <ian-n...@hotmail.com> wrote:
> On 06/21/12 12:11 PM, Bill Cunningham wrote:
>
>
>
>
>
> > On Jun 20, 7:56 pm, Ian Collins<ian-n...@hotmail.com>  wrote:
> >> On 06/21/12 11:47 AM, Bill Cunningham wrote:
>
> >>> On Jun 20, 7:40 pm, Ian Collins<ian-n...@hotmail.com>    wrote:
>
> >>>> As others have pointed out, you have been asking the same or similar
> >>>> questions here for many years.
>
> >>>> Your original question on this thread could easily have been answered
> >>>> with a quick search.  Quite often the search results for your questions
> >>>> include the thread from the previous time(s) you asked it!
>
> >>>    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/
>
> >> which has a clear explanation and example.
>
> >> The first hit for "c fread example" also has a simple example.
>
> >> I'm sure there are plenty more.
>
> > <cut example code>
>
> > #include<stdio.h>
> > #include<stdlib.h>
>
> > int main () {
> >    FILE * pFile;
> >    long lSize;
> >    char * buffer;
> >    size_t result;
>
> >    pFile = fopen ( "myfile.bin" , "rb" );
> >    if (pFile==NULL) {fputs ("File error",stderr); exit (1);}
>
> >    Below is not really clear to me. I haven't got to fseek yet
>
> > // obtain file size:
> >    fseek (pFile , 0 , SEEK_END);
> >    lSize = ftell (pFile);
> >    rewind (pFile);
>
> >    // allocate memory to contain the whole file:
> >    buffer = (char*) malloc (sizeof(char)*lSize);
>
> >   Why is there a cast above when returns a generic pointer?
>
> So it will compile as C++ (note the name of the site!).

Ok well I don't know anything about that. (C++)

> >    if (buffer == NULL) {fputs ("Memory error",stderr); exit (2);}
>
> >    // copy the file into the buffer:
> >    result = fread (buffer,1,lSize,pFile);
> >    if (result != lSize) {fputs ("Reading error",stderr); exit (3);}
>
> >    /* the whole file is now loaded in the memory buffer. */
>
> >    // terminate
> >    fclose (pFile);
> >    free (buffer);
> >    return 0;
> > }
>
> > I only googled "man fread" and expected an example there.
> > I wanted and example with fwrite too.
>
> Try improving your google searches.
>
> --
> Ian Collins- Hide quoted text -
>
> - Show quoted text -- Hide quoted text -
>
> - Show quoted text -

[toc] | [prev] | [next] | [standalone]


#22490

FromKenneth Brody <kenbrody@spamcop.net>
Date2012-06-21 15:05 -0400
Message-ID<-9mdnR3ccJsQ7X7SnZ2dnUVZ_h2dnZ2d@bestweb.net>
In reply to#22452
On 6/20/2012 7:56 PM, Ian Collins wrote:
> 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".  :-)

> which has a clear explanation and example.
>
> The first hit for "c fread example" also has a simple example.
>
> I'm sure there are plenty more.


-- 
Kenneth Brody

[toc] | [prev] | [next] | [standalone]


#22491

FromIan Collins <ian-news@hotmail.com>
Date2012-06-22 07:17 +1200
Message-ID<a4haajF7l4U10@mid.individual.net>
In reply to#22490
On 06/22/12 07:05 AM, Kenneth Brody wrote:
> On 6/20/2012 7:56 PM, Ian Collins wrote:
>> 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!

-- 
Ian Collins

[toc] | [prev] | [next] | [standalone]


#22494

FromJames Kuyper <jameskuyper@verizon.net>
Date2012-06-21 16:01 -0400
Message-ID<4FE37D85.1070401@verizon.net>
In reply to#22491
On 06/21/2012 03:17 PM, Ian Collins wrote:
> On 06/22/12 07:05 AM, Kenneth Brody wrote:
>> On 6/20/2012 7:56 PM, Ian Collins wrote:
>>> 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++.

[toc] | [prev] | [next] | [standalone]


#22505

FromKeith Thompson <kst-u@mib.org>
Date2012-06-21 15:08 -0700
Message-ID<lnvcik8gif.fsf@nuthaus.mib.org>
In reply to#22494
James Kuyper <jameskuyper@verizon.net> writes:
[...]
> 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.

-- 
Keith Thompson (The_Other_Keith) kst-u@mib.org  <http://www.ghoti.net/~kst>
    Will write code for food.
"We must do something.  This is something.  Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"

[toc] | [prev] | [next] | [standalone]


#22508

FromJames Kuyper <jameskuyper@verizon.net>
Date2012-06-21 18:36 -0400
Message-ID<4FE3A1F2.8010201@verizon.net>
In reply to#22505
On 06/21/2012 06:08 PM, Keith Thompson wrote:
> James Kuyper <jameskuyper@verizon.net> writes:
> [...]
>> 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.

[toc] | [prev] | [next] | [standalone]


#22511

FromKeith Thompson <kst-u@mib.org>
Date2012-06-21 16:49 -0700
Message-ID<lnmx3w8bv7.fsf@nuthaus.mib.org>
In reply to#22508
James Kuyper <jameskuyper@verizon.net> writes:
[...]
> 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.

-- 
Keith Thompson (The_Other_Keith) kst-u@mib.org  <http://www.ghoti.net/~kst>
    Will write code for food.
"We must do something.  This is something.  Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"

[toc] | [prev] | [next] | [standalone]


#22455

FromBen Bacarisse <ben.usenet@bsb.me.uk>
Date2012-06-21 01:30 +0100
Message-ID<0.ce4076a59796ffb7a984.20120621013009BST.87wr31pkvi.fsf@bsb.me.uk>
In reply to#22451
Bill Cunningham <billcun@suddenlink.net> writes:

> On Jun 20, 7:40 pm, Ian Collins <ian-n...@hotmail.com> wrote:
>
>> As others have pointed out, you have been asking the same or similar
>> questions here for many years.
>>
>> Your original question on this thread could easily have been answered
>> with a quick search.  Quite often the search results for your questions
>> include the thread from the previous time(s) you asked it!
>
>  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>

-- 
Ben.

[toc] | [prev] | [next] | [standalone]


#22457

FromBill Cunningham <billcun@suddenlink.net>
Date2012-06-20 17:51 -0700
Message-ID<a4faea68-3cdf-4755-be8e-fe827fdf618b@t8g2000yqd.googlegroups.com>
In reply to#22455
On Jun 20, 8:30 pm, Ben Bacarisse <ben.use...@bsb.me.uk> wrote:
> Bill Cunningham <bill...@suddenlink.net> writes:
> > On Jun 20, 7:40 pm, Ian Collins <ian-n...@hotmail.com> wrote:
>
> >> As others have pointed out, you have been asking the same or similar
> >> questions here for many years.
>
> >> Your original question on this thread could easily have been answered
> >> with a quick search.  Quite often the search results for your questions
> >> include the thread from the previous time(s) you asked it!
>
> >  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-dee4a63d1...@f63g2000hsf.googlegroups.com>

 OK I will cut and paste and study this. I was thinking of a do{}
while() approach.

[toc] | [prev] | [next] | [standalone]


#22459

FromBen Bacarisse <ben.usenet@bsb.me.uk>
Date2012-06-21 02:22 +0100
Message-ID<0.5f8846ecaeb65ba95cac.20120621022256BST.87k3z1pifj.fsf@bsb.me.uk>
In reply to#22457
Bill Cunningham <billcun@suddenlink.net> writes:

> On Jun 20, 8:30 pm, Ben Bacarisse <ben.use...@bsb.me.uk> wrote:
<snip>
>> Here's an example (not my code):
<snip example>
>> 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-dee4a63d1...@f63g2000hsf.googlegroups.com>
>
>  OK I will cut and paste and study this. I was thinking of a do{}
> while() approach.

Well that's progress.  Nick Keighley re-wrote the example to get rind of
a do ... while() because it confused you four years ago.

-- 
Ben.

[toc] | [prev] | [next] | [standalone]


#22518

Fromnick_keighley_nospam@hotmail.com
Date2012-06-22 00:13 -0700
Message-ID<b9dd1209-69e6-45ae-b5b8-e820e74ac4cd@googlegroups.com>
In reply to#22455
On Thursday, June 21, 2012 1:30:09 AM UTC+1, Ben Bacarisse wrote:
> Bill Cunningham <billcun@suddenlink.net> writes:
> 
> > On Jun 20, 7:40 pm, Ian Collins <ian-n...@hotmail.com> wrote:
> >
> >> As others have pointed out, you have been asking the same or similar
> >> questions here for many years.
> >>
> >> Your original question on this thread could easily have been answered
> >> with a quick search.  Quite often the search results for your questions
> >> include the thread from the previous time(s) you asked it!
> >
> >  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.

[toc] | [prev] | [next] | [standalone]


Page 2 of 3 — ← Prev page 1 [2] 3  Next page →

Back to top | Article view | comp.lang.c


csiph-web