您的位置:首页 > 运维架构 > Linux

Linux文件描述符

2010-04-05 18:45 239 查看
Code:

//Linux文件描述符,Linux中的文件分为:普通文件,目录文件,链接文件,设备文件。

//文件描述符:Linux对所有设备和文件的操作都是通过文件描述符来进行的。文件描述符是一个非负整数,是一个索引值,指向打开文件的记录表。标准输入STDIN_FILENO(0),标准输出STDOUT_FILENO(1),出错处理STDERR_FILENO(2)。

//不带缓存的I/O操作 open close

//open函数返回的值是最小未使用的文件描述符,0或1或2或3.返回失败为-1。

#include<unistd.h>

#include<sys/types.h>

#include<sys/stat.h>

#include<fcntl.h>

#include<stdlib.h>

#include<stdio.h>

int main(void)

{

int fd;

if((fd=open("/home/duanlongfei/桌面/hello.c",O_CREAT | O_TRUNC | O_WRONLY,0600))<0)

{

perror("open:");

exit(1);

}

else

{

printf("Open file:hello.c %d/n",fd);

}

if(close(fd)<0)

{

perror("close:");

exit(1);

}

else printf("Close hello.c/n");

exit(0);

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: