Monday, May 24, 2010

Unix c program: why the file descriptor returns value is 3 not 1?

pls help me to explain code shown below,





int main(void)


{


int fd;


char path[] = "myhello";


if ((fd = open(path,O_CREAT|O_TRUNC|O_WRONLY,0644)...


perror("open");


exit(EXIT_FAILURE);


} else {


printf("opened %s\n", path);


printf("descriptor is %d\n", fd);


}


where fd is 3 not 1, is it according to the code segment like this


((fd = open(path,O_CREAT|O_TRUNC|O_WRONLY,0644)...

Unix c program: why the file descriptor returns value is 3 not 1?
You already have three open descriptors before your program starts: stdin, stdout, stderr. These use file descriptors 0, 1 and 2 respectively. Therefore, 3 is the next available number.


No comments:

Post a Comment