Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > fread/fwrite 2

Reply
Thread Tools

fread/fwrite 2

 
 
Bill Cunningham
Guest
Posts: n/a
 
      06-23-2012
Ok after looking a Nick's old post again, I decided to try my own
copy. It seems to fill better with my style. Now it compiles can
copies fine. The thing is it hasn't been tested as to whether or not
it copied actual 2048 bytes at a time. I will submit it for review...

#include <stdio.h>

int main()
{
size_t nread = 0;
char buf[2048];
FILE *fpi, *fpo;
if ((fpi = fopen("t", "rb")) == NULL)
return -1;
if ((fpo = fopen("z", "wb")) == NULL)
return -2;
do {
nread = fread(buf, sizeof(buf), 1, fpi);
if (ferror(fpi))
return -3;
fwrite(buf, sizeof(buf), nread, fpo);
if (ferror(fpo))
return -4;
}
while (!feof(fpi));
fclose(fpi);
fclose(fpo);
return 0;
}
 
Reply With Quote
 
 
 
 
Bill Cunningham
Guest
Posts: n/a
 
      06-23-2012
On Jun 23, 4:31*pm, Bill Cunningham <(E-Mail Removed)> wrote:
> Ok after looking a Nick's old post again, I decided to try my own
> copy. It seems to fill better with my style. Now it compiles can
> copies fine. The thing is it hasn't been tested as to whether or not
> it copied actual 2048 bytes at a time. I will submit it for review...
>
> #include <stdio.h>
>
> int main()
> {
> * * size_t nread = 0;
> * * char buf[2048];
> * * FILE *fpi, *fpo;
> * * if ((fpi = fopen("t", "rb")) == NULL)
> * * * * return -1;
> * * if ((fpo = fopen("z", "wb")) == NULL)
> * * * * return -2;
> * * do {
> * * * * nread = fread(buf, sizeof(buf), 1, fpi);
> * * * * if (ferror(fpi))
> * * * * * * return -3;
> * * * * fwrite(buf, sizeof(buf), nread, fpo);
> * * * * if (ferror(fpo))
> * * * * * * return -4;
> * * }
> * * while (!feof(fpi));
> * * fclose(fpi);
> * * fclose(fpo);
> * * return 0;
>
>
>
> }- Hide quoted text -
>
> - Show quoted text -


Ok I guess there must be a short count somewhere in this code. Most of
it has been copied I get no errors and I must admit I'm a little
stumped. It's got to be in the fread fwrite system somehow.

B
 
Reply With Quote
 
 
 
 
Bill Cunningham
Guest
Posts: n/a
 
      06-23-2012
On Jun 23, 4:31*pm, Bill Cunningham <(E-Mail Removed)> wrote:
> Ok after looking a Nick's old post again, I decided to try my own
> copy. It seems to fill better with my style. Now it compiles can
> copies fine. The thing is it hasn't been tested as to whether or not
> it copied actual 2048 bytes at a time. I will submit it for review...
>
> #include <stdio.h>
>
> int main()
> {
> * * size_t nread = 0;
> * * char buf[2048];
> * * FILE *fpi, *fpo;
> * * if ((fpi = fopen("t", "rb")) == NULL)
> * * * * return -1;
> * * if ((fpo = fopen("z", "wb")) == NULL)
> * * * * return -2;
> * * do {
> * * * * nread = fread(buf, sizeof(buf), 1, fpi);
> * * * * if (ferror(fpi))
> * * * * * * return -3;
> * * * * fwrite(buf, sizeof(buf), nread, fpo);
> * * * * if (ferror(fpo))
> * * * * * * return -4;
> * * }
> * * while (!feof(fpi));
> * * fclose(fpi);
> * * fclose(fpo);
> * * return 0;
>
>
>
> }- Hide quoted text -
>
> - Show quoted text -


Ok my apoligies all. It looks like in the time it took me to look at
Nick K's old code and write my own I ended up getting a couple of
parameters backwards. Looks like a write error to me. Right? If
anyones has any input I'd appreciate.

B
 
Reply With Quote
 
Ike Naar
Guest
Posts: n/a
 
      06-23-2012
On 2012-06-23, Bill Cunningham <(E-Mail Removed)> wrote:
> On Jun 23, 4:31?pm, Bill Cunningham <(E-Mail Removed)> wrote:
>> Ok after looking a Nick's old post again, I decided to try my own
>> copy. It seems to fill better with my style. Now it compiles can
>> copies fine. The thing is it hasn't been tested as to whether or not
>> it copied actual 2048 bytes at a time. I will submit it for review...
>>
>> #include <stdio.h>
>>
>> int main()


int main(void)

