LinuxSir.cn,穿越时空的Linuxsir!

 找回密码
 注册
搜索
热搜: shell linux mysql
查看: 945|回复: 6

帮我看看exit函数的输出

[复制链接]
发表于 2006-4-20 20:53:37 | 显示全部楼层 |阅读模式
[root@localhost c]# more exit.c
#include<unistd.h>
#include<stdio.h>
#include<unistd.h>
#include<stdlib.h>

main()
{

  printf("output begin\n");
  printf("content in buffer");
  write(STDOUT_FILENO,"abc",4);
//  _exit(0);
  exit(0);
}
[root@localhost c]# ./a.out
output begin
abccontent in buffer[root@localhost c]# ./a.out > temp
[root@localhost c]# more temp
abc
content in buffer
[root@localhost c]#
为什么将a.out标准输出重定向到别的文件里后,output begin反而不能打印输出了?
发表于 2006-4-21 10:40:12 | 显示全部楼层
看看 printf/write 的手册吧
printf 和 write 的混用是有问题的, 尤其是楼主上面的这种混用.
另外, 标准输出在不同的使用方式(交互式, 或重定向到文件)下其缓冲模式
一般是不同的. 这些都可能导致问题...
回复 支持 反对

使用道具 举报

 楼主| 发表于 2006-4-22 03:03:30 | 显示全部楼层
我知道交互式的时候,对于printf函数来说是行缓冲
而重定向时,对于printf是全缓冲

但这不能解释我所碰到的问题。
现在的问题是,为什么  printf("output begin\n");
在将输出重定向的时候,不能输出output begin?(使用exit()退出)
而交互式运行的时候没有问题。
回复 支持 反对

使用道具 举报

发表于 2006-4-22 12:52:10 | 显示全部楼层
在printf后面加上fflush试试?
回复 支持 反对

使用道具 举报

发表于 2006-4-22 13:37:17 | 显示全部楼层
Post by samwang
[root@localhost c]# more exit.c
#include<unistd.h>
#include<stdio.h>
#include<unistd.h>
#include<stdlib.h>

main()
{

  printf("output begin\n");
  printf("content in buffer");
  write(STDOUT_FILENO,"abc",4);
//  _exit(0);
  exit(0);
}
[root@localhost c]# ./a.out
output begin
abccontent in buffer[root@localhost c]# ./a.out > temp
[root@localhost c]# more temp
abc
content in buffer
[root@localhost c]#
为什么将a.out标准输出重定向到别的文件里后,output begin反而不能打印输出了?

在我的系统上没有发生这个问题。你最好再确认一下贴出的代码是否有误?
或者有条件的话,可以换个环境试试。
回复 支持 反对

使用道具 举报

 楼主| 发表于 2006-4-26 10:55:31 | 显示全部楼层
kj501:能把你的环境以及两种运行方式的结果告诉我么?
回复 支持 反对

使用道具 举报

发表于 2006-4-29 16:03:54 | 显示全部楼层
我用的是debian,我的系统也正确输出为
$ more temp
abcoutput begin
content in buffer
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

快速回复 返回顶部 返回列表