469,088 Members | 1,273 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,088 developers. It's quick & easy.

success! I think

I began to think about some excercises I could with files after reading
turtorials today on arithmetic operators and confusing myself and decided to
do this.
Take the mv command from linux that moves and renames files read it as
binary and write it as text. After some compiler qipes it compiled and I
believe it did want I wanted it to.

#include <stdio.h>

int main(void) {
FILE *ifp, *ofp;
int a;
ifp=fopen("mv","rb");
ofp=fopen("m","wt");
a=fgetc(ifp);
if (a==EOF) {
fclose(ifp);
}
fputc(a,ofp);
if (a==EOF) {
fclose(ofp);
}
}

Because I didn't know how long the file would be I couldn't say exactly
how many bytes I should read so fread/fwrite were out and I decided on
fgetc/fputc. Is my code up to standards? And also can someone tell me what
the first int parameter of fputc is and does? My references that I look at
do not say. But I think I used it right. I guessed a 'container' for a char.

Bill
Jun 27 '08 #1
30 1137

"Bill Cunningham" <no****@nspam.comwrote in message
news:V1aMj.3202$Ho5.2125@trnddc01...
I began to think about some excercises I could with files after reading
turtorials today on arithmetic operators and confusing myself and decided
to do this.
Take the mv command from linux that moves and renames files read it as
binary and write it as text. After some compiler qipes it compiled and I
believe it did want I wanted it to.

#include <stdio.h>

int main(void) {
FILE *ifp, *ofp;
int a;
ifp=fopen("mv","rb");
ofp=fopen("m","wt");
a=fgetc(ifp);
if (a==EOF) {
fclose(ifp);
}
fputc(a,ofp);
if (a==EOF) {
fclose(ofp);
}
}

Because I didn't know how long the file would be I couldn't say exactly
how many bytes I should read so fread/fwrite were out and I decided on
fgetc/fputc. Is my code up to standards?
Not quite.

Try putting in a comment describing what it should do.

Try testing it to see that it does so!

--
Bart
Jun 27 '08 #2

"Bartc" <bc@freeuk.comwrote in message
news:1e****************@text.news.virginmedia.com. ..
Not quite.

Try putting in a comment describing what it should do.

Try testing it to see that it does so!
Well as far as testing it an EBCDIC file was written with characters so
it seems to work. But when I changed the ofp pointer and fopen's mode to wb
I got the same thing. So now I am doubting that I am getting what I want
though it seemed work.

Bill
Jun 27 '08 #3
"Bill Cunningham" <no****@nspam.comwrites:
"Bartc" <bc@freeuk.comwrote in message
news:1e****************@text.news.virginmedia.com. ..
>Not quite.

Try putting in a comment describing what it should do.

Try testing it to see that it does so!
Well as far as testing it an EBCDIC file was written with characters so
it seems to work. But when I changed the ofp pointer and fopen's mode to wb
I got the same thing. So now I am doubting that I am getting what I want
though it seemed work.

Bill
You are trolling aren't you?
Jun 27 '08 #4

"Richard" <de***@gmail.comwrote in message
news:ft**********@registered.motzarella.org...
You are trolling aren't you?
Where the hell did that come from? I am doubting that my code is doing
what I wanted and I would like to ask for serious feedback.

Bill
Jun 27 '08 #5
"Bill Cunningham" <no****@nspam.comwrites:
"Richard" <de***@gmail.comwrote in message
news:ft**********@registered.motzarella.org...
>You are trolling aren't you?

Where the hell did that come from? I am doubting that my code is doing
what I wanted and I would like to ask for serious feedback.

Bill
Here's some : step through it with a debugger. That will be specific to
your platform properly. Even a casual look at your code should tell you
it doesn't do what you want. Stepping through with a debugger will prove
invaluable since you clearly are a beginner in all aspects of
programming. Rather than take you through all the issues with your code,
try to figure it out yourself first. Go line by line and ask yourself
what it is doing and, as importantly, why.
Jun 27 '08 #6
On Sat, 12 Apr 2008 21:43:17 GMT, "Bill Cunningham" <no****@nspam.com>
wrote:
I began to think about some excercises I could with files after reading
turtorials today on arithmetic operators and confusing myself and decided to
do this.
Take the mv command from linux that moves and renames files read it as
binary and write it as text. After some compiler qipes it compiled and I
believe it did want I wanted it to.

#include <stdio.h>

