12

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

IITian
  • 121
  • 1
  • 1
  • 3
  • 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
  • 1
    possible 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

5 Answers5

11

A single filename? Yes. That is certainly possible, but not using system().

Use popen(). This is available in and , 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;
}
johnsyweb
  • 129,524
  • 23
  • 177
  • 239
4

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.

nitin
  • 1,262
  • 1
  • 8
  • 17
3

You can use popen(3) and read from that file.

FILE *popen(const char *command, const char *type);

So basically you run your command and then read from the FILE returned. popen(3) works just like system (invokes the shell) so you should be able to run anything with it.

cnicutar
  • 172,020
  • 25
  • 347
  • 381
2

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;
}
Mark Lakata
  • 19,008
  • 5
  • 97
  • 119
0

I agree with nitin that an easy way is to just redirect it into an intermediary plaintext file. Its what I am doing, except I use it to output stuff my server receives into a .txt file.

It is also really useful for simply storing encrypted UDP packets, so they can be decrypted at a later time.

Using a .txt file to store the output of a system() function is recommended for applications where they can wait for a second or two to read/write from files, but if you are doing more intensive applications that need to be done instantaneously, maybe consider piping it directly into a program using popen();

Dharman
  • 26,923
  • 21
  • 73
  • 125
bilberto
  • 1
  • 4