linux文件操作
来源:互联网 发布:琼瑶毁三观台词 知乎 编辑:程序博客网 时间:2024/06/05 18:54
一.文件类型
1)普通文件(regular file).这是最常见的文件类型,这种文件包含了某种形式的数据.
2)目录 (directory)
3)字符特殊文件(character special file).系统中某些类型的设备,例如声卡,网卡,键盘,鼠标。
4)块特殊文件(block special file).用于磁盘设备,系统中的所有设备或者是字符特殊文件,或者是块特殊文件。
5)FIFO:这种文件用于进程间的通信,有时也将其称为命名管道。
6)套接口(socket).用于进程间的网络通信。套接口也可用于在一台宿主机上的进程之间的非网络通信。
7)符号连接(symbolic link),这种文件指向另一个文件。
可以用下面的宏确定文件类型:S_ISREG( ) 普通文件 S_ISDIR( ) 目录文件 S_ISCHR( ) 字符特殊文件
S_ISBLK( ) 块特殊文件 S_ISFIFO( ) 管道或FIFO S_ISLNK( ) 符号连接 S_ISSOCK( ) 套接字
二.文件的属性和操作
stat、fstat和lstat函数取得文件的属性,三个函数的返回:若成功则为0,若出错则为-1
#include <sys/types.h>
#include <sys/stat.h>
int stat(const char * pathname, struct stat *buf);
int fstat(int filedes,struct stat *buf);
int lstat(const char *pathname, struct stat *buf);
#include <sys/types.h>
#include <sys/stat.h>
int stat(const char * pathname, struct stat *buf);
int fstat(int filedes,struct stat *buf);
int lstat(const char *pathname, struct stat *buf);
struct stat {
dev_t st_dev; /* device */
ino_t st_ino; /* inode */
mode_t st_mode; /* protection */
nlink_t st_nlink; /* number of hard links */
uid_t st_uid; /* user ID of owner */
gid_t st_gid; /* group ID of owner */
dev_t st_rdev; /* device type (if inode device) */
off_t st_size; /* total size, in bytes */
blksize_t st_blksize; /* blocksize for filesystem I/O */
blkcnt_t st_blocks; /* number of blocks allocated */
time_t st_atime; /* time of last access */
time_t st_mtime; /* time of last modification */
time_t st_ctime; /* time of last change */
};
access函数按进程的实际用户ID和实际组ID进行存取许可权测试。
#include <unistd.h>
int access(const char *pathname, int mode);
返回:若成功则为0,若出错则为-1
dev_t st_dev; /* device */
ino_t st_ino; /* inode */
mode_t st_mode; /* protection */
nlink_t st_nlink; /* number of hard links */
uid_t st_uid; /* user ID of owner */
gid_t st_gid; /* group ID of owner */
dev_t st_rdev; /* device type (if inode device) */
off_t st_size; /* total size, in bytes */
blksize_t st_blksize; /* blocksize for filesystem I/O */
blkcnt_t st_blocks; /* number of blocks allocated */
time_t st_atime; /* time of last access */
time_t st_mtime; /* time of last modification */
time_t st_ctime; /* time of last change */
};
access函数按进程的实际用户ID和实际组ID进行存取许可权测试。
#include <unistd.h>
int access(const char *pathname, int mode);
返回:若成功则为0,若出错则为-1
umask函数为进程设置文件方式创建屏蔽字,并返回以前的值。
#include <sys/types.h>
#include <sys/stat.h>
mode_t umask(mode_t cmask);
返回:以前的文件方式创建屏蔽字
chmod和fchmod函数:改变存取许可权
#include <sys/types.h>
#include <sys/stat.h>
int chmod(const char *pathname, mode_t mode);
int fchmod(int filedes, mode_t mode);
两个函数返回:若成功则为0,若出错则为-1
#include <sys/types.h>
#include <sys/stat.h>
int chmod(const char *pathname, mode_t mode);
int fchmod(int filedes, mode_t mode);
两个函数返回:若成功则为0,若出错则为-1
chown、fchown和lchown函数:改变所有者和组所有者
#include <sys/types.h>
#include <unistd.h>
int chown(const char *pathname, uid_t owner, gid_t group);
int fchown(int filedes, uid_t owner, gid_t group);
int lchown(const char *pathname, uid_t owner, gid_t group);
三个函数返回:若成功则为0,若出错则为-1
#include <sys/types.h>
#include <unistd.h>
int chown(const char *pathname, uid_t owner, gid_t group);
int fchown(int filedes, uid_t owner, gid_t group);
int lchown(const char *pathname, uid_t owner, gid_t group);
三个函数返回:若成功则为0,若出错则为-1
截短文件可以调用函数truncate和ftruncate
#include <sys/types.h>
#include <unistd.h>
int truncate(const char *pathname, off_t length);
int ftruncate(int filedes, off_t length);
两个函数返回;若成功则为0,若出错则为-1
#include <sys/types.h>
#include <unistd.h>
int truncate(const char *pathname, off_t length);
int ftruncate(int filedes, off_t length);
两个函数返回;若成功则为0,若出错则为-1
rename:文件重命名
#include <stdio.h>
int rename(const char *oldname, const char *newname);
返回:若成功则为0,若出错则为-1
#include <stdio.h>
int rename(const char *oldname, const char *newname);
返回:若成功则为0,若出错则为-1
utime:修改文件时间
#include <sys/types.h>
#include <utime.h>
int utime(const char *pathname, const struct utimbuf *times);
返回:若成功则为0,若出错则为-1
struct utimbuf {
time_t actime; /*access time*/
time_t modtime; /*modification time*/
}
#include <sys/types.h>
#include <utime.h>
int utime(const char *pathname, const struct utimbuf *times);
返回:若成功则为0,若出错则为-1
struct utimbuf {
time_t actime; /*access time*/
time_t modtime; /*modification time*/
}
三、目录操作
mkdir和rmdir:创建和删除目录
#include <sys/types.h>
#include <sys/stat.h>
int mkdir(const char *pathname, mode_t mode);
返回:若成功则为0,若出错则为-1
#include <sys/types.h>
#include <sys/stat.h>
int mkdir(const char *pathname, mode_t mode);
返回:若成功则为0,若出错则为-1
函数rmdir删除一个空目录。
#include <unistd.h>
int rmdir(const char *pathname);
#include <unistd.h>
int rmdir(const char *pathname);
#include <sys/types.h>
#include <dirent.h>
DIR *opendir(const char *pathname);
返回:若成功则为指针,若出错则为NULL
struct dirent *readdir(DIR *dir);
返回:若成功则为指针,若在目录尾或出错则为NULL
void rewinddir(DIR *dir);
int closedir(DIR *dir);
返回:若成功则为0,若出错则为-1
#include <dirent.h>
DIR *opendir(const char *pathname);
返回:若成功则为指针,若出错则为NULL
struct dirent *readdir(DIR *dir);
返回:若成功则为指针,若在目录尾或出错则为NULL
void rewinddir(DIR *dir);
int closedir(DIR *dir);
返回:若成功则为0,若出错则为-1
定义在头文件<dirent.h>中的dirent结构如下所示:
struct dirent
{
__ino_t d_ino;
__off_t d_off;
unsigned short int d_reclen;
unsigned char d_type;
char d_name[256];
};
struct dirent
{
__ino_t d_ino;
__off_t d_off;
unsigned short int d_reclen;
unsigned char d_type;
char d_name[256];
};
chdir、fchdir和getcwd函数:改变目录和取得当前目录
#include <unistd.h>
int chdir(const char *pathname);
int fchdir(int filedes);
char *getcwd(char *buf, size_t size);
两个函数的返回:若成功则为0,若出错则为-1
int chdir(const char *pathname);
int fchdir(int filedes);
char *getcwd(char *buf, size_t size);
两个函数的返回:若成功则为0,若出错则为-1
四、文件I/O接口概述
文件有5个操作:打开、关闭、读、写、定位。有两套接口都可以用来完成这些操作,一类是系统调用接口,对应的函数是open
文件有5个操作:打开、关闭、读、写、定位。有两套接口都可以用来完成这些操作,一类是系统调用接口,对应的函数是open
、close、read、write、lseek,另一类是标准I/O库接口,对应的函数是fopen、fclose、fread、fwrite、fseek。
调用open函数可以打开或创建一个文件。
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
int open(const char *pathname, int oflag,.../*, mode_t mode */);
返回:若成功为文件描述符,若出错为-1
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
int open(const char *pathname, int oflag,.../*, mode_t mode */);
返回:若成功为文件描述符,若出错为-1
用creat函数创建一个新文件。
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
int creat(const char * pathname, mode_t mode) ;
返回:若成功为只写打开的文件描述符,若出错为-1
此函数等效于:
open(pathname, O_WRONLY|O_CREAT|O_TRUNC, mode);
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
int creat(const char * pathname, mode_t mode) ;
返回:若成功为只写打开的文件描述符,若出错为-1
此函数等效于:
open(pathname, O_WRONLY|O_CREAT|O_TRUNC, mode);
close函数关闭一个打开文件:
#include <unistd.h>
int close(int filedes);
返回:若成功为0,若出错为-1
#include <unistd.h>
int close(int filedes);
返回:若成功为0,若出错为-1
read函数从打开文件中读数据。
#include <unistd.h>
ssize_t read(int filedes, void *buff, size_t nbyt s);
返回:读到的字节数,若已到文件尾为0,若出错为-1
#include <unistd.h>
ssize_t read(int filedes, void *buff, size_t nbyt s);
返回:读到的字节数,若已到文件尾为0,若出错为-1
write函数向打开文件写数据。
#include <unistd.h>
ssize_t write(int filedes, const void * buff, size_t nbytes);
返回:若成功为已写的字节数,若出错为-1
#include <unistd.h>
ssize_t write(int filedes, const void * buff, size_t nbytes);
返回:若成功为已写的字节数,若出错为-1
用lseek显式地定位一个打开文件。
#include <sys/types.h>
#include <unistd.h>
off_t lseek(int filedes, off_t offset, int whence);
返回:若成功为新的文件位移,若出错为-1?
若whence是SEEK_SET,则将该文件的位移量设置为距文件开始处offset个字节。
若whence是SEEK_CUR,则将该文件的位移量设置为其当前值加offset,offset可为正或负。
若whence是SEEK_END,则将该文件的位移量设置为文件长度加offset,offset可为正或负。
#include <sys/types.h>
#include <unistd.h>
off_t lseek(int filedes, off_t offset, int whence);
返回:若成功为新的文件位移,若出错为-1?
若whence是SEEK_SET,则将该文件的位移量设置为距文件开始处offset个字节。
若whence是SEEK_CUR,则将该文件的位移量设置为其当前值加offset,offset可为正或负。
若whence是SEEK_END,则将该文件的位移量设置为文件长度加offset,offset可为正或负。
- linux文件操作
- linux文件操作备忘录
- Linux shell__文件操作
- Linux文件操作常用命令
- linux 临时性文件操作
- Linux文件操作篇
- linux文件相关操作
- linux--文件操作命令
- linux 文件操作
- Linux文件基本操作
- linux 文件操作 简介
- linux 文件操作指令
- linux文件操作
- Linux文件操作
- linux文件操作命令
- Linux文件操作
- Linux超大文件操作
- Linux文件操作
- 黑马程序员——java流程控制与函数
- cpanel pop3s 不能收信
- 网页爬虫抓取URL
- 分页技术
- 将自已的程序加到鼠标右键菜单里
- linux文件操作
- 堆和栈的区别 (转贴)
- 黑马程序员-Java模拟交通灯
- 黑马程序员-Java模拟银行业务
- [C++]vector的存储方式和自增长问题
- UVA 11549 Calculator Conundrum
- QT4.8.1和Qt4.8.4编译X86出错:/usr/include/linux/futex.h:96: 错误:‘u32’ 在此作用域中尚未声明
- 使用poi操作导入excel代码示例
- 【iPone(iOS)相关开发】