int main(void) {
FILE *ifp, *ofp;
int a;
ifp=fopen("mv","rb");
ofp=fopen("m","wt");
a=fgetc(ifp);
How do you ever get back to read another character.
if (a==EOF) {
fclose(ifp);
If you would learn to indent your code, it might make it much easier
for you to see where you are going wrong.
}
fputc(a,ofp);
if (a==EOF) {
fclose(ofp);
}
}

Because I didn't know how long the file would be I couldn't say exactly
how many bytes I should read so fread/fwrite were out and I decided on
Your comment makes no sense. Both fread and fwrite have methods for
specifying how many bytes to read/write and for informing the caller
how many bytes were actually read/written. You don't have to use them
but to say you can't is a limitation on you and not the functions.
>fgetc/fputc. Is my code up to standards? And also can someone tell me what
the first int parameter of fputc is and does? My references that I look at
do not say. But I think I used it right. I guessed a 'container' for a char.
You need a better reference. Find one that describes the purpose of
each parameter of all the standard functions. You can even find free
copies of the draft standard (n1124 and n1256) on the web which will
do this.
Remove del for email
Jun 27 '08 #7
On Sat, 12 Apr 2008 22:23:03 GMT, "Bill Cunningham" <no****@nspam.com>
wrote:
>
"Bartc" <bc@freeuk.comwrote in message
news:1e****************@text.news.virginmedia.com ...
>Not quite.

Try putting in a comment describing what it should do.

Try testing it to see that it does so!
Well as far as testing it an EBCDIC file was written with characters so
it seems to work. But when I changed the ofp pointer and fopen's mode to wb
I got the same thing. So now I am doubting that I am getting what I want
though it seemed work.
Only for some strange definition of work. Your function doesn't
contain any form of looping. It executes fputc and fgetc each exactly
once. How can your function read or write more than one character?
Remove del for email
Jun 27 '08 #8
Only for some strange definition of work. Your function doesn't
contain any form of looping. It executes fputc and fgetc each exactly
once. How can your function read or write more than one character?
I'm not exactly sure how to use fgetc and fputc. This is the first time
I've put them into code so I'm a little confused on where in the loops to
put them. I've never really used debuggers though I have gdb. I don't think
it takes more than a keaner eye than mine to see the problems though.

Bill
Jun 27 '08 #9

"Barry Schwarz" <sc******@dqel.comwrote in message
news:j3********************************@4ax.com...
> Because I didn't know how long the file would be I couldn't say
exactly
how many bytes I should read so fread/fwrite were out and I decided on

Your comment makes no sense. Both fread and fwrite have methods for
specifying how many bytes to read/write and for informing the caller
how many bytes were actually read/written. You don't have to use them
but to say you can't is a limitation on you and not the functions.
Exactly. Fread and fwrite require a buffer size and number of elements.
I just wanted to read the entire file without having to enter an exact size
so I don't think fwrite and fread would be the choice here.

Bill
Jun 27 '08 #10

"Bill Cunningham" <no****@nspam.comwrote in message
news:gtvMj.4699$bx3.1392@trnddc02...
>
"Barry Schwarz" <sc******@dqel.comwrote in message
news:j3********************************@4ax.com...
>> Because I didn't know how long the file would be I couldn't say
exactly
how many bytes I should read so fread/fwrite were out and I decided on

Your comment makes no sense. Both fread and fwrite have methods for
specifying how many bytes to read/write and for informing the caller
how many bytes were actually read/written. You don't have to use them
but to say you can't is a limitation on you and not the functions.

Exactly. Fread and fwrite require a buffer size and number of elements.
I just wanted to read the entire file without having to enter an exact
size so I don't think fwrite and fread would be the choice here.
In your original post you gave the impression the code worked and you wanted
comments on the coding standard.

Clearly you hadn't tested the code, unless you tested with a file containing
a single character.

The code below does a file copy byte by byte using fgetc/fputc, using binary
mode, of any type of file.

If you want to translate between text files, mixing binary and text modes on
input and output, that might give some strange effects.

If you want to translate a binary executable file as you indicated in your
post, into text, that is fairly meaningless unless you're talking about some
sort of dump or disassembly, but in that case I'd advise to forget that for
now.

/* Copy input file to output file */
#include <stdio.h>
#include <stdlib.h>

#define inputfile "mv"
#define outputfile "m"

int main(void) {

FILE *ifp, *ofp;
int a;

ifp=fopen(inputfile,"rb");

if (ifp==0){
printf("Can't open input file %s\n",inputfile);
exit (0);
}

ofp=fopen(outputfile,"wb");
if (ofp==0){
printf("Can't open output file %s\n",outputfile);
fclose(ifp);
exit (0);
}

while ((a=fgetc(ifp))!=EOF)
fputc(a,ofp);

fclose(ifp);
fclose(ofp);

}

