- 论坛徽章:
- 0
|
验证代码如下:
1、如下代码编译成a.out;
1#include <stdio.h>
2 #include <stdlib.h>
3 #include <unistd.h>
4
5 int main(int argc, char *argv[])
6 {
7 FILE *fp;
8 char a[] = "test.tst.tst";
9 printf("hello.\n");
10
11 fp = fopen(argv[1], "w+");
12 fwrite(a, 1, sizeof(a), fp);
13 fclose(fp);
14
15 _exit(0);
16 }
~2、下面的代码编成另一个程序b.out去调用上面的a.out,为什么我在命令行直接执行"./a.out child.txt"能够生成child.txt文件,而执行"./b.out"却不能够生成呢?是什么原因啊?请教一下,O(∩_∩)O谢谢
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <unistd.h>
4
5 int main(void)
6 {
7 pid_t pid;
8 int status;
9
10 if ( (pid = fork()) < 0)
11 printf("fork error.\n");
12 else if (pid == 0) {
13 if (execle("/home/a.out", "child.txt", (char *)0))
14 printf("execl error.\n");
15 }
16 if (wait(&status) != pid)
17 printf("wait error.\n");
18
19 return 0;
20 } |
|