redirect console output to a String?

This is a discussion on redirect console output to a String? within the C Programming forums, part of the General Programming Boards category; Hi all, I am new to C and am writing a small application in C. One part of it is ...

  1. #1
    Registered User
    Join Date
    Feb 2008
    Posts
    5

    redirect console output to a String?

    Hi all,

    I am new to C and am writing a small application in C. One part of it is to fire some system commands and store the output in some variable.

    A small example of what i wanna do:

    Code:
     
    int main(void)
    {
          return system("ifconfig | grep 'inet addr'");
    }
    now i want to store the output of system() in a string (or an array of String). I tried out many things but nothing seems to work.

    Will be very happy if somebody could help me out in this

  2. #2
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    you can use the POSIX standard (I think) popen command, which lets you redirect input and output of a program to your program. But it's not standard C or C++.

    Google for it.

  3. #3
    Registered User
    Join Date
    May 2006
    Posts
    57
    nice place to look for that kind of stuff(they have examples alot of times!)

    http://www.opengroup.org/onlinepubs/...ons/popen.html

  4. #4
    Registered User
    Join Date
    Feb 2008
    Posts
    5
    Thanks a lot for your help. I have finally constructed the following code snipet which works fine. As i am a beginner i post it here hoping for the betterment of the code.

    Code:
            FILE *pipein_fp;
            char readbuf[80];
    
            if (( pipein_fp = popen("xyz", "r")) == NULL)
            {
                    perror("popen");
                    exit(1);
            }
    
            while(fgets(readbuf, 80, pipein_fp))
                    printf(readbuf, "%c");
    
            pclose(pipein_fp);
    
            return(0);

  5. #5
    Registered User
    Join Date
    Feb 2008
    Posts
    5

    Find IP of the host

    I was interested to know if there is some more elegent way to get the IP Address of the host other then firing ifconfig through popen and then parsing the string to get the IP.

    I googled around and found out funtions like gethostname(), gethostbyname() etc. My (little) understanding which i got after hours of googling was that these functions try to find out the IP through some DNS. Maybe i am wrong.

    If not, is there any other way by which i can get the IP of the host.

    If i am wrong can someone explain (with an example) how these functions would work.

    Thanks a Lot in Advance.

  6. #6
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    Quote Originally Posted by TwistedMetal View Post
    Code:
    printf(readbuf, "%c");
    Code:
    printf("%s", readbuf);
    See the example in zxcv's link.

  7. #7
    Registered User
    Join Date
    Feb 2008
    Posts
    5
    Thanks Robwhit.

    Can somebody provide some help regarding finding the IP Address please.

  8. #8
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    gethostbyname etc are the functions you are looking for...
    so search for the sample using these functions, do not forget - this sample should use the Winsock 2 dll (ws_32.dll if I remember correctly the spelling of the name - msnd will give you correct lib name to include in a project)
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  9. #9
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,274
    Quote Originally Posted by TwistedMetal View Post
    Thanks Robwhit.

    Can somebody provide some help regarding finding the IP Address please.
    You could pass the current hostname to gethostbyname() but this is not guaranteed to be the actual address of the hardware interface (for instance, if the hostname is misconfigured). How you get this address is going to vary from platform to platform. Calling ifconfig is not actually a terribly bad way to do it. ifconfig also gives you tons of other info as well.

  10. #10
    Registered User
    Join Date
    Feb 2008
    Posts
    5

    *** stack smashing detected ***

    Hi all,

    Does anybody has some idea why this piece of code

    Code:
    int setIP(char fName[100])
    {
            FILE *pipein_fp;
            char readbuf[100];
            char cmd[] = "vmware-cmd ";
            char var[] = " setguestinfo ip 68.180.53.46";
            strcat(cmd, fName);
            strcat(cmd, var);
    
            if (( pipein_fp = popen(cmd, "r")) == NULL)
            {
                    perror("popen");
                    exit(1);
            }
    
            while(fgets(readbuf, 100, pipein_fp))
                    printf("%s", readbuf);
    
            pclose(pipein_fp);
    
            printf("Server IP for VM set successfully");
            return(0);
    
    }
    is generating the following error

    Code:
    setguestinfo(ip 68.180.53.46) = 1
    *** stack smashing detected ***: ./TCPEchoServer terminated
    Server IP for VM set successfullyAborted (core dumped)
    Thanks

  11. #11
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    strcat(cmd, fName); you do not have place in the cmd to add there even one byte
    change cmd declaration so the buffer be big enough to store the resulting string
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 8
    Last Post: 04-25-2008, 03:45 PM
  2. Inheritance Hierarchy for a Package class
    By twickre in forum C++ Programming
    Replies: 7
    Last Post: 12-08-2007, 04:13 PM
  3. Replies: 4
    Last Post: 03-29-2006, 04:36 PM
  4. Problem with string output...
    By Lau in forum C Programming
    Replies: 6
    Last Post: 11-20-2003, 10:51 AM
  5. Warnings, warnings, warnings?
    By spentdome in forum C Programming
    Replies: 25
    Last Post: 05-27-2002, 07:49 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21