--
Bart
Jun 27 '08 #11

"Bartc" <bc@freeuk.comwrote in message
news:Sh*****************@text.news.virginmedia.com ...
>
"Bill Cunningham" <no****@nspam.comwrote in message
news:gtvMj.4699$bx3.1392@trnddc02...
>>
"Barry Schwarz" <sc******@dqel.comwrote in message
news:j3********************************@4ax.com.. .
>>> Because I didn't know how long the file would be I couldn't say
exactly
how many bytes I should read so fread/fwrite were out and I decided on

Your comment makes no sense. Both fread and fwrite have methods for
specifying how many bytes to read/write and for informing the caller
how many bytes were actually read/written. You don't have to use them
but to say you can't is a limitation on you and not the functions.

Exactly. Fread and fwrite require a buffer size and number of
elements. I just wanted to read the entire file without having to enter
an exact size so I don't think fwrite and fread would be the choice here.

In your original post you gave the impression the code worked and you
wanted comments on the coding standard.

Clearly you hadn't tested the code, unless you tested with a file
containing a single character.
That's what I must've done.
The code below does a file copy byte by byte using fgetc/fputc, using
binary mode, of any type of file.

If you want to translate between text files, mixing binary and text modes
on input and output, that might give some strange effects.

If you want to translate a binary executable file as you indicated in your
post, into text, that is fairly meaningless unless you're talking about
some sort of dump or disassembly, but in that case I'd advise to forget
that for now.
OK
/* Copy input file to output file */
#include <stdio.h>
#include <stdlib.h>

#define inputfile "mv"
#define outputfile "m"

int main(void) {

FILE *ifp, *ofp;
int a;

ifp=fopen(inputfile,"rb");

if (ifp==0){
printf("Can't open input file %s\n",inputfile);
exit (0);
}

ofp=fopen(outputfile,"wb");
if (ofp==0){
printf("Can't open output file %s\n",outputfile);
fclose(ifp);
exit (0);
}

while ((a=fgetc(ifp))!=EOF)
fputc(a,ofp);

fclose(ifp);
fclose(ofp);

}
I appreciate your patience. When I first looked at C for some reason I
became transfixed. I know Basic pretty well but I would like to learn C very
much. Some times I sound confused because much of the time I am. I have
lived in the same town all my life and I pretty much gave up for the most
part on driving because I would start out somewhere and end up on the
opposite side of town. This is the confusion I have to deal with with drugs
that stop my panic attacks.

So I'm not just going to catch onto a language like C. But I will
continue to try but I think I may have a hard road to hoe. Thanks again.

Bill
Jun 27 '08 #12
Bill Cunningham <no****@nspam.comwrote:
>
Exactly. Fread and fwrite require a buffer size and number of elements.
I just wanted to read the entire file without having to enter an exact size
so I don't think fwrite and fread would be the choice here.
That's because you're not thinking enough. If you just want to read
bytes, use an element size of 1 and make the number of elements the
length of your buffer. fread() returns the number of elements it
actually read, which is what you need to pass to fwrite() to write the
chunk of data that you just read. Just do that in a loop to read and
writes successive chunks of the file until there's nothing left to read
and you're done.

-Larry Jones

I think grown-ups just ACT like they know what they're doing. -- Calvin
Jun 27 '08 #13

<la************@siemens.comwrote in message
news:gp************@jones.homeip.net...
That's because you're not thinking enough. If you just want to read
bytes, use an element size of 1 and make the number of elements the
length of your buffer. fread() returns the number of elements it
actually read, which is what you need to pass to fwrite() to write the
chunk of data that you just read. Just do that in a loop to read and
writes successive chunks of the file until there's nothing left to read
and you're done.
Hum. There is sizeof. This might just work.

fread(buff,sizeof(int),1,fp);

I could try that. I just bet it would work to.

Bill
Jun 27 '08 #14
On Mon, 14 Apr 2008 01:41:05 GMT, "Bill Cunningham" <no****@nspam.com>
wrote:
>
<la************@siemens.comwrote in message
news:gp************@jones.homeip.net...
>That's because you're not thinking enough. If you just want to read
bytes, use an element size of 1 and make the number of elements the
length of your buffer. fread() returns the number of elements it
actually read, which is what you need to pass to fwrite() to write the
chunk of data that you just read. Just do that in a loop to read and
writes successive chunks of the file until there's nothing left to read
and you're done.
Hum. There is sizeof. This might just work.

