I have the following c code. I want to display my file with less by calling execv()
however the following seems never work. The program terminates and noting pop out.
#include <stdlib.h>
#include <unistd.h>
#include <stdio.h>
#include <string.h>
int main(void){
int pid;
if(pid=fork()>0){
//read in from stdin and pass to pipe
}else if(pid==0){
//read from pipe
//write to out.txt
//everything up to here works fine
char* para[]={"less","/Desktop/out.txt"};
execv("/bin/less",para);
}
return 0;
}
(The original code contained
execv("bin/less", para);.) Unless the current directory is the root directory,/, or unless there is a programlessin the subdirectory./bin/less, then one of your problems is that you have a probable typo in the name of the executable. That assumes the program is/bin/lessand not/usr/bin/less. You might even useexecvp()to do a PATH-based search for the program.There’s an additional problem: you need to include a null pointer to mark the end of the argument list.
Finally, you can print an error message after the
execv()returns. The mere fact that it returns tells you it failed.Or:
The remarks in the code about pipes are puzzling since there is no sign of pipes other than in the comments. As it stands,
lesswill read the file it is told to read. Note thatlesswill not paginate its output if the output is not going to a terminal. Since we can see no I/O redirection, we have to assume, then, thatlesswill ignore anything the program tries to write to it, and will not send any data back to the program.