The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > High Level Programming
Google UNIX.COM


High Level Programming Post questions about C, C++, Java, SQL, and other programming languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
help me to answer this lo-lp-kl Shell Programming and Scripting 0 05-25-2008 07:45 PM
Answer them if u can... dreambig UNIX for Dummies Questions & Answers 5 12-07-2007 07:14 AM
Is sed the answer? LaLonde Shell Programming and Scripting 7 08-29-2007 04:45 PM
can any one answer ???? mobile01 High Level Programming 2 11-30-2006 08:32 AM
get system() answer ? Sven28 High Level Programming 4 11-23-2001 10:15 AM

Reply
 
Submit Tools LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 05-12-2008
Registered User
 

Join Date: May 2008
Posts: 4
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit! Stumble this Post!Spurl this Post!
How do I get system answer in c

How do I get the answer of a system call that is printed in the terminal?

for example:

I execute system("pwd");
and get the answer /home/user/

But because I need to send this result to somewhere, I need to store it in a string.

Thanks in advance.
Reply With Quote
Forum Sponsor
  #2 (permalink)  
Old 05-12-2008
Registered User
 

Join Date: Oct 2007
Location: USA
Posts: 378
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit! Stumble this Post!Spurl this Post!
popen()

Use popen() instead.
Reply With Quote
  #3 (permalink)  
Old 05-12-2008
Registered User
 

Join Date: May 2008
Posts: 4
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit! Stumble this Post!Spurl this Post!
Instead of what?
I do not create or open a file. All I need is to get the answer of any command from the command line into a string, "pwd" is just an example.

Can you please give a simple example?

Situation: I have

char command[50] = "ls";

system(command); // executes the string

___________________?

Last edited by eldaran; 05-12-2008 at 05:27 PM.
Reply With Quote
  #4 (permalink)  
Old 05-12-2008
Registered User
 

Join Date: Feb 2008
Posts: 18
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit! Stumble this Post!Spurl this Post!
you can use popen following way:

Code:
#include <stdio.h>
int main()
{
#define BUFSIZE 1024
char buf[1024];
FILE *fp = NULL;
char *cmd = "ls -al /home/";
int nbytes = 0;
int fbytes = BUFSIZE;
int ch =0;
fp = popen(cmd, "r");
if (fp == NULL)  {
        printf("unable to open %s", cmd);
        exit(0);
}

while (nbytes < fbytes && (ch = fgetc(fp)) != EOF)
        buf[nbytes++] = ch;
buf[nbytes] = 0;
pclose(fp);
printf("output for %s %d:\n %s", cmd , nbytes, buf);
return 0;
}
Reply With Quote
  #5 (permalink)  
Old 05-13-2008
Registered User
 

Join Date: May 2008
Posts: 4
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit! Stumble this Post!Spurl this Post!
thanks for answering
Reply With Quote
  #6 (permalink)  
Old 05-13-2008
Registered User
 

Join Date: May 2008
Posts: 4
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit! Stumble this Post!Spurl this Post!
it works for some commands for example pwd, ifconfig, netstat, etc; but it does not get some answers such as "No such file or directory" when I used "gcc nonExistent.c" or "route help".
Any ideas?
Reply With Quote
  #7 (permalink)  
Old 05-13-2008
Registered User
 

Join Date: Feb 2008
Posts: 18
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit! Stumble this Post!Spurl this Post!
netstat, ifconfig are not in your PATH. Try with providing full path or Might be you need supreuser permissions.
First try these commands on your promopt/shell.
Reply With Quote
  #8 (permalink)  
Old 05-13-2008
era era is offline
Herder of Useless Cats
 

Join Date: Mar 2008
Location: /there/is/only/bin/sh
Posts: 2,203
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit! Stumble this Post!Spurl this Post!
Actually the problem is that you are getting those messages on standard error, not standard output. Try popen3 if your system has that.
Reply With Quote
Google UNIX.COM
Reply

Thread Tools
Display Modes




All times are GMT -7. The time now is 05:06 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008 The CEP Blog All Rights Reserved -Ad Management by RedTyger Visit The Global Fact Book

Content Relevant URLs by vBSEO 3.2.0

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101