这里我在 linux 上的代码中使用了下面的代码。 在系统函数中使用 cp 命令。
我知道系统函数,如果命令成功执行,它会返回 0。否则它会返回错误代码。
如果我在这里使用正确的源路径和目标路径,那么我会得到这样的输出
Number == 0
如果我给出错误的源路径和目标路径而不是我得到的
cp: cannot create regular file `/home/sam/test/test': No such file or directory
Number == 256
cp: cannot stat `/home/sam/main/test2/test': Not a directory
Number == 256
这里我想知道cp
命令的错误码what cp command return here.
我的问题在这里
1 System function return error code of cp command?
2 Can i get error code of cp command from source code of cp command?
3 i want to handle all types of error in this cp command.
代码:
#include <stdlib.h>
#include <stdio.h>
void main()
{
int a;
a = system("cp /home/sam/main/test /home/sam");
printf("Number == %d\n",a);
}
所以任何人都请向我解释这一切
最佳答案
使用系统返回值的正确方法是使用 wait-specific macros .
if (WIFEXITED(a)) {
int rc;
rc = WEXITSTATUS(a);
printf("Exit with status: %d\n", rc);
} else {
/* Killed by a signal. */
}
关于c - 在 C/Linux 中返回什么系统函数和 cp 命令,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9803371/