English 中文(简体)
使用 execv 执行的执行要小一些吗?
原标题:execute less with execv?

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;
}
最佳回答

(原代码中包含 execv ("bin/less", para); .) 除非目前的目录是根目录 / , 或者在子目录 ./bin/less 中存在 /code> 程序。 /bin/ less 中存在程序 /code> 。 那么问题之一是, 您在可执行文件名称中有一个可能的印记。 假设程序是 / bin/less , 而不是 。 您甚至可以使用 > execvp () 来搜索此程序。

还有一个问题:您需要包含一个无效指针以标记参数列表的结尾。

最后, 您可以在 execv () 返回后打印错误消息。 仅返回这一事实就说明它失败了 。

#include <stdlib.h>
#include <unistd.h>
#include <stdio.h>

int main(void)
{
    int pid;
    if ((pid = fork()) != 0)
    {
        // read in from stdin and pass to pipe
        // Need to test for fork() error here too
    }
    else
    {
        // read from pipe
        // write to out.txt
        // everything up to here works fine

        char *para[] = { "/bin/less", "Desktop/out.txt", 0 };
        execv(para[0], para);
        fprintf(stderr, "Failed to execute %s
", para[0]);
        exit(1);
    }
    return 0;
}

或者:

        char *para[] = { "less", "Desktop/out.txt", 0 };
        execvp(para[0], para);
        fprintf(stderr, "Failed to execute %s
", para[0]);

管道代码中的注释令人费解, 因为除注释中外没有管道的迹象。 目前, < code> less 将读取它要读的文件 。 请注意, 如果输出不进入终端, < code> less 将不会使其输出光滑 。 由于我们可以看到没有 I/ O 重定向, 我们必须假设, < code> 将忽略程序试图写入它的任何内容, 并且不会将任何数据发送回程序 。

问题回答
char* para[]={"less","/Desktop/out.txt"};
execv("/bin/less",para);

Execv怎么知道何时停止阅读参数?

我认为,如果您将代码放在那里处理 Execv () 返回一个错误, 您就会发现这个错误。 您也没有测试叉子( ) 的错误 。





相关问题
Fastest method for running a binary search on a file in C?

For example, let s say I want to find a particular word or number in a file. The contents are in sorted order (obviously). Since I want to run a binary search on the file, it seems like a real waste ...

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

Tips for debugging a made-for-linux application on windows?

I m trying to find the source of a bug I have found in an open-source application. I have managed to get a build up and running on my Windows machine, but I m having trouble finding the spot in the ...

Trying to split by two delimiters and it doesn t work - C

I wrote below code to readin line by line from stdin ex. city=Boston;city=New York;city=Chicago and then split each line by ; delimiter and print each record. Then in yet another loop I try to ...

Good, free, easy-to-use C graphics libraries? [closed]

I was wondering if there were any good free graphics libraries for C that are easy to use? It s for plotting 2d and 3d graphs and then saving to a file. It s on a Linux system and there s no gnuplot ...

Encoding, decoding an integer to a char array

Please note that this is not homework and i did search before starting this new thread. I got Store an int in a char array? I was looking for an answer but didn t get any satisfactory answer in the ...