linux文件操作的一些函数
2012-12-31 21:21
561 查看
dup, dup2,
dup3 - duplicate a file descriptor
these system calls create a copy of the file descriptor oldfd.
dup() uses the lowest-numbered unused descriptor for the new descriptor.
dup2() makes newfd be the copy of oldfd, closing newfd first if necessary, but note the following:
*If oldfd is not a valid file descriptor, then the call fails, and newfd is not closed.*If oldfd is a valid file descriptor, and newfd has the same value as oldfd, then dup2() does nothing, and returns newfd.
— Macro: int O_ACCMODE
This macro stands for a mask that can be bitwise-ANDed with the file status flag value to produce a value representing the file access mode. The mode will be
or
unlink -
delete a name and possibly the file it refers to
unlink()
deletes a name from the file system. If that name was the last link to a file and no processes have the file open the file is deleted and the space it was using is made available for reuse.
dup3 - duplicate a file descriptor
these system calls create a copy of the file descriptor oldfd.
dup() uses the lowest-numbered unused descriptor for the new descriptor.
dup2() makes newfd be the copy of oldfd, closing newfd first if necessary, but note the following:
*If oldfd is not a valid file descriptor, then the call fails, and newfd is not closed.*If oldfd is a valid file descriptor, and newfd has the same value as oldfd, then dup2() does nothing, and returns newfd.
#include<unistd.h> #include<sys/stat.h> #include<fcntl.h> #include<stdio.h> int main(int argc ,char *argv[]) { int fd=open("./1.c",O_RDONLY); printf("original fd is %d\n",fd); int new_fd=dup(fd); printf("new fd is %d\n",new_fd); }
— Macro: int O_ACCMODE
This macro stands for a mask that can be bitwise-ANDed with the file status flag value to produce a value representing the file access mode. The mode will be
O_RDONLY,
O_WRONLY,
or
O_RDWR. (On GNU/Hurd systems it could also be zero, and it never includes the
O_EXECbit.)
unlink -
delete a name and possibly the file it refers to
unlink()
deletes a name from the file system. If that name was the last link to a file and no processes have the file open the file is deleted and the space it was using is made available for reuse.
#include<fcntl.h> #include<stdio.h> int main(void) { if(open("cc",O_RDWR)<0) printf("open error\n"); if(unlink("cc")<0) printf("unlink error\n"); printf("file unlinked\n"); sleep(15); printf("done\n"); exit(0); }
相关文章推荐
- linux下的一些文件操作函数
- linux文件操作函数(open、write、read、close)可用于串口读写
- linux-011文件系统节点操作函数解析
- linux-011文件系统超级块的操作函数(挂载卸载系统)解析
- matlab中一些文件操作函数(fopen,fullfile等)
- 关于在WIN32调用一些Zw系列的文件操作函数
- linux函数代码操练---文件操作-----fwrite和fread
- PHP操作文件的一些基本函数使用示例
- C语言中一些对文件操作的函数总结
- python学习笔记: 一些有用的文件操作函数
- Objective-C:Objective-C:文件中一些对目录进行操作的函数
- linux系统文件操作函数之dup,dup2,fcntl
- Linux下文件及目录的一些操作(附递归遍历目录源码)
- unix环境下的文件操作的一些函数open()、close()、read()、write()、dup()、fsync()sync()函数
- Linux下的文件操作函数
- linux下文件操作函数
- Linux文件与目录操作函数
- 解析linux 文件和目录操作的相关函数
- [Linux流操作]使用fread和fwrite函数读写文件
- Linux 文件操作函数 opendir()、readdir()、closedir()、stat()