Hi everyone,

I want to copy the output of a program with another program so I can write it to a file. I'm running Ubuntu Linux, and I'm trying to see what my system is like with hwinfo. The list is too long, though, so I need to put it into a file. Does anyone know how to do this? Or do any other Ubuntu fans know any way to do this just in code?

If you can find out how to do this, it'll really help me because I also want to do something like this in with libcURL. I can just use the C file, but I like C++ much better.

Thanks!

If the output is to the console/terminal. The you can do this

./program input_data > outputFile.txt 2> errorFile.txt

Note that I created and output file for the normal output and an error txt file for errors. You might want to put them all together with

./program input_data > outputFile.txt 2>&1

which just means put the output to outputFile.txt and then write the errors to the same place as the standard output.

Note that works on bash shells [default for ubuntu and most other linux distributions]


---------

Note if you actually want to pass the output to a file and process the data, you can pipe it and
use popen and associated pipes. You will need to include the unistd.h file and you can create a
file pipe e.g.

FILE* inputPipe;
 
inputPipe=popen("myProgram","r");

if (inputPipe == NULL)
   { // Deal with error }

char txt[MAXCHAR];
while (fgets(txt, MAXCHAR, inputPipe) != NULL)
   {
      std::cout<<"Data == "<<txt<<std::endl;
   }

There are more C++ ways to do this with boost and others.. but I am sorry I rarely do that so you will have to wait for a more experienced member to help you [and me] out.

Thanks! This will really help me. Sorry I couldn't reply sooner, our internet was down.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.