>> {
>> ? ? size_t nread = 0;
>> ? ? char buf[2048];
>> ? ? FILE *fpi, *fpo;
>> ? ? if ((fpi = fopen("t", "rb")) == NULL)
>> ? ? ? ? return -1;
>> ? ? if ((fpo = fopen("z", "wb")) == NULL)
>> ? ? ? ? return -2;
>> ? ? do {
>> ? ? ? ? nread = fread(buf, sizeof(buf), 1, fpi);


The second parameter of fread specifies the size of the
elemtents to be read, and the third parameter specifies the
(maximal) number of them. If you're going to read bytes instead
of 2048-byte blocks, it would be more natural to use

nread = fread(buf, 1, sizeof buf, fpi);

>> ? ? ? ? if (ferror(fpi))
>> ? ? ? ? ? ? return -3;
>> ? ? ? ? fwrite(buf, sizeof(buf), nread, fpo);


and, to match the change in the fread arguments, here it would be

fwrite(buf, 1, nread, fpo);

>> ? ? ? ? if (ferror(fpo))
>> ? ? ? ? ? ? return -4;
>> ? ? }
>> ? ? while (!feof(fpi));
>> ? ? fclose(fpi);
>> ? ? fclose(fpo);
>> ? ? return 0;
>>
>>
>>
>> }- Hide quoted text -
>>
>> - Show quoted text -

>
> Ok I guess there must be a short count somewhere in this code. Most of
> it has been copied I get no errors and I must admit I'm a little
> stumped. It's got to be in the fread fwrite system somehow.
>
> B



--
http://www.velocityreviews.com/forums/(E-Mail Removed)
SDF Public Access UNIX System - http://sdf.lonestar.org
 
Reply With Quote
 
John Gordon
Guest
Posts: n/a
 
      06-24-2012
In <(E-Mail Removed)> Bill Cunningham <(E-Mail Removed)> writes:

> Ok my apoligies all. It looks like in the time it took me to look at
> Nick K's old code and write my own I ended up getting a couple of
> parameters backwards. Looks like a write error to me. Right? If
> anyones has any input I'd appreciate.


If the program isn't behaving the way you want, tell us that. Say
exactly what it is doing wrong.

Does it not compile?
Does it run, but print an error message?
Does it run, but not produce the desired output?

Vague comments like "Looks like a write error" don't help us track down
the problem.

--
John Gordon A is for Amy, who fell down the stairs
(E-Mail Removed) B is for Basil, assaulted by bears
-- Edward Gorey, "The Gashlycrumb Tinies"

 
Reply With Quote
 
Bill Cunningham
Guest
Posts: n/a
 
      06-24-2012
On Jun 24, 10:01*am, John Gordon <(E-Mail Removed)> wrote:

> If the program isn't behaving the way you want, tell us that. *Say
> exactly what it is doing wrong.
>
> Does it not compile?
> Does it run, but print an error message?
> Does it run, but not produce the desired output?
>
> Vague comments like "Looks like a write error" don't help us track down
> the problem.


Well It's like I said above it's a short count. diff says the files
differ. I'm not wanting to copy 2048 bytes but 2048 byte sized blocks.
I can copy bytes with fputs and fputc. I want a copy of the entire
file.

B
 
Reply With Quote
 
Ben Bacarisse
Guest
Posts: n/a
 
      06-24-2012
Bill Cunningham <(E-Mail Removed)> writes:

> On Jun 24, 10:01*am, John Gordon <(E-Mail Removed)> wrote:
>
>> If the program isn't behaving the way you want, tell us that. *Say
>> exactly what it is doing wrong.
>>
>> Does it not compile?
>> Does it run, but print an error message?
>> Does it run, but not produce the desired output?
>>
>> Vague comments like "Looks like a write error" don't help us track down
>> the problem.

>
> Well It's like I said above it's a short count. diff says the files
> differ. I'm not wanting to copy 2048 bytes but 2048 byte sized blocks.
> I can copy bytes with fputs and fputc. I want a copy of the entire
> file.


Did you miss the correct analysis of the bug? It was posted yesterday by
Ike Naar.

Another strategy would have been to compare all the changes you made
from the original working code. That would have enabled you to find the
bug that Ike pointed out by yourself.

--
Ben.
 
Reply With Quote
 
Bill Cunningham
Guest
Posts: n/a
 
      06-24-2012
On Jun 24, 3:06*pm, Ben Bacarisse <(E-Mail Removed)> wrote:
> Bill Cunningham <(E-Mail Removed)> writes:
> > On Jun 24, 10:01*am, John Gordon <(E-Mail Removed)> wrote:

>
> >> If the program isn't behaving the way you want, tell us that. *Say
> >> exactly what it is doing wrong.

>
> >> Does it not compile?
> >> Does it run, but print an error message?
> >> Does it run, but not produce the desired output?

>
> >> Vague comments like "Looks like a write error" don't help us track down
> >> the problem.

>
> > *Well It's like I said above it's a short count. diff says the files
> > differ. I'm not wanting to copy 2048 bytes but 2048 byte sized blocks.
> > I can copy bytes with fputs and fputc. I want a copy of the entire
> > file.

>
> Did you miss the correct analysis of the bug? *It was posted yesterday by
> Ike Naar.


Both the posts were written by me at the same time. 6:18 and 6:32 PM
were when each were posted. Then Ike responded at 6:42. Why it took so
long for the 2nd post to post I don't know.
>
> Another strategy would have been to compare all the changes you made
> from the original working code. *That would have enabled you to find the
> bug that Ike pointed out by yourself.
>

I did. My original post was premature.

Bill
 
