system with variable

Hello!
Need to take variable char or string data and make system call (Linux CLI) as per above.
Please advise

const char key = "echo -n '3. Current Directory is '; pwd";
system(key);
So, what's your actual problem? (Presumably key should be an array in your sample).
got compilation error:

58 | const char key = "echo -n '3. Current Directory is '; pwd";
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
| |
| const char*
transaction.cpp:59:9: error: invalid conversion from ‘char’ to ‘const char*’ [-fpermissive]
59 | system(key);
| ^~~
| |
| char
const char* const key = "echo -n '3. Current Directory is '; pwd"; // Note: *

or

const char key[] = "echo -n '3. Current Directory is '; pwd"; // Note: []
Thanks!
Can it be used with string as well?
You mean std::string? Sure:
1
2
3
std::string key = "echo -n '3. Current Directory is '; pwd";
...
system(key.c_str()); // Note: c_str() since system(...) expects const char* 
Answer: Don’t use system() for stuff like that.

Assuming C++

1
2
#include <filesystem>
#include <iostream> 
 
std::cout << "3. Current Directory is " << std::filesystem::current_path() << "\n";

Assuming C

1
2
3
4
5
6
#ifdef _MSC_VER
  #include <dirent.h>
  #define getcwd _getcwd
#else
  #include <unistd.h>
#endif 
1
2
3
4
5
const char * cwd = getcwd( NULL, 0 );

printf( "3. Current Directory is: %s\n", cwd );

free( cwd );
or
1
2
char cwd[MAX_PATH] = {0};
printf( "3. Current Directory is: %s\n", getcwd( cwd, MAX_PATH ) );

If you are using C you may wish to handle the (very unlikely) chance that getcwd() will return NULL.

Hope this helps.
Last edited on
thank you, coder777
Answer: Don’t use system() for stuff like that.
You probably shouldn't use system() at all, as you can't get error information from it.
Why?
I don't know how to convert linux commands to C++ functions.
Why?

a hacker can replace the program (even the operating system ones) your program calls via system with malware of the same name. The odds of this are zero when its a personal utility program, but you cannot release a program out for others to use using this technique as the more popular it becomes, the more of a target it becomes, and its a security hazard.

that is on top of being unable to easily get errors back from it which was already stated, though you can redirect your commands to a text file and capture the output and work with it that way (clunky!).

in general, if the thing you are doing is trivial to do in standard c++, you should *learn* the c++ way and do it that way. If it is going to take a lot of code or a library, you can spawn a program (there are a half dozen ways to do that, exec, spawn, create process, system, fork, and more?) each with pros and cons.
the command you are doing is trivial in standard c++ using <filesystem> calls. Include it, look up what to do, and do that instead for this specific use case.
https://en.cppreference.com/w/cpp/filesystem/current_path
If everything you are doing winds up in a system() call, why use C++ at all? Just write a shell script. It’ll be faster and easier.
... then call the shell script with system() :)
> I don't know how to convert linux commands to C++ functions.
https://linux.die.net/man/1/pwd

Scroll down to
See Also
getcwd(3)


Where a command is just a simple(ish) wrapper around some API, the manual page usually tells you some similar section 2 or section 3 function you can use in your code.

If you're not sure what you're after, apropos is your friend.
$ apropos directory
alphasort (3)        - scan a directory for matching entries
basename (1)         - strip directory and suffix from filenames
bindtextdomain (3)   - set directory containing message catalogs
chacl (1)            - change the access control list of a file or directory
chdir (2)            - change working directory
chroot (2)           - change root directory
chroot (8)           - run command or interactive shell with special root directory
closedir (3)         - close a directory
cups-files.conf (5)  - file and directory configuration file for cups
dbus-cleanup-sockets (1) - clean up leftover sockets in a directory
depmod.d (5)         - Configuration directory for depmod
dh_installdeb (1)    - install files into the DEBIAN directory
dh_testdir (1)       - test directory before building Debian package
dir (1)              - list directory contents
dirfd (3)            - get directory stream file descriptor
dirsplit (1)         - splits directory into multiple with equal size
execveat (2)         - execute program relative to a directory file descriptor
fchdir (2)           - change working directory
fdopendir (3)        - open a directory
File::BaseDir (3pm)  - Use the Freedesktop.org base directory specification
File::Listing (3pm)  - parse directory listing
find (1)             - search for files in a directory hierarchy
futimesat (2)        - change timestamps of a file relative to a directory file descriptor
get_current_dir_name (3) - get current working directory
getcwd (2)           - get current working directory
getcwd (3)           - get current working directory
// snipped
Topic archived. No new replies allowed.