C and shell question

If something doesn't fit in any other forum then post it here.
Forum rules
READ NOW: L2j Forums Rules of Conduct
Post Reply
bagun
Posts: 55
Joined: Sun Oct 03, 2010 12:09 pm

C and shell question

Post 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.
Server Developer/Admin of L2Jservers
User avatar
JIV
L2j Veteran
L2j Veteran
Posts: 1882
Joined: Sun Jan 06, 2008 8:17 pm
Location: Slovakia
Contact:

Re: C and shell question

Post by JIV »

wrong forum :P
bagun
Posts: 55
Joined: Sun Oct 03, 2010 12:09 pm

Re: C and shell question

Post by bagun »

Yea i know that all of you dont know about C,but i think you can help me :lol:
Server Developer/Admin of L2Jservers
User avatar
BiggBoss
L2j Veteran
L2j Veteran
Posts: 1104
Joined: Wed Apr 15, 2009 3:11 pm
Location: Spain

Re: C and shell question

Post 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 :)
Image
bagun
Posts: 55
Joined: Sun Oct 03, 2010 12:09 pm

Re: C and shell question

Post 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
Server Developer/Admin of L2Jservers
User avatar
Stake
Posts: 383
Joined: Sun Mar 23, 2008 9:33 pm
Location: Hungary
Contact:

Re: C and shell question

Post 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.
Image
Image
bagun
Posts: 55
Joined: Sun Oct 03, 2010 12:09 pm

Re: C and shell question

Post by bagun »

Thx i will test it :)
Server Developer/Admin of L2Jservers
bagun
Posts: 55
Joined: Sun Oct 03, 2010 12:09 pm

Re: C and shell question

Post by bagun »

Stake your code has a error in line 31 i remember.Today i will revise it and tell you something.
Server Developer/Admin of L2Jservers
User avatar
Stake
Posts: 383
Joined: Sun Mar 23, 2008 9:33 pm
Location: Hungary
Contact:

Re: C and shell question

Post 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
Image
Image
bagun
Posts: 55
Joined: Sun Oct 03, 2010 12:09 pm

Re: C and shell question

Post 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
Server Developer/Admin of L2Jservers
Post Reply