fcntl() function question?
why? follow program output: fd = 0. #include <sys/types.h>
#include <unistd.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
int fd;
if ((fd = fcntl(STDOUT_FILENO, F_GETFD, 0)) == -1)
{
printf("fcntl error.\n");
}
printf("fd = %d\n", fd);
exit(0);
}
I think output: fd = 1. where error?
----------------解决方案--------------------------------------------------------
Sorry,your program cannot run under my Turbo C or Visual C++ compilers.
----------------解决方案--------------------------------------------------------
compiler cc or gcc
----------------解决方案--------------------------------------------------------
I have no such compilers available.
May be somebody could help you.
----------------解决方案--------------------------------------------------------
From what have discussed above, i found my English is bad.
----------------解决方案--------------------------------------------------------
以下是Linux_good在2006-6-22 15:18:28的发言(翻译):
为什么? 下列程序输出: fd = 0.
#include <sys/types.h>
#include <unistd.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
int fd;
if ((fd = fcntl(STDOUT_FILENO, F_GETFD, 0)) == -1)
{
printf("fcntl error.\n");
}
printf("fd = %d\n", fd);
exit(0);
}
我认为输出应为: fd = 1. 哪儿错了?
为什么? 下列程序输出: fd = 0.
#include <sys/types.h>
#include <unistd.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
int fd;
if ((fd = fcntl(STDOUT_FILENO, F_GETFD, 0)) == -1)
{
printf("fcntl error.\n");
}
printf("fd = %d\n", fd);
exit(0);
}
我认为输出应为: fd = 1. 哪儿错了?
[译者注]楼主用的是cc或gcc编译器。
----------------解决方案--------------------------------------------------------
int fcntl(int fd,int cmd,long arg);
函数的返回值代表是否成功,如果成功,则返回0,失败则返回-1,为什么lz会认为返回1呢?
----------------解决方案--------------------------------------------------------
而且lz的理解似乎有点错误,fd代表欲设置的文件描述,怎么用来接收返回的错误值呢?
----------------解决方案--------------------------------------------------------