Page 1 of 1

C and shell question

Posted: Sat Nov 06, 2010 9:22 am
by bagun
Ok i have a question.It can be possible to save the result of a command line in a C variable.I make this program,what works perfect

Code: Select all

#include <stdio.h>#include <signal.h> main (){    int res;    char pid[6];    FILE *fichero;    system("ps -lae | grep señal |tr -s \" \" \":\" | cut -d: -f 4 > resultado.txt");    fichero=fopen("resultado.txt","r+");    fgets(pid,6,fichero);    printf ("%s\n",pid);    fclose(fichero);    printf ("le voi enviar una señal\n");    kill(atoi(pid),SIGKILL); } 
I need to save the result of this comand line in a variable,not to create a file.

Code: Select all

system("ps -lae | grep señal |tr -s \" \" \":\" | cut -d: -f 4 > resultado.txt");
So,is this can be possible? Thanks in advice.

Re: C and shell question

Posted: Sat Nov 06, 2010 9:57 am
by JIV
wrong forum :P

Re: C and shell question

Posted: Sat Nov 06, 2010 2:22 pm
by bagun
Yea i know that all of you dont know about C,but i think you can help me :lol:

Re: C and shell question

Posted: Sat Nov 06, 2010 4:43 pm
by BiggBoss

Code: Select all

 int res; 
bad step in C, is hihgly recommended to initialize it and then reserver his space memory

Code: Select all

 int res = 0; 
welcome to the dynamic memory management :)

Re: C and shell question

Posted: Sun Nov 07, 2010 12:33 pm
by bagun
BiggBoss wrote:

Code: Select all

 int res; 
bad step in C, is hihgly recommended to initialize it and then reserver his space memory

Code: Select all

 int res = 0; 
welcome to the dynamic memory management :)
jeje thanks with this point,but can you hel me with the shell command saving? saving PID in a file i think is no very good

Re: C and shell question

Posted: Sun Nov 07, 2010 2:49 pm
by Stake
Hi!
If you know about stdio, then it will help you. :)

Code: Select all

#include <stdio.h>#include <iostream> #define MAX_BUFFER 1000 using namespace std; string getOutput(string); int main(int argc, char *argv[]){    string val = getOutput("ps -lae | grep señal |tr -s \" \" \":\" | cut -d: -f 4");    cout << val;    return 0;} string getOutput(string cmd){    string data;    FILE *stream;    char buffer[MAX_BUFFER]; // i set it to 1000 chars, don't forget to increase for bigger input stream    stream = popen(cmd.c_str(), "r"); // opens the stream    while(fgets(buffer, MAX_BUFFER, stream) != NULL) // stores the input stream into buffer        data.append(buffer); // while data is stored in the buffer, append to the data string    pclose(stream); // closes the stream    return data;}
And no matter, if you didn't give a value to the declaration, the compiler always sets the string and char to null, and integer to 0.

Re: C and shell question

Posted: Sun Nov 07, 2010 3:14 pm
by bagun
Thx i will test it :)

Re: C and shell question

Posted: Tue Nov 09, 2010 1:29 pm
by bagun
Stake your code has a error in line 31 i remember.Today i will revise it and tell you something.

Re: C and shell question

Posted: Tue Nov 09, 2010 1:35 pm
by Stake
bagun wrote:Stake your code has a error in line 31 i remember.Today i will revise it and tell you something.
Then that is not my code. There is no line 31 in mine, only 28 at the most. Insert a new line at the end, because some compilers don't like "new lineless" codes. :D

Re: C and shell question

Posted: Tue Nov 09, 2010 1:38 pm
by bagun
Stake wrote:
bagun wrote:Stake your code has a error in line 31 i remember.Today i will revise it and tell you something.
Then that is not my code. There is no line 31 in mine, only 28 at the most. Insert a new line at the end, because some compilers don't like "new lineless" codes. :D

I made some modifications on your code.Now i cant access it,i will tell you later :D