Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

C Programming - whats the correct syntax?

Options
  • 07-01-2011 11:49am
    #1
    Registered Users Posts: 2,353 ✭✭✭


    Unsure as to what to google.....whats the correct syntax for the line below. I want to insert a value to a string for command line execution

    if(!(0==system("pidof -x "<<ProcessName<<" > /dev/null")))

    Im not sure what operands to use...im guessing <<


Comments

  • Moderators, Technology & Internet Moderators Posts: 1,335 Mod ✭✭✭✭croo


    It's been a while since I did C so I won't comment on the syntax.. but looks ok.

    But if you wanted to determine if a process is running or not I *think* this won't work. My thinking is so long as the pidof syntax is correct you will get a 0 return, i.e. it will report there was no problem running the command as opposed to the command returning no processes.

    I'm not 100% but that's how I would think it works.


  • Registered Users Posts: 15,065 ✭✭✭✭Malice


    First off that looks like C++ rather than C. In C the '<<' operator is a bitwise left-shift operator.
    Secondly, and this is a matter of taste, I would reverse the order of the if() statement so that you're doing something like if (<operation> == 0) rather than if (0 == <operation>)
    Thirdly, I would create the string outside of the if statement and remove the boolean check to improve clarity. Something like this:

    [PHP]
    /* I'm not sure if this syntax is valid, it's been a long time since I did any C work.
    Maybe you could use strcat()? */
    char *command = "pidof -x " + ProcessName + " > /dev/null";

    if (system(command) == 0)
    {
    // Do stuff.
    }
    else
    {
    // Do other stuff.
    }
    [/PHP]


  • Registered Users Posts: 5,246 ✭✭✭conor.hogan.2


    #include <stdio.h>

    int main ( int argc, char *argv[] )
    {
    if (...
    ...
    }

    That is for when you want to take commands straight of the CL, im not sure if that what you want or not.

    If it is search for "command line arguments in c" or "second main in c"


  • Registered Users Posts: 9,579 ✭✭✭Webmonkey


    #include <stdio.h>

    int main ( int argc, char *argv[] )
    {
    if (...
    ...
    }

    That is for when you want to take commands straight of the CL, im not sure if that what you want or not.

    If it is search for "command line arguments in c" or "second main in c"
    I think he wants to send command parameters to a process when he executes it via the system command.


  • Registered Users Posts: 5,246 ✭✭✭conor.hogan.2


    Ah, makes more sense.


  • Advertisement
  • Registered Users Posts: 15,065 ✭✭✭✭Malice


    That is for when you want to take commands straight of the CL, im not sure if that what you want or not.
    I think he's trying to run a DOS command using system().


  • Moderators, Technology & Internet Moderators Posts: 1,335 Mod ✭✭✭✭croo


    pidof is a unix command to display the process id of the given process. it will return a list of process id numbers in cash there are multiple instances the processes running.

    system(command) will execute the command as if it were at the command line.

    I suspect what the OP wants is to determine if a process is running or not.


  • Registered Users Posts: 2,353 ✭✭✭Galway K9


    oh thats all working and process, my prob was concatinating a string with values so i used strcat()....Thanks for all the help.


Advertisement