fread(buff,sizeof(int),1,fp);

I could try that. I just bet it would work to.
What makes you think the size of an integer has any significance to
the code you presented?
Remove del for email
Jun 27 '08 #15
On 12 Apr, 22:43, "Bill Cunningham" <nos...@nspam.comwrote:
* * I began to think about some excercises I could with files after reading
turtorials today on arithmetic operators and confusing myself and decided to
do this.
* * Take the mv command from linux that moves and renames files read it as
binary and write it as text. After some compiler qipes it compiled and I
believe it did want I wanted it to.

#include <stdio.h>

int main(void) {
*FILE *ifp, *ofp;
*int a;
*ifp=fopen("mv","rb");
*ofp=fopen("m","wt");
*a=fgetc(ifp);
*if (a==EOF) {
*fclose(ifp);
*}
*fputc(a,ofp);
*if (a==EOF) {
*fclose(ofp);
*}
* *}

* * Because I didn't know how long the file would be I couldn't say exactly
how many bytes I should read so fread/fwrite were out and I decided on
fgetc/fputc. Is my code up to standards? And also can someone tell me what
the first int parameter of fputc is and does? My references that I look at
do not say. But I think I used it right. I guessed a 'container' for a char.
you posted an almost identical problem in february. I gave
you a general way to copy a file. use google "keighley cunningham"
to find it.

Either learn this stuff or give up learning C.
--
Nick Keighley
Jun 27 '08 #16
"Bill Cunningham" <no****@nspam.comwrites:
<la************@siemens.comwrote in message
news:gp************@jones.homeip.net...
>That's because you're not thinking enough. If you just want to read
bytes, use an element size of 1 and make the number of elements the
length of your buffer. fread() returns the number of elements it
actually read, which is what you need to pass to fwrite() to write the
chunk of data that you just read. Just do that in a loop to read and
writes successive chunks of the file until there's nothing left to read
and you're done.
Hum. There is sizeof. This might just work.

fread(buff,sizeof(int),1,fp);

I could try that. I just bet it would work to.

Bill
Learn to read a man page or a C book or, and I hate to say this, give
up. Personally I think you are trolling.

But if you're not, read your program as a cpu would generally do it -
line by line and statement by statement. Your code and your thinking
seem to bear almost no resemblance to the problem you want to solve.
Jun 27 '08 #17


you posted an almost identical problem in february. I gave
you a general way to copy a file. use google "keighley cunningham"
to find it.

Either learn this stuff or give up learning C.

Nick I still have a copy of the file you posted and I am going to keep
it. I think I need to learn more about loops like while. You used an example
of do. Your code was very thorough and precise. I haven't got to do in the
tutorials but I will continue reading and studing your code. Don't think
your post was in vain.

Bill
--
Nick Keighley
Jun 27 '08 #18
What makes you think the size of an integer has any significance to
the code you presented?
I was addressing Lawrence's post concerning fread. It really didn't have
much to do with using fgetc and fputc or the first code I posted.

Bill
Jun 27 '08 #19
On 14 Apr, 22:32, "Bill Cunningham" <nos...@nspam.comwrote:
you posted an almost identical problem in february. I gave
you a general way to copy a file. use google "keighley cunningham"
to find it.

Either learn this stuff or give up learning C.

* * Nick I still have a copy of the file you posted and I am going to keep
it. I think I need to learn more about loops like while. You used an example
of do. Your code was very thorough and precise. I haven't got to do in the
tutorials but I will continue reading and studing your code. Don't think
your post was in vain.
your current problem seems almost identical to one I
gave a solution for. The only difference is the previous one
used fread/fwrite. I cannot comprehend how you can read my code
and then post code that tries to copy a file ***without using a
loop construct***
--
Nick Keighley

Jun 27 '08 #20

your current problem seems almost identical to one I
gave a solution for. The only difference is the previous one
used fread/fwrite. I cannot comprehend how you can read my code

That's it right there. The ch you used I don't see all of what's going
on in your code. Basically I can't read it all.
Especially down around do.

and then post code that tries to copy a file ***without using a
loop construct***

***By Nick K.***

#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);
}

do
{
if ((ch_read = fread (buf, 1, sizeof(buf), fp_in)) !=
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);
}
}
while (ch_read == sizeof(buf));

fclose (fp_in);
fclose (fp_out);

