PDA

View Full Version : system("")



skyruler54
09-27-2002, 07:09 PM
This is dos specific I think. Could someone list the most useful system commands and what they do?

face_master
09-28-2002, 12:44 AM
Any command you use normally at the command prompt/msdos prompt you can place between the brackets, but the 2 most commonly used ones are:
system("pause"); // Displays "Press any key to continue..."
system("cls"); // clears the screen

Fordy
09-28-2002, 01:47 AM
There are loads of DOS commands....

Books like "DOS for Dummies" by Dan Gookin are cheap and give you a good start with learning DOS

Waldo2k2
09-28-2002, 08:53 PM
very very useful ones are COPY, etc.
www.easydos.com/index
all the commands there can be done through system...if i remember correctly, it can even be used to launch other programs instead of using shellExecute() like so


system("cd c:\whatever");
system("someProgram.exe"); //where someProgram resides in c:\whatever

Aran
09-28-2002, 09:23 PM
yeah, ok.. But then how do you read off what that program prints out or access any of the values that program returns?

Waldo2k2
09-28-2002, 09:27 PM
well, i know that if i call the COPY program in from system and something goes wrong it will print out the error within my program...so, yeah, my guess is as good as yours, but the program WILL execute, that's all im saying

Fordy
09-29-2002, 05:34 AM
Originally posted by Aran Elus
yeah, ok.. But then how do you read off what that program prints out or access any of the values that program returns?

You can reroute the programs output to another stream (say a file) and then read itor do whatever.....

The return of the executed program is the return of system()



#include <stdio.h>
#include <stdlib.h>

int main (){

FILE *fout = NULL;
char c;
int nRet;

fout = freopen("myfile.txt","w",stdout);//all output to file
nRet = system("DIR");//run your command, save result
freopen("CON","w",stdout);//restore stream (this closes file)
fout = fopen("myfile.txt","r");//open the file again
printf("Results of DIR command\n");
while((c = fgetc(fout)) != EOF)//read contents
putc(c,stdout); //spit out results
printf("\nExited with a return of %d\n",nRet);//State result
fclose(fout);

return 0;
}

shaik786
10-14-2002, 07:39 AM
Or simply use popen()

master5001
10-17-2002, 09:21 PM
The standard c library eliminates the need for the use of the function system(). Code like this:



//assume name is a char *
remove(name);


runs much faster than this:



//assume temp is also a char *
sprintf(temp, "delete %s", name);
system(temp);


Of course in terms of easy some prefer copying a file in one slow system() call rather than a couple of calls to various stdio functions.

sean
10-18-2002, 09:21 AM
The only time I use System is to use simple program that either I've written specifically for that purpose, and programs that produce no output themselves. It simply a way of telling the system what to do.