gabriel rosenkoetter on Fri, 14 Jun 2002 17:40:25 +0200


[Date Prev] [Date Next] [Thread Prev] [Thread Next] [Date Index] [Thread Index]

Re: [PLUG] unix cp function call


On Fri, Jun 14, 2002 at 11:03:24AM -0400, Bill Jonas wrote:
> I don't think that there's a library function call or syscall or
> anything special to use.  Just open both files, read(2) from one, and
> write(2) to the other (or whichever pair or reading/writing functions
> you prefer -- you could even use getchar(3)/putchar(3) if you wanted,
> but it's not recommended ;-) ).

Erm. Fred, if you actually meant that you had the pathnamess for
the files in your char *s, then you need to do something more like
this:

#define MAX_BUF 1024
char *dst, *src;
/*
 * fill dst and src
 */
char buffer[MAX_BUF];
size_t bytes_read;
FILE *source, *dest;

source = fopen(source, "r");
dest = fopen(dest, "w");
memset(buffer, 0, MAX_BUF);

bytes_read = fread(buffer, 1, MAX_BUF, source);
  /* second arg could be whatever you like; 1 == sizeof(char) in my world */
while(bytes_read) {
	fwrite(buffer, 1, bytes_read, dest);
}

fclose(dest);
fclose(source);
  /*
   * Don't forget this! You could lose data if you do!
   * Also, Proper Form dictates this order, though it shouldn't
   * matter unless you're doing C++.
   */

See fopen(3), fread(3), memset(3).

-- 
gabriel rosenkoetter
gr@eclipsed.net

Attachment: pgpDxsXSIP6ft.pgp
Description: PGP signature