使用system()复制文件(带"cp")永远不会返回?

0 c system

char cmd[256];
memset(cmd,0,256);
sprintf(cmd, "cp %s %s", "test1.txt", "test2.txt");
system(cmd);
printf("cmd completed\n");
Run Code Online (Sandbox Code Playgroud)

当我运行上面的代码时,我的应用程序在系统调用时挂起.我从来没有去过printf系列.我在使用GCC编译器的Linux Centos上运行.

任何帮助表示赞赏.

如果我在一个单独的应用程序中运行上述代码(主要复制粘贴).它运行正常.

Bri*_*and 7

作为一个完整的程序,下面的代码在大多数情况下都可以正常工作.

我可以让它挂起的唯一方法是if test1.txt是一种导致cp挂起的特殊文件类型.例如,mkfifo test1.txt会给你有趣的结果.有趣的结果,我的意思是你必须用CTRL + C杀死程序,杀死等等.

您的问题很可能不在您发布的代码中.

/* copy.c, 
   compile using the command "gcc -o copy copy.c"
   run using "./copy"
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define CMD_SIZE 256
char cmd[CMD_SIZE];

int main(int argc, char **argv)
{
  memset( cmd, 0, CMD_SIZE );
  sprintf( cmd, "cp %s %s", "test1.txt", "test2.txt" );
  system(cmd);
  printf("cmd complete\n");
}
Run Code Online (Sandbox Code Playgroud)

作为完整性检查,您可以尝试添加printf("%d\n", BUFSIZE)以检查其值,BUFSIZEprintf("%s\n", cmd)确保该命令看起来像您想要的那样.