running other files

Pages: 12
Jun 27, 2015 at 7:36pm
hey someone gave me this code to make a command execute an exe file. would it be able to execute another c++ file? if it is, how would i make it work, and after that other file finishes, it'll go back to the same string below, or where it'll just stop it in all. plz help!
also, would it need to be the full directory (users/username/desktop/file) or would it just look in the directory where the a.out file was executed in?

1
2
3
4
5
6
7
8
9
10
11
while(true)
{
    std::string command;
    std::cout << "enter a command: ";
    std::cin >> command;

    if(command == "hello")
        system(".\hello.exe"); // name of the programm you want to start
    if(command == "quit")
        break;
}
Last edited on Jun 27, 2015 at 8:34pm
Jun 27, 2015 at 8:18pm
What do you mean by
would it be able to execute another c++ file?
a "c++ file" is just a plain text file with a .cpp extension. Maybe you mean execute the executable? Or are you talking about just being able to call the objects from the other file?

Also you shouldn't use system like that. Because from what you have there you are probably going to try something like system(".\" + command + ".exe"); next and then you have a pretty big security flaw since someone could inject all sorts of commands that aren't files to be opened.

If you wish to open another file from inside of your file you should use the OS's library.
Last edited on Jun 27, 2015 at 8:19pm
Jun 27, 2015 at 8:33pm
but would it be possible to make a command.cpp (main file) file be able to run another c++ file called help.cpp and then run the same command.cpp file when it is done running

also, would i just be able to put the c++ directory i made with the system command inside a usb so i wouldn't need to enter the full directory?

plz help
Last edited on Jun 27, 2015 at 8:33pm
Jun 27, 2015 at 8:56pm
I still don't get what you mean by run another c++ file inside of another. Are you talking about calling the main function from one file inside of another?

The directly directory is based off of where the file is located. You may put it on the c drive and it would be local to that drive, if you put it on a usb then it would be local to that drive.


Also, are you trying to create something you can #include? If so you are going to need a header file with the declarations (the one you are including) and then a cpp file with the definitions.
Last edited on Jun 28, 2015 at 12:45am
Jun 27, 2015 at 9:13pm
i am looking for something that will do this:

something in the main.cpp file runs another file called help.cpp like

1
2
    if(command == "hello")
        system(".\help.cpp");

(this is probably incorrect)
like that. where itll run another cpp file
1
2
    if(command == "hello")
        system(".\help.cpp");
Last edited on Jun 27, 2015 at 9:16pm
Jun 27, 2015 at 9:39pm
a cpp file is just a text file. You are going to want to compile it into an executable and then call that file.
Jun 27, 2015 at 10:03pm
ok i did what you said but i got this error from this code:
 
system(".\cmd.out");

main.cpp:19:14: warning: unknown escape sequence '\c'
system(".\cmd.out");
^~
1 warning generated.
Jun 28, 2015 at 12:44am
I don't recall telling you to do system(".\cmd.out");
Jun 28, 2015 at 2:10am
but what code should i use to run the other executable file?
Jun 28, 2015 at 2:13am
The same way you did to call "hello.exe" though it is highly unsecure, if you are using windows you would want to use CreateProcess() and for linux I think it is exec() IIRC.
Jun 29, 2015 at 11:48am
i got a pc...
so i would just write like CreateProcess(cmd.exe), and it would read the default EXE's directory, or would i have to write out the whole directory?
Jun 29, 2015 at 11:53am
wait createprocess didnt work... i will try to use the origional one
Jun 29, 2015 at 12:03pm
i tried these
1
2
3
CreateProcess(cmd.exe)
system(".\hello.exe");
CreateProcess("cmd.exe");

none worked...
Jun 29, 2015 at 4:08pm
for the nth time, i would suggest user to not to use system()

http://www.cplusplus.com/forum/articles/11153/
Jun 29, 2015 at 4:45pm
There's nothing wrong with using system() for its intended purpose -- though you really should use CreateProcess() [on Windows] or fork() and exec*() [on *nixen].

Every program has a current working directory attached to it -- where in the directory tree it thinks it is. This CWD gets passed to any process invoked via system(). If you want a child process to be given a different CWD, your options are, in order of increasing madness:

- change the CWD before running the child process (and changing it back when you are done)
- give a command string that changes the directory AND starts the child process
- have the child process set its own CWD

To change the CWD from your program you can use Boost Filesystem, or you can use the old chdir() POSIX function (#include <unistd.h> on *nixen or <dirent.h> on Windows).


To compile a C++ program from your program, you will need to invoke the user's compiler, just as you would from the command-line. This is actually not a very easy task, particularly on Windows.


It kind of looks to me like you want to write a shell interpreter. That's an interesting project. There is a lot to find on the internet (https://www.google.com/search?q=write+your+own+shell). A good one to start is this one: http://stephen-brennan.com/2015/01/16/write-a-shell-in-c/

Brennan's is for C, so you can easily update it to use C++ techniques. For example, instead of using strtok(), use a split function: http://www.cplusplus.com/faq/sequences/strings/split/

Also, his tutorial is for Linux, so you won't want to use fork() and exec*() on Windows. Use CreateProcess(). It's a scary-looking function, but read through it and use the defaults for everything you can:
http://www.google.com/search?btnI=1&q=msdn+CreateProcess

Good luck!
Jun 29, 2015 at 6:20pm
i used createprocess, it didnt work. would it be
CreateProcess(command.exe)
or sometihng else? please just answer the correct code
Jun 29, 2015 at 6:29pm
How about clicking on the link I gave you? It tells you exactly what arguments it takes and comes replete with examples.
Jun 29, 2015 at 6:46pm
there are several links, and i only wanted the code to run a file called commandrun.exe in this directory:

C:\FOS\CoreServices\commandrun.exe

all i want is the code thatll run it. im sorry, im new to C++ and i just want the code
Jun 29, 2015 at 7:10pm
Sorry, if you are too lazy to bother, then so am I.
Jun 29, 2015 at 9:45pm
den just paste the code and dont edit it :)
Pages: 12