How to call Linux command in C

Dear Friends,
How we can call the Linux commands like ls, cat, grep, clear and others Linux commands in C programs.

system call
or popen command

am pretty sure many such threads have been posted in the forum

if you do search thats a definite 'HIT' :slight_smile:

But still,

sprintf(cmd, "/bin/ls -l");
system(cmd);

Hi,
I think u can also call Linux commands using the exec family functions.
for that u need to read IPC programming in C.

Hi, I'm calling the split command from inside C and i'm wondering why it is not working (the file is not being split). Also, when I try executing only the unix command in the shell, it's working just fine. Am I missing something here? Below is my code:

Thanks in advance!

what is the return status of the system function call ( that uses split command ) ?

main()
{
system("split -l 50 -a 1 /absolutepath/sample.txt");
return 0;
}

If you could provide the return-value of system, one could examine why the call is failing.

try:

#include <stdio.h>
#include <stdlib.h>
int main(const int argc, const char* const* const argv) {
 int res;
 int err;
 res = system("split -l 50 -a 1 /absolutepath/sample.txt");
 fprintf(stdout,"execution returned %d.\n",res);
 if ((-1 != res) && (127 != res))
  fprintf(stdout,"now would be a good time to check out 'man split' to check what the resulting return-value (%d) means.\n",res);
 return res;
}