Read command output file 
Author Message
 Read command output file

Hi
Below is c program , I want to read the command line output into string
variable. How to write the code ?
Eric

/* xidle.cpp
              02/03/02
              02/07/02
*/
#include <stdio.h>
#include <string>
#include <fstream>
#include <iostream>
#include <iomanip>

int main()
{
  string buffer;
  ifstream inFile;

  FILE *cmdp=popen("ps -ef| grep nobody","r");
  if (!cmdp) {
     perror("popen");
     return 1;
  }

  inFile.open(????);    // How to import command line ???

  while (!inFile.eof())
  {
   inFile >> buffer;
   cout  << buffer <<endl;

  }

  return 0;

Quote:
}



Mon, 26 Jul 2004 22:27:33 GMT  
 Read command output file

Quote:

> Hi
> Below is c program , I want to read the command line output into string
> variable. How to write the code ?
> Eric

> /* xidle.cpp
>               02/03/02
>               02/07/02
> */

If it is a C program, why do you call it xidle.cpp (.cpp is a C++
extension)?

Quote:
> #include <stdio.h>
> #include <string>
> #include <fstream>
> #include <iostream>
> #include <iomanip>

> int main()
> {
>   string buffer;
>   ifstream inFile;

>   FILE *cmdp=popen("ps -ef| grep nobody","r");
>   if (!cmdp) {
>      perror("popen");
>      return 1;
>   }

>   inFile.open(????);    // How to import command line ???

>   while (!inFile.eof())
>   {
>    inFile >> buffer;
>    cout  << buffer <<endl;

Where did you get this code, I'm fairly new to C but this looks like C++.
Quote:

>   }

>   return 0;

> }

Here is a example,

void main(void)
{
        FILE *stream;
        char buffer[500];
        int i;

        stream = popen("ps -ef| grep nobody","r");

        /*stream is a pointer to a FILE descriptor*/

        if (!stream)
        {
                perror("popen");
                exit(1);
        }

        i = 0;
        /* fgetc gets one character at a time and saves it in buffer */

        while (!feof(stream))
        {
                buffer[i] = fgetc(stream);
                ++i;
        }
        buffer[i-1] = '\0';                            

/* '\0' means end of string, this way you won'thave garbage inyour buffer */

        printf("%s\n", buffer);
        return 0;

Quote:
}

here are some URL's to some tutorials:
http://www.cm.cf.ac.uk/Dave/C/CE.html
http://www.eskimo.com/~scs/C-faq.top.html


Mon, 26 Jul 2004 23:25:02 GMT  
 Read command output file

    el> Hi
    el> Below is c program , I want to read the command line output into string
    el> variable. How to write the code ?
    el> Eric

No, sorry, but it's a C++ program.  Try comp.lang.c++, down the hall.

[...]

Cheers
Kevin

--
He that breaks a thing to find out what it is has left the path of
wisdom                                         -- Gandalf the Grey



Mon, 26 Jul 2004 23:29:52 GMT  
 Read command output file

Quote:


> > Below is c program , I want to read the command line output into string
> > variable. How to write the code ?

> > /* xidle.cpp
> >               02/03/02
> >               02/07/02
> > */
> If it is a C program, why do you call it xidle.cpp (.cpp is a C++
> extension)?

Because it actually _is_ a C++ program:

Quote:
> > #include <stdio.h>
> > #include <string>
> > #include <fstream>
> > #include <iostream>
> > #include <iomanip>

- all but the first of these headers do not exist in C, though the
  second one is probably similar or equal to C's <string.h>;

Quote:
> > int main()
> > {
> >   string buffer;
> >   ifstream inFile;

> >   FILE *cmdp=popen("ps -ef| grep nobody","r");

- popen() is certainly not ISO C, and quite possibly not ISO C++,
  either. It _is_ in POSIX, but that's still off-topic here;

Quote:
> >   if (!cmdp) {
> >      perror("popen");
> >      return 1;
> >   }

> >   inFile.open(????);    // How to import command line ???

- this is quite probably C++; while function pointers inside structures
  are possible in C, this line probably intends to call a C++ object
  method;

Quote:
> >   while (!inFile.eof())
> >   {
> >    inFile >> buffer;
> >    cout  << buffer <<endl;
> Where did you get this code, I'm fairly new to C but this looks like C++.

- it is C++; this is a C++ way of doing in- and output.

Quote:
> >   }

> >   return 0;

> > }

So, several indications of the fact that this is _not_ C code, but C++
code, and should have been posted to comp.lang.c++.

Quote:
> Here is a example,

Which is C, not C++, but far from perfect itself:

- you forget to #include the necessary headers, invoking undefined
  behaviour in at least one case;

Quote:
> void main(void)

- this is an unportable way of declaring main() in ISO C; in C, main()
  _always_ returns an int, even if it's always 0;

Quote:
> {
>         FILE *stream;
>         char buffer[500];
>         int i;

>         stream = popen("ps -ef| grep nobody","r");

- popen() is still not ISO C, but POSIX;

Quote:
>         /*stream is a pointer to a FILE descriptor*/

>         if (!stream)
>         {
>                 perror("popen");
>                 exit(1);

- 1 is not a portable argument value for exit(), or return value for
  main(); #include <stdlib.h> and use exit(EXIT_FAILURE); instead;

Quote:
>         }

>         i = 0;
>         /* fgetc gets one character at a time and saves it in buffer */

>         while (!feof(stream))

- this is not a very good way to write a read loop - read the FAQ
  (question 12.2) to find out why;

Quote:
>         {
>                 buffer[i] = fgetc(stream);
>                 ++i;
>         }
>         buffer[i-1] = '\0';                            

- you don't check that you stay within the bounds of your buffer,
  possibly invoking undefined behaviour and leaving yourself open to
  anything from core dumps to Internet Worms;

Quote:
> /* '\0' means end of string, this way you won'thave garbage inyour buffer */

>         printf("%s\n", buffer);
>         return 0;
> }

Anyway, presuming stream points to a valid, opened input stream, no
matter how you got it (e.g., using fopen() or some kind of extension
like popen(), as long as it's a normal FILE *), you can simply use this:

  {
    char buffer[MAX_LINE];

    /* Open the stream before this line... */
    fgets(buffer, sizeof buffer, stream);
  }

It's that simple.

Richard



Mon, 26 Jul 2004 23:47:26 GMT  
 Read command output file

Quote:

> > > #include <stdio.h>
> > > #include <string>
> > > #include <fstream>
> > > #include <iostream>
> > > #include <iomanip>

> - all but the first of these headers do not exist in C, though the
>   second one is probably similar or equal to C's <string.h>;

Actually, it's nothing like <string.h>. It is the header for the C++
string class. This tends to cause much confusion.

In fact, <string.h> is still a valid C++ header, although deprecated in
favor of <cstring>.

Brian Rodenborn



Tue, 27 Jul 2004 05:49:01 GMT  
 
 [ 5 post ] 

 Relevant Pages 

1. reading output after run command?

2. reading output from "exec"ed shell command

3. read a file than produce 2 output files(HELP)

4. Reading a file & output a file

5. read a file than produce 2 output files(HELP)

6. Determining output file name based on preprocessor commands?

7. fgets reading a configuration file: compiles OK but output wrong

8. Read and Output RTF file

9. Help! reading from a file and output

10. ANSI C Run Time Erroe to output data to the output file

11. VC 5 TRACE output to a file instead of the output window

12. MFC CDC HTML Output (ala Meta File Output)

 

 
Powered by phpBB® Forum Software