CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    Oct 2007
    Posts
    63

    saving the o/p of system() in a string

    the system() function executes an OS command . it returns 0 if command is executed else it returns some integer.

    as an example :
    Code:
    #include <stdlib.h>
    
    int main()
    {
      system("date");
    }
    this gives me the date on execution.
    how do i save the string generated by date command into a character array variable(like char buf[50])?

    the only thing that i could think of so far is to redirect the stream, to a file at OS level and then open that file and read from it.

    like this
    Code:
    #include <stdlib.h>
    #include<stdio.h>
    
    int main()
    {
      system("date >f1");
      FILE *fp;
      char ch;
      fp=fopen("f1","r");
      while((ch=fgetc(fp))!=EOF)
      {
         printf("%c",ch);
       }
       fclose(fp);
       remove("f1");
    }
    this obviously is pretty lame.
    i would want to redirect the output(not the return variable) to a string(char array) variable directly....I want to use C ... instead of using OS
    Last edited by creeping death; March 6th, 2009 at 12:19 PM.

  2. #2
    Join Date
    Nov 2003
    Posts
    1,902

    Re: saving the o/p of system() in a string

    See popen() in your manual.

    gg

  3. #3
    Join Date
    Oct 2007
    Posts
    63

    Re: saving the o/p of system() in a string

    Is popen() available on a ported-gcc compiler on windows too?

  4. #4
    Join Date
    Nov 2003
    Posts
    1,902

    Re: saving the o/p of system() in a string

    Should be available to both Cygwin and MinGW.

    gg

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured