开发者

Execute shell commands using popen in c++

开发者 https://www.devze.com 2023-03-31 14:52 出处:网络
I need to b开发者_如何学JAVAe able to execute some shell commands such as moving to the right directory where I have some files I need to decode and then decoding them using another command. I read so

I need to b开发者_如何学JAVAe able to execute some shell commands such as moving to the right directory where I have some files I need to decode and then decoding them using another command. I read some stuff about using popen but I didnt really understand how to use it for executing multiple commands.

Any pointers will be greatly appreciated :)

Thanks

FILE *pf;
char command[150];
char data[512];

// Execute a process listing
sprintf(command, "cd");
pf = _popen(command,"r"); 

sprintf(command, "cd Test_copy");
pf = _popen(command,"r");   */

sprintf(command, "java -jar Tool.jar -b x.fit x.csv"); 
pf = _popen(command,"r"); 

if(!pf){
  fprintf(stderr, "Could not open pipe for output.\n");
  return;
}

// Grab data from process execution
fgets(data, 512 , pf);

// Print grabbed data to the screen.
fprintf(stdout, "-%s-\n",data); 

if (_pclose(pf) != 0)
    fprintf(stderr," Error: Failed to close command stream \n");


Use ShellExecute to play with files (open with default application etc.). Use system to run shell commands.


No, don't. That would be like using a sledgehammer to knock on a door. Furthermore, it is "evil": http://www.cplusplus.com/forum/articles/11153/

0

精彩评论

暂无评论...
验证码 换一张
取 消