linux 文件操作函数
来源:互联网 发布:怎么和淘宝小二联系 编辑:程序博客网 时间:2024/04/28 15:44
文件操作(Linux)
常用linux函数库中的基本函数
1. close函数,关闭文件
2. creat函数,建立文件
3. dup、dup2函数,复制文件描述词
4. fcntl函数,文件描述词操作
5. flock函数,锁定文件或解除锁定
6. fsync函数,将缓冲区数据写回磁盘
7. lseek函数,移动文件的读写位置
8. mkstemp函数,建立唯一的临时文件
9. open函数,打开文件
10. read函数,由已打开的问价读取数据
11. sync函数,将缓冲区数据写回磁盘
12. write函数,将数据写入已打开的文件内
等。
详细:
1. open、close函数
int open(const char *pathname, int flags);
int open(const char *pathname, int flags, mode_t mode);
int close(int fd);
2. read、write函数
ssize_t read(int fd, void buffer, size_t count);
ssize_t read(int fd, const void *buffer, size_t count);
3. access、stat、fstat函数。 查看文件属性
int access(const char *pathname, int mode);
int stat(const char *pathname, struct stat *buf);
int fstat(int fd, struct stat *buf);
4. 目录文件操作
getcwd函数,得到当前的工作路径
char *getcwd(char *buffer, size_t size);
mkdir函数,创建一个目录
int mkdir(const char *path, mode_t mode);
opendir函数,打开一个目录
DIR *opendir(const char *path);
readdir函数,读一个已打开的目录
struct dirent *readdir(DIR *dir);
rewinddir函数,重读目录
void rewinddir(DIR *dir);
closedir函数,关闭一个目录
int closedir(DIR *dir, off_t off);
telldir、seekdir函数,与ftell、fseek函数类似
off_t telldir(DIR *dir);
void seekdir(DIR *dir, off_t off);
5. 管道文件
int pipe(int fildes[2]);
创建一个管道,fildes[0]读的文件描述符,fildes[1]写的文件描述符
6. access函数,判断是否具有存取文件的权限
int access(const char *pathname, int mode);
其中mode:R_OK, W_OK, X_OK, F_OK(可读、可写、可执行、存在)
7. alphasort函数,以字母顺序排序目录结构
int alphasort(const struct dirent **a, const struct dirent **b);
8. chdir函数,改变当前的工作目录
int chdir(cosnt char *path);
9. chmod函数,改变文件的权限
int chmod(const char *path, mode_t mode);
10. chown函数,改变文件的所有者
int chown(const char *path, uid_t owner, gid_t group);
11. chroot函数,改变根目录
int chroot(const char *path);
12. closedir函数,关闭目录
int closedir(DIR *dir);
13. fchdir函数,改变当前的工作目录
int fchdir(int fd);
14. fchmod函数,改变文件的权限
int fchmod(int fd, mode_t mode);
15. fchown函数,改变文件的所有者
int chown(int fd, uid_t owner, gid_t group);
16. fstat函数 获取文件的状态
int fstat(int files, struct stat *buf);
17. ftruncate函数,改变文件大小
int ftruncate(int fd, off_t length);
18. getcwd函数,获取当前的工作目录
char *getcwd(char *buf, size_t size);
19. link函数,建立文件连接(硬链接)
int link(const char *oldpath, const char *newpath);
不同的文件系统,则用symlink函数
20. lstat函数 获取文件状态
int lstat(const char *filename, struct stat *buf);
与stat的区别在于:当filename为符号连接时,lstat返回link本身的状态
21. opendir函数,打开目录
DIR *opendir(const char *name);
22. readdir函数,读取目录
struct dirent *readdir(DIR *dir);
23. readlink函数取得符号连接所指的文件
int readlink(const char *path, char *buf, size_t bufsize);
24. remove函数,删除文件
int remove(const char *pathname);
若文件为一连接或者一个目录,则用unlink和rmdir
25. rename函数,更改文件名称或位置
int rename(const char *oldpath, const char *newpath);
若newpath所指的文件已存在,则会删除该文件
26. rewinddir函数,重设读取目录的位置为开头位置
void rewinddir(DIR *dir);
27, seekdir函数,设置下回读取目录的位置
void seekdir(DIR *dir, off_t offset);
28. stat函数,取得文件状态
int stat(const char *filename, struct stat *buf);
29. symlink函数,建立文件符号连接
int symlink(const char *oldpath, const char *newpath);
如果newpath指定的名称是已存在的文件,则不会建立连接
30. telldir函数,获取目录的读取位置
off_t telldir(DIR *dir);
31. truncate函数,改变文件大小
int truncate(const char *path, off_t length);
32. umask函数,设置建立新文件时的权限掩码
mode_t umask(mode_mask);
建立文件时,所得到的文件权限为 mode&(~mask)
33. unlink 函数, 删除文件
int unlink(const char *pathname);
删除指定文件
34. utime函数,修改文件的存取时间和更改时间
int utime(const char *filename, struct utimebuf *buf);
一些c库中文件操作的函数:
1. fopen打开一个文件
2. fgetc从文件中读取一个字符
3. fputc将一个字符写入到一个文件
4. fgets从文件中读取一行字符串
5. fputs将一行字符串写入到文件中
5. fprintf格式化字符串到文件中
6. fread按字节数读取文件
7. fwrite按字节数写文件
8. fseek移动文件流的位置
9. fclose关闭一个文件
其中fgetc、fputc、fgets、fputs、fprintf读写二进制文件会出现乱码
- linux 文件操作函数
- linux文件操作函数
- linux文件操作函数
- Linux 文件操作函数
- Linux文件操作常用函数
- linux文件操作函数说明
- linux文件操作--函数分类
- Linux文件操作函数接口
- linux文件操作相关函数
- linux c文件操作函数
- Linux C 函数参考(文件内容操作)
- Linux 常用C函数(文件操作篇)
- Linux 常用C函数(文件操作篇)
- 文件操作函数全集 Linux C
- Linux C 函数参考(文件内容操作)
- Linux C 文件操作函数参考
- Linux C 函数参考(文件内容操作)
- Linux文件与目录操作函数
- 设备像素比devicePixelRatio简单介绍
- 《tiny6410裸机程序》第八章:S3C6410总中断控制寄存器
- Cookie试用
- Tower Tips and Tricks (part 3): Commits and Patches
- 设计模式——访问者
- linux 文件操作函数
- 采用busybox的嵌入式Linux根文件系统的的制作方法
- struts2教程:7、指定Struts2处理的请求后缀
- 处之泰然
- Tower Tips and Tricks (part 1): drag and drop
- 插入排序--希尔排序
- ubuntu下apt-get 命令参数大全
- 关于ARM汇编中使用宏定义的处理
- Tower Tips and Tricks (part 4): customization & keyboard shortcuts