CWD C++ Windows - c++

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.

Related

Cannot require luacom library in lua script

I am new to Lua programming language. I installed Lua for Windows v5.1.5-52. I want to use luacom library to run shell script. Here is my code,
local luacom = require('luacom');
local shell = luacom.CreateObject("WScript.Shell")
shell:Run ('echo 123', 0)
which throws the following error:
lua: COM exception:(.\src\library\tLuaCOM.cpp,398):The system cannot
find the file specified.
I looked for tLuaCOM.cpp file, but could not find it, not even folder src. Though I found luacom.dll in clibs folder.
Is there any workaround with this problem?
tLuaCOM.cpp is a luacom source file, so it's probably not on your PC, except you've build it yourself.
The error comes from one of the calls - either CreateObject() or Run.
The Run Method (Windows Script Host) helps says that it starts processes:
The Run method starts a program running in a new Windows process.
but echo is a shell command, not an executable, so you have to start an instance of the Windows command interpreter and pass your command like:
shell:Run('cmd /c "echo 123"')

execute command and get working directory

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.

Stop error output in C++ when making new folder

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.

How can I open a new terminal from C ++ code and write inside it

How can I open a new terminal from C ++ code and write inside it. I know how to open new terminal by using system command (system("/Applications/Utilities/Terminal.app/Contents/MacOS/Terminal")), but do not know how to write string in it ? I'm working on an operating system mac os.
In Linux you can do so
std :: string cmd = "gnome-terminal-x sh-c 'ls-l; exec bash'";
system (cmd.c_str ());
how to do it in the mac os ?
Your basic mechanism of calling system() should still work, you just need a different command.
One way to do this is by running AppleScript from the command line via osascript. You can use the "AppleScript Editor" application (and use the Library command in its Window menu) to learn more about all the commands that can be given to programs in this way.
For example, to make the Mac Terminal run top, I could invoke this command line:
/usr/bin/osascript -e 'tell application "Terminal" to do script "top"'
Similarly, if I'd already written an entire file of commands to run, I could give it a .command extension and ask Terminal to open the file instead:
/usr/bin/osascript -e 'tell application "Terminal" to open "/Users/me/Desktop/MyFile.command"'

System() fails with return value 255/-1 (8 bit representation)

I have an executable, which has a few system commands (basically it does copying and running script files). When I test in standalone (launching the executable myself as with sudo) system() is working fine.
Now I integrate my executable with supervisord. Functionality of my executable is working fine, but the system() command fails with 255 / -1 (8 bit representation).
List of things I checked:
The current working directory of the process is correct
Supervisord and my process are running as root
chown of directory and file are root
Any other suggestions?
system("sudo cp ./Scripts/x.sh /tmp/");
sudo is the command to obtain superuser rights. It normally promts you for password (but under some circumstances, it skips it). Maybe it fails if it has no console to prompt you.
Anyway you should NOT do this. You sould just write system("cp ./Scripts/x.sh /tmp/") and start your program with root access (supervisord probably has a way to do that).