-3

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.

Community
  • 1
  • 1
NerdOfCode
  • 215
  • 2
  • 11
  • 1
    Did you read the spec for `printf()` closely? Your code does not match the format. – Yunnosch Mar 13 '18 at 07:14
  • Could you clarify, please? – NerdOfCode Mar 13 '18 at 07:15
  • 3
    `system()` does not return a string with the output. – JFMR Mar 13 '18 at 07:16
  • The return value of `system()` is implementation specific. You need first to find out how to get at the resulting output. Let us know about that in order to possibly get an answer which does what you want. – Yunnosch Mar 13 '18 at 07:20
  • In case you want to just get the result, but do not insist on using `system()` check this out: https://stackoverflow.com/questions/16285623/pwd-to-get-path-to-the-current-file – Yunnosch Mar 13 '18 at 07:30
  • Thanks, @Yunnosch! This actually helped a good bit! – NerdOfCode Mar 13 '18 at 07:34
  • I think you have downvotes for misusing printf. Maybe stress the interesting, helpful and not too much lack-of-research-based part on using `system()`. To do so please edit your question to describe what problems remain even after changing to e.g. `printf("Command[%s]: ", system("pwd"));`. I.e. focus on the intricacies of the `system()` call. In order to protect existing answers (admittedly one of them is mine), please phrase e.g. "Even if I change to correct use of printf, as recommended in early helpful answers, I get the problem that....". – Yunnosch Mar 13 '18 at 15:39
  • No matter the case of downvotes or not, I have learned the cause of my Problem and many of my problems and from now on I will try and read some more of the manual pages before posting here. – NerdOfCode Mar 13 '18 at 15:53

2 Answers2

1

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?

Yunnosch
  • 26,130
  • 9
  • 42
  • 54
1

printf does not work as a "concat" operation of comma separated strings. It rather has a format string with placeholders and the arguments for the placeholders.

So you could write:

char *s1 = "hello", *s2 = "world"; 
printf("%s %s", s1, s2);

But the following code will not concat the both strings; it will rather treat s1 as a format string (without placeholders) and will ignore the argument:

char *s1 = "hello", *s2 = "world"; 
printf(s1, s2);

Note further that int system(const char* cmd) does not return a string, which you could use in printf then, at all. So I'd recommend to write.

printf("Command[");
system("pwd");
printf("]: ");

This should work on console as long as system-command and your programsstdout` target the same output stream.

Stephan Lechner
  • 34,891
  • 4
  • 35
  • 58