need help getting the results of the system function into a variable

Discussion in 'C Programming' started by SF, Feb 27, 2006.

  1. SF

    SF Guest

    Hello All,

    In a windows C learning project I am wokring on I use the system
    function to run a command, I want to suck the results into a vairable.
    The system function seems to only return an int. I would like to know
    how could this is done? I can see only one way currently and I know
    that there must be a better method. Right now I would be tempted to
    pipe the output of the command to a temp file and then get the data
    from the file. That seems really dirty to me, can you suggest a better
    way?

    Much thanks in advance.
     
    SF, Feb 27, 2006
    #1
    1. Advertising

  2. In article <>,
    SF <> wrote:
    >In a windows C learning project I am wokring on I use the system
    >function to run a command, I want to suck the results into a vairable.
    >The system function seems to only return an int. I would like to know
    >how could this is done?


    There is no way to do it using only standard C.

    >I can see only one way currently and I know
    >that there must be a better method. Right now I would be tempted to
    >pipe the output of the command to a temp file and then get the data
    >from the file. That seems really dirty to me, can you suggest a better
    >way?


    pipes are operating system-specific extensions, outside the
    control of standard C. Standard C does not even define the result
    of using system(): it just says that it exists and that the result
    is operating-system dependant.

    Possibly you meant "redirect" instead of "pipe", but the ability to
    redirect system() output into a file is again operating-system dependant
    (though it's probably available on more operating systems than support
    pipes.)

    For what it's worth, the usual POSIX of handling this is to use
    popen(), but if you want to know what facilities are available in
    Windows, you should check a Windows programming newsgroup.

    --
    I was very young in those days, but I was also rather dim.
    -- Christopher Priest
     
    Walter Roberson, Feb 27, 2006
    #2
    1. Advertising

  3. SF

    SF Guest

    Thanks for the reply,

    I am using redirect rather than a pipe. I guess I did not use the right
    term there.

    Are you aware of a better function then system() to run a shell
    command? one that could return the output rather than an int.

    Thanks for your help.
     
    SF, Feb 27, 2006
    #3
  4. In article <>,
    SF <> wrote:
    >Are you aware of a better function then system() to run a shell
    >command? one that could return the output rather than an int.


    As I indicated, system() is all you get in standard C, and anything
    else should be asked about in a newsgroup specific to your
    operating system. When you post there, be sure to specify your
    version of Windows and Service Pack(s), as the answer might be
    dependant upon exactly what you have installed.
    --
    There are some ideas so wrong that only a very intelligent person
    could believe in them. -- George Orwell
     
    Walter Roberson, Feb 27, 2006
    #4
  5. "SF" <> wrote in message
    news:...
    > Hello All,
    >
    > In a windows C learning project I am wokring on I use the system
    > function to run a command, I want to suck the results into a vairable.
    > The system function seems to only return an int. I would like to know
    > how could this is done? I can see only one way currently and I know
    > that there must be a better method. Right now I would be tempted to
    > pipe the output of the command to a temp file and then get the data
    > from the file. That seems really dirty to me, can you suggest a better
    > way?
    >
    > Much thanks in advance.
    >

    If you have a POSIX-compliant system, use popen()
    --
    Fred L. Kleinschmidt
    Boeing Associate Technical Fellow
    Technical Architect, Software Reuse Project
     
    Fred Kleinschmidt, Feb 27, 2006
    #5
  6. SF

    Default User Guest

    SF wrote:

    > Are you aware of a better function then system() to run a shell
    > command? one that could return the output rather than an int.


    Anything else would be platform specific. You need to find a newsgroup
    dedicated to your implementation or (perhaps) OS. Also, see the
    information in my .sig below.


    Brian
    --
    Please quote enough of the previous message for context. To do so from
    Google, click "show options" and use the Reply shown in the expanded
    header.
     
    Default User, Feb 27, 2006
    #6
  7. SF

    Ron Lima Guest

    > Are you aware of a better function then system() to run a shell
    > command? one that could return the output rather than an int.


    Have you tried CreateProcess system call to do so? I know that this
    system call returns several information about the process that you
    start with it. This is something specific for Windows platform and is
    not STDC at all.

    Check this link:
    http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dllproc/base/child_processes.asp

    This has several information about creating processes on windows
    platforms.

    Hope that it helps.

    Ron
     
    Ron Lima, Feb 28, 2006
    #7
  8. Re: need help getting the results of the system function into avariable

    "Ron Lima" <> writes:
    >> Are you aware of a better function then system() to run a shell
    >> command? one that could return the output rather than an int.

    >
    > Have you tried CreateProcess system call to do so? I know that this
    > system call returns several information about the process that you
    > start with it. This is something specific for Windows platform and is
    > not STDC at all.

    [snip]

    And if this information had been posted to a Windows-specific
    newsgroup, the experts there could post any necessary corrections.
    As it is, most of us have no idea whether the suggested solution will
    actually work, or whether something else might be better.

    If you want to discuss Windows-specific interfaces, please don't do it
    here.

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
    We must do something. This is something. Therefore, we must do this.
     
    Keith Thompson, Feb 28, 2006
    #8
  9. SF

    Guest

    Its execpls( ) ... this works well on unix operating systems...

    Jitendra
     
    , Feb 28, 2006
    #9
  10. SF

    Guest

    sorry .... its execlp ( ) ...

    thanks
    jitendra
     
    , Feb 28, 2006
    #10
  11. Re: need help getting the results of the system function into avariable

    writes:
    > sorry .... its execlp ( ) ...


    And it's off-topic; try comp.unix.programmer.

    And please read <http://cfaj.freeshell.org/google/>.

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
    We must do something. This is something. Therefore, we must do this.
     
    Keith Thompson, Feb 28, 2006
    #11
  12. SF

    SF Guest

    Keith Thompson wrote:
    > writes:
    > > sorry .... its execlp ( ) ...

    >
    > And it's off-topic; try comp.unix.programmer.
    >
    > And please read <http://cfaj.freeshell.org/google/>.
    >
    > --



    Thanks for all the suggestions, I have posted this in a win32
    programming groups and they suggested something similar.

    Sorry if this was off topic as a few members seem to indicate, I
    thought this was a general C discussion forum rather than related to
    any particular OS. I was not aware that my topic was outside the scope
    of standard C development.

    BTW, what is the topic of this group so I can use it better?


    For those who suggested the createprocess function, I have seen that on
    msdn but I have not found a working example source. I am still
    learning, if you could show me an example or link to one it would help
    me a lot.

    Thank you, sorry again for the O/T.
     
    SF, Feb 28, 2006
    #12
  13. SF wrote:
    > Keith Thompson wrote:
    > > writes:
    > > > sorry .... its execlp ( ) ...

    > >
    > > And it's off-topic; try comp.unix.programmer.
    > >
    > > And please read <http://cfaj.freeshell.org/google/>.
    > >
    > > --

    >
    >
    > Thanks for all the suggestions, I have posted this in a win32
    > programming groups and they suggested something similar.
    >
    > Sorry if this was off topic as a few members seem to indicate, I
    > thought this was a general C discussion forum rather than related to
    > any particular OS. I was not aware that my topic was outside the scope
    > of standard C development.


    This group is specifically /not/ about any particular OS, rather it
    concerns only C language as defined by the Standard.

    > BTW, what is the topic of this group so I can use it better?


    A good place to start is http://www.clc-wiki.net/wiki/Main_Page
    Also, lurking for a while helps a lot (you can use Google to quick-lurk
    a few months worth of posts).

    --
    BR, Vladimir
     
    Vladimir S. Oka, Feb 28, 2006
    #13
  14. Re: need help getting the results of the system function into avariable

    In article <>,
    Keith Thompson <> wrote:
    > writes:
    >> sorry .... its execlp ( ) ...


    >And it's off-topic; try comp.unix.programmer.


    Besides being off-topic it isn't the right answer for any version
    of Windows -or- Unix.
    --
    Okay, buzzwords only. Two syllables, tops. -- Laurie Anderson
     
    Walter Roberson, Feb 28, 2006
    #14
  15. SF

    SF Guest

    SF wrote:
    > Keith Thompson wrote:
    > > writes:
    > > > sorry .... its execlp ( ) ...

    > >
    > > And it's off-topic; try comp.unix.programmer.
    > >
    > > And please read <http://cfaj.freeshell.org/google/>.
    > >
    > > --

    >
    >
    > Thanks for all the suggestions,
    > For those who suggested the createprocess function, I have seen that on
    > msdn but I have not found a working example source. I am still
    > learning, if you could show me an example or link to one it would help
    > me a lot.
    >
    > Thank you, sorry again for the O/T.




    The example here works for me however I am not certain how I can get
    the results into a variable.

    http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dllproc/base/creating_processes.asp

    I would like to be able to run a command line program and get the
    results. Not simply process information but the output. Any command
    that would output text then exit such as netstat, nslookup or even dir.
    I know these questions are windows specific, but I think that it is a C
    realted question on how to interface with a function and get its info
    into a variable. This is more of a learning project for me rather than
    trying to solve any particular problem.

    I am still way off topic, I apologize.

    SF
     
    SF, Feb 28, 2006
    #15
  16. SF wrote:

    <snip "way off topic" stuff>

    > I am still way off topic, I apologize.


    So why come back here, and not go to one of the groups dealing with
    your specific platform?

    --
    BR, Vladimir
     
    Vladimir S. Oka, Feb 28, 2006
    #16
  17. SF wrote:
    > I know these questions are windows specific, but I think that it is a C
    > realted question on how to interface with a function and get its info
    > into a variable.


    This is on topic, but if you have to ask this sort of question, you're
    better off re-reading your C textbook. Here's an example, though:

    int a_func(void)
    {
    return 42;
    }

    void b_func(int *x)
    {
    *x = 42;
    return;
    }

    int main(void)
    {
    int a_variable;

    a_variable = a_func();

    b_func(&a_variable);

    return 0;
    }

    If you're asking about a specific non-standard function, you are off
    topic here.

    --
    BR, Vladimir
     
    Vladimir S. Oka, Feb 28, 2006
    #17
  18. SF

    SF Guest

    Vladimir S. Oka wrote:
    > SF wrote:
    >
    > <snip "way off topic" stuff>
    >
    > > I am still way off topic, I apologize.

    >
    > So why come back here, and not go to one of the groups dealing with
    > your specific platform?
    >
    > --
    > BR, Vladimir


    Well, since I already have an off topic post where people are nice
    enough to reply, adding something to that post is not the worst thing
    in the world is it?

    I do have this posted in other groups. I also said how it could be
    viewed as a regular C question since I am not really asking anything
    about windows itself but on using a popular API written in C.

    Also I figure that my on subject addition to an off topic thread is as
    harmless as your off subject question in that same thread.

    Cheers.
     
    SF, Feb 28, 2006
    #18
  19. In article <>,
    SF <> wrote:

    >I would like to be able to run a command line program and get the
    >results. Not simply process information but the output. Any command
    >that would output text then exit such as netstat, nslookup or even dir.
    >I know these questions are windows specific, but I think that it is a C
    >realted question on how to interface with a function and get its info
    >into a variable.


    The C way to "interface with a function and get its info into a
    variable" is to store the return value of the function. If the
    information you need is not in the return value of the function
    (e.g. because you want the I/O output performed by the function)
    then the matter is no longer a C matter but a system-specific matter.


    I will have some pity on you and let you know that there isn't
    any standard Unix or Windows call equivilent in structure to

    variable := result_of_command("command string");

    You could synthesize such a routine yourself, but watch out for the
    possibility that the output is gigabytes long or even indefinitely
    long...

    The calls that are available all either do not capture the output
    (not what you want) or else capture it in a form that you have to fread()
    or read().

    Older versions of Windows literally did do what you've been trying to
    avoid -- redirected the output to a file and read the file back. Newer
    versions of Windows provide Windows-specific mechanisms that skip
    actually writing the output to disk. But those are topics for
    Windows-specific newsgroups.
    --
    I was very young in those days, but I was also rather dim.
    -- Christopher Priest
     
    Walter Roberson, Feb 28, 2006
    #19
  20. SF

    Al Balmer Guest

    On 28 Feb 2006 07:28:06 -0800, "SF" <> wrote:

    >Sorry if this was off topic as a few members seem to indicate, I
    >thought this was a general C discussion forum rather than related to
    >any particular OS. I was not aware that my topic was outside the scope
    >of standard C development.
    >
    >BTW, what is the topic of this group so I can use it better?


    The topic is the C language itself, not any particular implementation
    or platform.

    Your original question was fine (Is there a way to do it?) and
    Walter's original response was correct (Not in standard C). Walter was
    also correct (and helpful) in suggesting a different newsgroup for
    information about your specific problem.

    Things went off-topic from there.

    --
    Al Balmer
    Sun City, AZ
     
    Al Balmer, Feb 28, 2006
    #20
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    2
    Views:
    485
    Peter Hansen
    May 7, 2005
  2. Replies:
    4
    Views:
    370
  3. fmbright
    Replies:
    8
    Views:
    227
  4. David Resnick
    Replies:
    4
    Views:
    93
    C.DeRykus
    May 20, 2010
  5. SteveB
    Replies:
    1
    Views:
    66
    Erwin Moller
    May 30, 2006
Loading...

Share This Page