I am executing a system() function which returns me a file name. Now I dont want to display the output on the screen(ie the filename) or pipe to a newfile. I just want to store it in a variable. is that possible? if so, how? thanks
-
Which command do you execute in `system()`? – Nawaz May 07 '11 at 07:10
-
See this topic : [Run a System Command and Get Output?](http://stackoverflow.com/questions/646241/c-run-a-system-command-and-get-output) – Nawaz May 07 '11 at 07:15
-
1possible duplicate of [How can I run an external program from C and parse its output?](http://stackoverflow.com/questions/43116/how-can-i-run-an-external-program-from-c-and-parse-its-output) – Bo Persson May 07 '11 at 07:18
4 Answers
A single filename? Yes. That is certainly possible, but not using system()
.
Use popen()
. This is available in c and c++, you've tagged your question with both but are probably going to code in one or the other.
Here's an example in C:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main()
{
FILE *fpipe;
char *command = "ls";
char c = 0;
if (0 == (fpipe = (FILE*)popen(command, "r")))
{
perror("popen() failed.");
exit(EXIT_FAILURE);
}
while (fread(&c, sizeof c, 1, fpipe))
{
printf("%c", c);
}
pclose(fpipe);
return EXIT_SUCCESS;
}

- 136,902
- 23
- 188
- 247
-
This will cause undefined behavior at the `printf` call, since `fread()` does not null terminate the string stored in `line`. – Mark Lakata Jul 31 '14 at 20:32
-
Order of fread parameters are wrong. 2nd and 3rd parameters are swapped. It wont work correctly. – vicky Nov 26 '14 at 12:00
-
@vicky: Thanks for highlighting that this wouldn't work with output under 256 chars. Fixed. – johnsyweb Nov 27 '14 at 02:44
-
-
@Amir 9 years after writing this answer I am as puzzled as you are. The main function should return EXIT_SUCCESS: https://stackoverflow.com/a/8696712 – johnsyweb Apr 12 '20 at 22:00
Well,There is one more easy way by which you can store command output in a file which is called redirection method. I think redirection is quite easy and It will be useful in your case.
so For Example this is my code in c++
#include <iostream>
#include <cstdlib>
#include <string>
using namespace std;
int main(){
system("ls -l >> a.text");
return 0;
}
Here redirection sign easily redirect all output of that command into a.text file.

- 1,280
- 1
- 13
- 17
Here is my C++ implementation, which redirects system()
stdout to a logging system. It uses GNU libc's getline()
. It will throw an exception if it can't run the command, but will not throw if the command runs with non-zero status.
void infoLogger(const std::string& line); // DIY logger.
int LoggedSystem(const string& prefix, const string& cmd)
{
infoLogger(cmd);
FILE* fpipe = popen(cmd.c_str(), "r");
if (fpipe == NULL)
throw std::runtime_error(string("Can't run ") + cmd);
char* lineptr;
size_t n;
ssize_t s;
do {
lineptr = NULL;
s = getline(&lineptr, &n, fpipe);
if (s > 0 && lineptr != NULL) {
if (lineptr[s - 1] == '\n')
lineptr[--s ] = 0;
if (lineptr[s - 1] == '\r')
lineptr[--s ] = 0;
infoLogger(prefix + lineptr);
}
if (lineptr != NULL)
free(lineptr);
} while (s > 0);
int status = pclose(fpipe);
infoLogger(String::Format("Status:%d", status));
return status;
}

- 19,989
- 5
- 106
- 123