C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

fread/fwrite
Goto page Previous  1, 2, 3, 4  Next
 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C Language
View previous topic :: View next topic  
Author Message
Ben Bacarisse
Guest





PostPosted: Wed Jun 20, 2012 11:22 pm    Post subject: Re: fread/fwrite Reply with quote



Bill Cunningham <billcun (AT) suddenlink (DOT) net> writes:

Quote:
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.
Back to top
Bill Cunningham
Guest





PostPosted: Wed Jun 20, 2012 11:27 pm    Post subject: Re: fread/fwrite Reply with quote



On Jun 20, 6:38 pm, Kenneth Brody <kenbr...@spamcop.net> wrote:
Quote:
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

I think a typical ask to me for a question like that is "see
comp.compilers..." With an impression to me like "I gotta go...The lab
is waiting..."

Quote:
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.

As for techniques that are obvious in the C world I don't know because
I am not in the C world. Nor am I a computer scientist or doing
homework to become one.

Quote:
(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
Back to top
Bill Cunningham
Guest





PostPosted: Wed Jun 20, 2012 11:47 pm    Post subject: Re: fread/fwrite Reply with quote



On Jun 20, 7:40 pm, Ian Collins <ian-n...@hotmail.com> wrote:

Quote:
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
Back to top
Bill Cunningham
Guest





PostPosted: Thu Jun 21, 2012 12:11 am    Post subject: Re: fread/fwrite Reply with quote

On Jun 20, 7:56 pm, Ian Collins <ian-n...@hotmail.com> wrote:
Quote:
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.
Back to top
Bill Cunningham
Guest





PostPosted: Thu Jun 21, 2012 12:51 am    Post subject: Re: fread/fwrite Reply with quote

On Jun 20, 8:30 pm, Ben Bacarisse <ben.use...@bsb.me.uk> wrote:
Quote:
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.
Back to top
Bill Cunningham
Guest





PostPosted: Thu Jun 21, 2012 12:51 am    Post subject: Re: fread/fwrite Reply with quote

On Jun 20, 8:28 pm, Ian Collins <ian-n...@hotmail.com> wrote:
Quote:
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++)

Quote:
   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 -
Back to top
Guest






PostPosted: Thu Jun 21, 2012 7:17 am    Post subject: Re: fread/fwrite Reply with quote

On Wednesday, June 20, 2012 4:13:46 PM UTC+1, Bill Cunningham wrote:
Quote:
On Jun 20, 3:05 am, nick_keighley_nos...@hotmail.com wrote:
On Tuesday, June 19, 2012 5:51:55 PM UTC+1, (unknown) wrote:

<snip>

Quote:
Sorry. Typo. I meant array[0]
[was a new concept]


Quote:
in what sense is array[0] "new"? You must have seen an array indexed before!

Oh Ok yes of course. Yes. It was mentioned this was any positive
number.

*what* was any positive number? is english your first language? Is zero a positive number in your universe?

<snip>
Back to top
Guest






PostPosted: Thu Jun 21, 2012 7:21 am    Post subject: Re: fread/fwrite Reply with quote

On Wednesday, June 20, 2012 4:17:28 PM UTC+1, Bill Cunningham wrote:
Quote:
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:

did you read what he wrote?

Why would you expect Bill to do that? He's been around this group long
enough for any regular reader to have some idea of his modus operandi.

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.

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

It is a C
Quote:
social group. I can count on one hand what I've been *taught* on clc.

that's because you're a cretin

Quote:
If you want to learn something on clc the response is generally...
"GIYF" or "I don't do homework." 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.

I don't believe you've learnt anything
Back to top
Guest






PostPosted: Thu Jun 21, 2012 7:33 am    Post subject: Re: fread/fwrite Reply with quote

On Wednesday, June 20, 2012 11:59:38 PM UTC+1, Bill Cunningham wrote:
Quote:
On Jun 20, 6:38 pm, Kenneth Brody <kenbr...@spamcop.net> wrote:
On 6/20/2012 11:17 AM, bill...@suddenlink.net wrote:

<snip>

Quote:
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.

seems like tails we lose heads you win. If we give short answers we're impatient and long answers we're showing off our PhDs.

Quote:
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.

that's because you ask very basic questions that would be better answered by reading a basic tutorial. For fuck's sake you failed to recognise a simple array access in this very thread!

Quote:
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."

I don't mock people for mispellings and typos (there's a good reason for that). But to answer question you have to understand it. And you mess up terminology, ask questions you didn't mean to ask (see this thread) and generally play fast and loose with the laws of english language and basic logic.

programmer is disiplined thought. you appear to unable to think or to organise yourself.

Quote:
As I said before I believe there are
alot of serious programmers on this group computer scientists.

what language is *that* in.

Quote:
Patience is not common.

you are shown far too much patience

at one time I had a list of responses for you. Number one was "work out what you are trying to do". you nearly always fail on that one. So why are you posting here? For fun?

<snip>
Back to top
James Kuyper
Guest





PostPosted: Thu Jun 21, 2012 9:45 am    Post subject: Re: fread/fwrite Reply with quote

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 (AT) suddenlink (DOT) net wrote:
Quote:
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.

Quote:
... 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.

Quote:
... 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
Back to top
tom st denis
Guest





PostPosted: Thu Jun 21, 2012 11:22 am    Post subject: Re: fread/fwrite Reply with quote

On Jun 20, 7:47 pm, Bill Cunningham <bill...@suddenlink.net> wrote:
Quote:
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.

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
Back to top
Mark Bluemel
Guest





PostPosted: Thu Jun 21, 2012 2:17 pm    Post subject: Re: fread/fwrite Reply with quote

On Jun 21, 8:21 am, nick_keighley_nos...@hotmail.com wrote:
Quote:
On Wednesday, June 20, 2012 4:17:28 PM UTC+1, Bill Cunningham 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:
did you read what he wrote?

Why would you expect Bill to do that? He's been around this group long
enough for any regular reader to have some idea of his modus operandi..

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.

*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)...

Quote:

 It is a C
social group. I can count on one hand what I've been *taught* on clc.

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.

Quote:
If you want to learn something on clc the response is generally...
"GIYF" or "I don't do homework." 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.

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.
Back to top
Kenneth Brody
Guest





PostPosted: Thu Jun 21, 2012 5:05 pm    Post subject: Re: fread/fwrite Reply with quote

On 6/20/2012 7:56 PM, Ian Collins wrote:
Quote:
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". :-)

Quote:
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
Back to top
Ian Collins
Guest





PostPosted: Thu Jun 21, 2012 5:17 pm    Post subject: Re: fread/fwrite Reply with quote

On 06/22/12 07:05 AM, Kenneth Brody wrote:
Quote:
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". Smile

Except where it is discussing the shared standard library!

--
Ian Collins
Back to top
James Kuyper
Guest





PostPosted: Thu Jun 21, 2012 6:01 pm    Post subject: Re: fread/fwrite Reply with quote

On 06/21/2012 03:17 PM, Ian Collins wrote:
Quote:
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++.
Back to top
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C Language All times are GMT
Goto page Previous  1, 2, 3, 4  Next
Page 3 of 4

 
 


Powered by phpBB © 2001, 2006 phpBB Group