Reply With Quote
 
Bill Cunningham
Guest
Posts: n/a
 
      06-24-2012
On Jun 23, 6:42*pm, Ike Naar <(E-Mail Removed)> wrote:
> On 2012-06-23, Bill Cunningham <(E-Mail Removed)> wrote:
>
> > On Jun 23, 4:31?pm, Bill Cunningham <(E-Mail Removed)> wrote:
> >> Ok after looking a Nick's old post again, I decided to try my own
> >> copy. It seems to fill better with my style. Now it compiles can
> >> copies fine. The thing is it hasn't been tested as to whether or not
> >> it copied actual 2048 bytes at a time. I will submit it for review...

>
> >> #include <stdio.h>

>
> >> int main()

>
> int main(void)
>
> >> {
> >> ? ? size_t nread = 0;
> >> ? ? char buf[2048];
> >> ? ? FILE *fpi, *fpo;
> >> ? ? if ((fpi = fopen("t", "rb")) == NULL)
> >> ? ? ? ? return -1;
> >> ? ? if ((fpo = fopen("z", "wb")) == NULL)
> >> ? ? ? ? return -2;
> >> ? ? do {
> >> ? ? ? ? nread = fread(buf, sizeof(buf), 1, fpi);

>
> The second parameter of fread specifies the size of the
> elemtents to be read, and the third parameter specifies the
> (maximal) number of them. If you're going to read bytes instead
> of 2048-byte blocks, it would be more natural to use
>
> * * * * nread = fread(buf, 1, sizeof buf, fpi);
>
> >> ? ? ? ? if (ferror(fpi))
> >> ? ? ? ? ? ? return -3;
> >> ? ? ? ? fwrite(buf, sizeof(buf), nread, fpo);

>
> and, to match the change in the fread arguments, here it would be
>
> * * * * fwrite(buf, 1, nread, fpo);
>
>
>
>
>
> >> ? ? ? ? if (ferror(fpo))
> >> ? ? ? ? ? ? return -4;
> >> ? ? }
> >> ? ? while (!feof(fpi));
> >> ? ? fclose(fpi);
> >> ? ? fclose(fpo);
> >> ? ? return 0;

>
> >> }- Hide quoted text -

>
> >> - Show quoted text -

>
> > Ok I guess there must be a short count somewhere in this code. Most of
> > it has been copied I get no errors and I must admit I'm a little
> > stumped. It's got to be in the fread fwrite system somehow.

>
> > B


Thanks Ike and I hope this post was not premature. I altered my code
some taking your advice and looking at Nick's old code. Here's what I
have now and it compiles and runs fine and a complete file is copied.
I think in 2048 blocks too. Here's a copy of my amended code...

#include <stdio.h>

int main()
{
size_t nread = 2048;
char buf[2048];
FILE *fpi, *fpo;
if ((fpi = fopen("t", "rb")) == NULL)
return -1;
if ((fpo = fopen("z", "wb")) == NULL)
return -2;
do {
nread = fread(buf, 1, sizeof buf, fpi);
if (ferror(fpi))
printf("error 3");
fwrite(buf, 1, nread, fpo);
if (ferror(fpo))
printf("error 4");
}
while (!feof(fpi));
fclose(fpi);
fclose(fpo);
return 0;
}

Bill
 
Reply With Quote
 
Ben Bacarisse
Guest
Posts: n/a
 
      06-24-2012
Bill Cunningham <(E-Mail Removed)> writes:
<snip>
> Thanks Ike and I hope this post was not premature. I altered my code
> some taking your advice and looking at Nick's old code. Here's what I
> have now and it compiles and runs fine and a complete file is copied.
> I think in 2048 blocks too. Here's a copy of my amended code...
>
> #include <stdio.h>
>
> int main()


Why not int main(void)? It may not matter to you here, but it's a good
idea to get into the habit of using the best C idioms everywhere.

> {
> size_t nread = 2048;


I prefer not to initialise variables unless the value is actually
useful.

> char buf[2048];
> FILE *fpi, *fpo;
> if ((fpi = fopen("t", "rb")) == NULL)
> return -1;
> if ((fpo = fopen("z", "wb")) == NULL)
> return -2;


Are negative return values useful to you on your system? They aren't
very useful on mine.

A program that has hard-wired file names like this is not going to be
useful. I know this is learning exercise, but the next step would be to
learn how to get rid of this restriction.

> do {
> nread = fread(buf, 1, sizeof buf, fpi);
> if (ferror(fpi))
> printf("error 3");
> fwrite(buf, 1, nread, fpo);
> if (ferror(fpo))
> printf("error 4");


The error handling is inconsistent. You print a message for two errors
and use non-zero return values for the others. It suggest you haven't
really decided what to do with errors. Also, errors should be printed
to stderr -- that's what it's for after all.

> }
> while (!feof(fpi));


There's a bug here. What happens when there is a read error?

> fclose(fpi);
> fclose(fpo);


There's a another inconsistency here. You close the streams when all
goes well, but not when the output file can't be opened. It's a good
idea to get into the habit of closing all open streams.

> return 0;
> }


<snip>
--
Ben.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off




Advertisments