免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 1216 | 回复: 1
打印 上一主题 下一主题

[Linux] 请教一个exec子程序中写文件的问题 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2013-08-20 22:03 |只看该作者 |倒序浏览
验证代码如下:
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 }

论坛徽章:
0
2 [报告]
发表于 2013-08-20 22:18 |只看该作者
不好意思,我自己调用execle错误了
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP