Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
| From | Ike Naar <ike@iceland.freeshell.org> |
|---|---|
| Newsgroups | comp.lang.c |
| Subject | Re: copying files |
| Date | 2012-02-16 16:19 +0000 |
| Organization | A noiseless patient Spider |
| Message-ID | <slrn3vfsjjqb43.is8.ike@iceland.freeshell.org> (permalink) |
| References | <f62ccd5a-1cf4-4585-b8ca-c515a53d8a73@w9g2000vbv.googlegroups.com> <jhijf0$6lu$1@reader1.panix.com> <c93add96-9d72-4998-b8c2-62b9fee0a09d@hk10g2000vbb.googlegroups.com> <jhiqm2$r4n$1@reader1.panix.com> |
On 2012-02-16, JohnF <john@please.see.sig.for.email.com> wrote:
> int fcopy( char *infile, char *outfile ) {
> FILE *inptr = fopen(infile,"rb"), /*open file for binary read*/
> *outptr = fopen(outfile,"wb"); /*and write*/
> unsigned char buff[256]; /*block of bytes from infile*/
> int buflen=255, nread=0,nwrite=0, /*#bytes we try to read/write*/
> nrw = 0; /*total bytes read/written*/
> if ( inptr!=NULL && outptr!=NULL ) { /*have opened files*/
> while ( 1 ) { /*read & write them till eof*/
> /* --- read bytes from infile --- */
> nread = fread(buff,sizeof(unsigned char),buflen,inptr); /*read*/
sizeof(unsigned char) is 1 by definition.
Is there a particular reason why you are reading one byte less than
the size of the buffer? It seems more logical to use the entire buffer:
nread = fread(buff, 1, sizeof buff, inptr);
> if ( nread < 1 ) break; /* no bytes left in file */
> /* --- write bytes to outfile --- */
> nwrite = fwrite(buff,sizeof(unsigned char),nread,outptr); /*write*/
> if ( nwrite != nread ) { nrw=(-1); break; } /*problem writing*/
> nrw += nwrite; /*total #bytes*/
> if ( nread < buflen ) break; /* no bytes left in file */
if (nread < sizeof buff) break;
> } /* --- end-of-while(1) --- */
> fclose(inptr); fclose(outptr); /* close files */
> } /* --- end-of-if(fileptrs!=NULL) --- */
> return ( nrw ); /*back to caller with file size*/
Potential file descriptor leak: if one of the files could be opened
but the other couldn't, the opened file is not closed.
Also, in this case 0 is returned, so from the return value you
cannot distinguish between having fopen errors, and having completed
a successful copy of an empty file.
> } /* --- end-of-function fcopy() --- */
>
> ...which I've snipped from some code that works (with a few
> essentially cosmetic changes so it reads okay as a code fragment).
Back to comp.lang.c | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
copying files Hans Vlems <hvlems@freenet.de> - 2012-02-16 01:03 -0800
Re: copying files Malcolm McLean <malcolm.mclean5@btinternet.com> - 2012-02-16 01:23 -0800
Re: copying files Keith Thompson <kst-u@mib.org> - 2012-02-16 01:31 -0800
Re: copying files Hans Vlems <hvlems@freenet.de> - 2012-02-16 03:14 -0800
Re: copying files Keith Thompson <kst-u@mib.org> - 2012-02-16 09:43 -0800
Re: copying files Hans Vlems <hvlems@freenet.de> - 2012-02-16 14:59 -0800
Re: copying files Keith Thompson <kst-u@mib.org> - 2012-02-16 15:06 -0800
Re: copying files Hans Vlems <hvlems@freenet.de> - 2012-02-16 03:13 -0800
Re: copying files Keith Thompson <kst-u@mib.org> - 2012-02-16 09:56 -0800
Re: copying files Malcolm McLean <malcolm.mclean5@btinternet.com> - 2012-02-16 10:11 -0800
Re: copying files Hans Vlems <hvlems@freenet.de> - 2012-02-16 15:11 -0800
Re: copying files Keith Thompson <kst-u@mib.org> - 2012-02-16 15:21 -0800
Re: copying files Philip Lantz <prl@canterey.us> - 2012-02-17 09:53 -0800
Re: copying files Hans Vlems <hvlems@freenet.de> - 2012-02-18 07:22 -0800
Re: copying files Malcolm McLean <malcolm.mclean5@btinternet.com> - 2012-02-17 03:36 -0800
Re: copying files Willem <willem@toad.stack.nl> - 2012-02-17 22:04 +0000
Re: copying files Hans Vlems <hvlems@freenet.de> - 2012-02-18 07:28 -0800
Re: copying files Willem <willem@toad.stack.nl> - 2012-02-18 21:28 +0000
Re: copying files Hans Vlems <hvlems@freenet.de> - 2012-02-18 07:19 -0800
Re: copying files Hans Vlems <hvlems@freenet.de> - 2012-02-16 15:02 -0800
Re: copying files Keith Thompson <kst-u@mib.org> - 2012-02-16 01:34 -0800
Re: copying files Hans Vlems <hvlems@freenet.de> - 2012-02-16 03:27 -0800
Re: copying files Ben Bacarisse <ben.usenet@bsb.me.uk> - 2012-02-16 17:11 +0000
Re: copying files Hans Vlems <hvlems@freenet.de> - 2012-02-16 14:57 -0800
Re: copying files Keith Thompson <kst-u@mib.org> - 2012-02-16 09:54 -0800
Re: copying files Hans Vlems <hvlems@freenet.de> - 2012-02-16 06:41 -0800
Re: copying files JohnF <john@please.see.sig.for.email.com> - 2012-02-16 09:49 +0000
Re: copying files Hans Vlems <hvlems@freenet.de> - 2012-02-16 03:10 -0800
Re: copying files JohnF <john@please.see.sig.for.email.com> - 2012-02-16 11:52 +0000
Re: copying files Hans Vlems <hvlems@freenet.de> - 2012-02-16 06:56 -0800
Re: copying files David Thompson <dave.thompson2@verizon.net> - 2012-02-24 04:43 -0500
Re: copying files Ike Naar <ike@iceland.freeshell.org> - 2012-02-16 16:19 +0000
Re: copying files Hans Vlems <hvlems@freenet.de> - 2012-02-16 14:50 -0800
Re: copying files David Thompson <dave.thompson2@verizon.net> - 2012-02-24 04:43 -0500
Re: copying files Nobody <nobody@nowhere.com> - 2012-02-16 10:58 +0000
Re: copying files Keith Thompson <kst-u@mib.org> - 2012-02-16 09:41 -0800
Re: copying files Hans Vlems <hvlems@freenet.de> - 2012-02-16 14:59 -0800
Re: copying files Nobody <nobody@nowhere.com> - 2012-02-17 06:25 +0000
Re: copying files Kleuske <kleuske@nowhere.net> - 2012-02-16 11:15 +0000
Re: copying files Hans Vlems <hvlems@freenet.de> - 2012-02-16 06:45 -0800
Re: copying files Keith Thompson <kst-u@mib.org> - 2012-02-16 09:49 -0800
csiph-web