I am using C++ on Ubuntu. I have been using the command:
system("mkdir new_folder");
to make a new folder called new_folder. However, if that folder already exists, C++ outputs an error message (and continues to run afterwards).
Is there a way to stop the error message from printing out?
For this particular command use mkdir -p new_folder.
Generally, you want to fork your process and on one of the branches redirect stdout and stderr to /dev/null or similar then do exec to replace the process with the new one.
Related
I want to execute a shell command in C++ and at the end I would like to fetch the current working directory of the executed process.
e.g. I executing the command cd C:\
then at the end of the command I want to get the directory C:\
and store it in a variable.
What I tried was pipe = _popen(cmd, "r") to execute the command, but at the end of the command, even when _pclose(pipe) wasn't called yet, when I called _getcwd(NULL, 0), I got the cwd of the running C++ program and not the changed cwd from _popen.
Does anyone know, how I could achieve this?
I found a solution for this:
I am creating a new Process of "cmd.exe" using CreateProcess() and injecting a piece of assembly code in the created process.
http://forum.codecall.net/topic/61271-how-to-get-current-directory-of-another-process/
I combined this with the sample from msdn for redirecting the stdin and stdout of the child process.
I created a c++ programm that works with ros. The first step would be to open a roscore in a terminal and move on from there. I do so with system("roscore &");
I compiled my file and can run it just fine with ./file.
However, I want to be able to run it as an application (double click). I created a .desktop file and the program shows up in my application list. When i start it though, all I get is a terminal that opens with the message
sh: 1: roscore: not found
etc.
The same applies for the roslaunch commands. I also fork and exec a roslaunch command, which does not work as well.
I tried system("ls"); which worked. All cout messages work as well.
Any idea what is wrong here?
roscore executable is not located in std paths (/bin:/usr/bin:). Use the absolute path - system("/path/to/roscore &")
I am trying to have my program change directory (to where the user wishes to) but I am unable to navigate there and create a file? It appears that I am able to navigate there but when I get to the next system call it returns back to the current directory
Is there a way to set where my program cwd is pointing to?
std::string s1 = "cd " + userDirectory;
system(s1.c_str());
system("dir > test.txt");
SetCurrentDirectory Win32 on Windows.
chdir() / _chdir for POSIX (a common C API available on many OS:es).
boost/std::filesystem current_path() for C++ (std in C++17).
The system function starts a new command interpreter as a new process. And then runs the commands in that command interpreter. And as the cd command is a built-in command it will only apply to that command interpreter process, not your process.
You have a couple of solutions you can try:
Put the commands (cd and dir and everything else) in to a script file that you run.
Change the working directory of your process.
On Windows you can change working directory using SetCurrentDirectoryW function.
I tried the following code, to communicate with the command line from c++ code.
#include<iostream>
#include<cv.h>
int main()
{
system("gnome-terminal");
system("cd");
}
The gnome-terminal command is executing fine. After I close the terminal, when am expecting the cd to execute, however, is not happening. Could you please help me and point out the reason? Thanks. I was expecting the function to make the cmd go down to the home directory
, but it did not. am working in linux
I tried it even by removing gnome. simple cd is not working. am I doing something rong>?
If I try ls, it seems to be working fine!
My main aim is to open a new terminal, and execute commands on that new terminal through the present program that opened the new terminal. Could you please tell me how I can achieve this??
If you want to run a program and wait for it to finish before executing next line, take a look at this page and example code here: http://www.thegeekstuff.com/2012/03/c-process-control-functions/
But if you want to run gnome-terminal and execute a command in newly created window, do this:
system("gnome-terminal -x sh -c 'cd /tmp ; ls -la'");
The system function creates a shell child process to execute the specified command.
cd is a shell command which changes the current working directory of that shell process only.
So the child's cd probably works fine, but it has no effect on your C++ program, which is a different process.
Instead, you probably want to look at the Linux system call chdir.
Thanks for your help!! This command worked perfectly fine from this link
https://superuser.com/questions/198015/open-gnome-terminal-programmatically-and-execute-commands-after-bashrc-was-execu
gnome-terminal -x sh -c 'command1; command2; exec bash'
and I entered the respective commands in the new window. But to change the working directory in the shell am working o, I haven't still figured that out.
A simple question:
I'm using
system("mkdir /some/dest/");
on my program. How do I disable its output to the screen when running my program because if the dir exists, I get "mkdir: cannot create directory `/some/dest/': File exists"
Tried to run
system("mkdir /some/dest/ > /dev/null");
but it didn't work
Thanks
It's a weird way to create directories from C code, taking into account that mkdir (1) itself is just a wrapper for mkdir (2) system call.
I'd recommend using mkdir (2).
P.s. By (2) I mean man category of documentation ($ man 2 mkdir), this is a category for system calls.
There are two streams, stdout and stderr. You are redirecting only strout, not stderr.
You need to redirect stderr to /dev/null as well; read your shell manual. In bash I think you can do:
mkdir /some/dest/ &> /dev/null
This answer your question about how to hide error messages when calling system(), but the advice from other answers (using native C function mkdir) is more C/C++ idiomatic.