return 0;
}
----
do
{
if ((ch_read = fread (buf, 1, sizeof(buf), fp_in)) !=
sizeof(buf)
&& ferror(fp_in))
{

Here's where I get lost.

Bill

Jun 27 '08 #21
Bill Cunningham wrote:
That's it right there. The ch you used I don't see all of what's
going on in your code. Basically I can't read it all.
Especially down around do.
do
{
if ((ch_read = fread (buf, 1, sizeof(buf), fp_in)) !=
sizeof(buf)
&& ferror(fp_in))
{

Here's where I get lost.
Same here. Most of this is error-checking code that is not so important when
learning.

Without that error code, the main loop comes down to this, where I've
substituted N for sizeof(BUF):

do
{ ch_read = fread (buf, 1, N, fp_in);
fwrite (buf, 1, ch_read, fp_out);
}
while (ch_read == N);

This will read and write blocks of N characters, until either 0 characters
have been read, indicating end-of-file, or 1..N-1 characters have been read,
indicating this was the last (partial) block.

-- Bartc

Jun 27 '08 #22
Bartc said:

<snip>
Most of this is error-checking code that is not so important
when learning.
I can't agree with that. If you postpone learning about error-checking
until you've learned everything else, you'll end up never learning about
it at all - and this view is borne out by a huge amount of supposedly
professional code out there in the real world that completely ignores the
idea that anything might ever not quite work out the way the programmer
wants it to.

--
Richard Heathfield <http://www.cpax.org.uk>
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
Jun 27 '08 #23
On 16 Apr, 12:00, "Bartc" <b...@freeuk.comwrote:
Bill Cunningham wrote:
* *That's it right there. The ch you used I don't see all of what's
going on in your code. Basically I can't read it all.
Especially down around do.
do
* *{
* * * *if ((ch_read = fread (buf, 1, sizeof(buf), fp_in)) !=
sizeof(buf)
* * * * * * && *ferror(fp_in))
* * * *{
Here's where I get lost.

Same here. Most of this is error-checking code that is not so important when
learning.
eek!

Without that error code, the main loop comes down to this, where I've
substituted N for sizeof(BUF):

* * do
* * { * ch_read = fread (buf, 1, N, fp_in);
* * * * fwrite (buf, 1, ch_read, fp_out);
* * }
* * while (ch_read == N);
the original post had a piece of p-code
almost identical to the above.
>
This will read and write blocks of N characters, until either 0 characters
have been read, indicating end-of-file, or 1..N-1 characters have been read,
indicating this was the last (partial) block.
--
Nick Keighley

Jun 27 '08 #24
On 16 Apr, 03:07, "Bill Cunningham" <nos...@nspam.comwrote:
your current problem seems almost identical to one I
gave a solution for. The only difference is the previous one
used fread/fwrite. I cannot comprehend how you can read my code

* * That's it right there. The ch you used I don't see all of what's going
on in your code. Basically I can't read it all.
Especially down around do.

and then post code that tries to copy a file ***without using a
loop construct***
and I still don't understand why you tried to mange without a loop...
It doesn't have to be a do while (I'm not very fond of 'em)
but you do need a loop

***By Nick K.***

#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);
* * }

* * do
* * {
* * * * if ((ch_read = fread (buf, 1, sizeof(buf), fp_in)) !=
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);
* * * * }
* * }
* * while (ch_read == sizeof(buf));

* * fclose (fp_in);
* * fclose (fp_out);

* * return 0;

}

----
*do
* * {
* * * * if ((ch_read = fread (buf, 1, sizeof(buf), fp_in)) !=
sizeof(buf)
* * * * * * *&& *ferror(fp_in))
* * * * {

Here's where I get lost.
ok. I suppose assigning and testing all in one statement
is a bit Cish (though I'm not the onlt perpetrator!).

My code with the write taken out

do
{
if ((ch_read = fread (buf, 1, sizeof(buf), fp_in)) != sizeof(buf)
&& ferror(fp_in))
{
TERMINATE;
}
}
while (ch_read == sizeof(buf))
simplify the if

do
{
ch_read = fread (buf, 1, sizeof(buf), fp_in);

/* terminate the program if too few characters were read
and an error occurred. Note a "short" read could be
simply caused by getting to the end of the file and
that shouldn't be treated as an error */
if ((ch_read != sizeof(buf) && ferror(fp_in))
{
TERMINATE;
}
}
while (ch_read == sizeof(buf)); /* read until we get a short non-
error read */

is that clearer?

or this (no do-while)

#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;
}
this makes an extra test
--
Nick Keighley

GOD is REAL
unless a type declaration to the contrary is made
Jun 27 '08 #25

"Richard Heathfield" <rj*@see.sig.invalidwrote in message
news:5-*********************@bt.com...
Bartc said:

<snip>
>Most of this is error-checking code that is not so important
when learning.

I can't agree with that. If you postpone learning about error-checking
until you've learned everything else, you'll end up never learning about
it at all
OK, keep the extended checking for the next lesson then.

The essential checks are verifying the file handles, and checking for
end-of-file, and these are done here.

Other checks are for errors unlikely in a learning situation, but themselves
become essential if this code was put into a library function to be used in
many different circumstances.

Otherwise the code looks overwhelming.

Quote from K&R2 p.164: "Although output errors are rare, they do occur (for
example, if a disk fills up), so a production program should check this as
well". Note the word 'production'.
--
Bart

Jun 27 '08 #26
Bartc said:

<snip>
>
Quote from K&R2 p.164: "Although output errors are rare, they do occur
(for example, if a disk fills up), so a production program should check
this as well". Note the word 'production'.
Yes. I have a great deal of respect for Brian Kernighan, but that doesn't
mean I agree with him about every single thing - and this is one of the
places where I disagree.

When illustrative (i.e. exegetic) code includes full error-checking, the
explanation can be in danger of getting a little lost, so there's a case
for giving two versions of such code: (a) here's the basic idea; (b)
here's how it looks when we do it properly. How else will the student
learn how to deal with the imperfect real world?

--
Richard Heathfield <http://www.cpax.org.uk>
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
Jun 27 '08 #27

"Richard Heathfield" <rj*@see.sig.invalidwrote in message
news:5-*********************@bt.com...
I can't agree with that. If you postpone learning about error-checking
until you've learned everything else, you'll end up never learning about
it at all - and this view is borne out by a huge amount of supposedly
professional code out there in the real world that completely ignores the
idea that anything might ever not quite work out the way the programmer
wants it to.
I haven't got to do and certainly not switch yet. But I'm getting pretty
good with for and using while without braces. A continuous loop with
basically two decsions. I learned how to open and close a file and use fread
fwrite simply at first without error checking. I knew error checking was
good needed to be learned too so that was the next step. After I was opening
and closing files and writing both binary and text (with fprintf). I added
error checking. Then tried for fgetc and that's where I am now. I personally
would have to agree with Bart. A person just needs to realize at the outset
they will need error checking in real life.

I had to conquer the open /write/close beast then the error checking and
I also notice most people don't use feof() but EOF.

Bill
Jun 27 '08 #28
[snip]

is that clearer?

or this (no do-while)

#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;
}
this makes an extra test
That's the way I learned yes that's great! Except I didn't learn with the
while braces that seems to be an option and the sequential reading makes for
good error check I've found out. I use return -1 instead of the exit
macros.

Bill
Jun 27 '08 #29
I put together this code that seems to work great by studying your code
and other code I have saved and been studying .

#include <stdio.h>

int main(int argc, char *argv[]) {
FILE *ifp,*ofp;
int a;
if(argc!=3) {
fprintf(stderr,"usage error\n");
return -1;
}
ifp=fopen(argv[1],"rb");
if (ifp==0) {
fprintf(stderr,"input error\n");
return -1;
}
ofp=fopen(argv[2],"wb");
if (ofp==0) {
fprintf(stderr,"output error\n");
return -1;
}
while((a=fgetc(ifp))!=EOF)
fputc(a,ofp);
fclose(ifp);
fclose(ofp);
printf("done\n");
return 0;
}

A little less overhead without stdlib and using return -1 if that really
matters. It seems to work like cp.

Bill
Jun 27 '08 #30
On Wed, 16 Apr 2008 19:27:59 GMT, "Bill Cunningham" <no****@nspam.com>
wrote:
>[snip]

is that clearer?

or this (no do-while)

#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))
You don't need the first test. If ferror returns a non-zero value, an
error occurred regardless of how many bytes were read 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;
}

Remove del for email
Jun 27 '08 #31

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

reply views Thread by Stephan Deibel | last post: by
reply views Thread by Stephan Deibel | last post: by
reply views Thread by Stephan Deibel | last post: by
reply views Thread by spam.noam | last post: by
45 posts views Thread by Nicholas M. Makin | last post: by
5 posts views Thread by divingIn | last post: by
1 post views Thread by CARIGAR | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.