![]() |
|
|
|
|
|||||||
| High Level Programming Post questions about C, C++, Java, SQL, and other programming languages here. |
|
|
||||
| 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 |
|
|
Submit Tools | LinkBack | Thread Tools | Display Modes |
|
|||
|
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. |
| Forum Sponsor | ||
|
|
|
|||
|
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. |
|
|||
|
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;
}
|
| Thread Tools | |
| Display Modes | |
|
|