I have been trying for hours to create a program that outputs something like command[/home/nerdofcode/]:
by running something like:
printf("Command[", system("pwd"),"]: ");
…
But the problem I am receiving is that when I go to type input, it starts typing at command[...
and then once I click enter, it finally outputs the system("pwd");
…
Technical Info
I am using the system()
function to execute a system command on Linux.
Advertisement
Answer
To use printf correctly for a null-terminated string you need to change the parameters:
printf("Command[%s]: ", string_with_result);
In order to get the string_with_result
correctly, you need to study the way system()
works in your environment. Its return value is implementation specific and therefor does not allow to answer with code which does what you want.
char * string_with_result; /* pointer to null-terminated sequence of char */
This is the declaration for the string result to be used in printf as proposed above.
In case you want to just get the result, but do not insist on using system()
check this StackOverflow question and accepted answer:
‘pwd’ to get path to the current file
This Q/A might be the way on a “unix-ish” environment to actually use system()
, it uses popen()
:
C: Run a System Command and Get Output?