Article View: comp.os.msdos.programmer
Article #5633Re: using "system" in C
From: John Temples
Date: Fri, 05 Apr 1991 02:48
Date: Fri, 05 Apr 1991 02:48
30 lines
1502 bytes
1502 bytes
In article <jono.670739550@dec06> jono@dec06.cs.monash.edu.au (Jonathan Oliver) writes:
> system("copy file1 file2");
>
>there was not enough memory to perform the copy.
The two problems with your approach are that system() has to load a
copy of the command processor to do its job (which takes additional
time and memory), and you're assuming that there's a command called
"copy" on the system running your program. Since "copy" is a
COMMAND.COM internal command, and not everyone who runs DOS uses
COMMAND.COM, your program is non-portable. Also, system() usually
fails when your switch character is not set to "/". In other words,
system() is not usually a wise choice in any "serious" program.
You should open "file1" and "file2" and do the copy yourself, something
like this:
int copy(FILE *infile, FILE *outfile)
{
char buf[BUFSIZ];
int cnt;
while (cnt = fread(buf, sizeof(*buf), sizeof(buf), infile))
if (fwrite(buf, sizeof(*buf), cnt, outfile) != cnt)
return 1; /* probably disk full */
return 0;
}
--
John W. Temples -- john@jwt.UUCP (uunet!jwt!john)
Technical Details
Message-ID:
<1991Apr5.024832.2361@jwt.UUCP>Path:
arc1-ca.pugleaf.net!utzoo!utgpu!news-server.csri.toronto.edu!rpi!zaphod.mps.ohio-state.edu!samsung!uunet!jwt!johnReferences:
<jono.670739550@dec06>Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!utzoo!utgpu!news-server.csri.toronto.edu!rpi!zaphod.mps.ohio-state.edu!samsung!uunet!jwt!john From: john@jwt.UUCP (John Temples) Newsgroups: comp.os.msdos.programmer Subject: Re: using "system" in C Message-ID: <1991Apr5.024832.2361@jwt.UUCP> Date: 5 Apr 91 02:48:32 GMT References: <jono.670739550@dec06> Distribution: comp Organization: Private System -- Orlando, FL Lines: 30