I have executed a command "watch grep \"cpu MHz \" /proc/cpuinfo".After executing this command i got following result. Result of The Command
But when I am trying this command using c code.
#include<stdio.h>
#include<stdlib.h>
int main(){
FILE *fp;
char path[1035];
char command[]="watch grep \"cpu MHz \" /proc/cpuinfo";
fp = popen(command, "r");
if (fp == NULL) {
printf("Failed to run command\n" );
exit(1);
}
/* Read the output a line at a time - output it. */
while (fgets(path, sizeof(path)-1, fp) != NULL) {
printf("%s",path);
}
pclose(fp);
return 0;
}
I am getting following result.
Result of The Code tell me where am I going wrong?
watchin single quotes. E.g."watch 'grep \"cpu MHz\" /proc/cpuinfo'";watchcommand contains no newline characters, sofgetsis not returning until thepathbuffer is full. By the way, you don't need to usesizeof(path)-1in your call tofgets. You can just usesizeof(path).