linux文件操作

来源:百度文库 编辑:神马文学网 时间:2024/04/28 22:48:11
一.文件类型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
#include
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
int access(const char *pathname, int mode);
返回:若成功则为0,若出错则为-1

umask函数为进程设置文件方式创建屏蔽字,并返回以前的值。
#include
#include
mode_t umask(mode_t cmask);
返回:以前的文件方式创建屏蔽字chmod和fchmod函数:改变存取许可权
#include
#include
int chmod(const char *pathname, mode_t mode);
int fchmod(int filedes, mode_t mode);
两个函数返回:若成功则为0,若出错则为-1chown、fchown和lchown函数:改变所有者和组所有者
#include
#include
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
#include
int truncate(const char *pathname, off_t length);
int ftruncate(int filedes, off_t length);
两个函数返回;若成功则为0,若出错则为-1rename:文件重命名
#include
int rename(const char *oldname, const char *newname);
返回:若成功则为0,若出错则为-1utime:修改文件时间
#include
#include
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
#include
int mkdir(const char *pathname, mode_t mode);
返回:若成功则为0,若出错则为-1函数rmdir删除一个空目录。
#include
int rmdir(const char *pathname);#include
#include
DIR *opendir(const char *pathname);
返回:若成功则为指针,若出错则为NULL
struct dirent *readdir(DIR *dir);
返回:若成功则为指针,若在目录尾或出错则为NULL
void rewinddir(DIR *dir);
int closedir(DIR *dir);
返回:若成功则为0,若出错则为-1定义在头文件中的dirent结构如下所示:
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
int chdir(const char *pathname);
int fchdir(int filedes);
char *getcwd(char *buf, size_t size);
两个函数的返回:若成功则为0,若出错则为-1
四、文件I/O接口概述
文件有5个操作:打开、关闭、读、写、定位。有两套接口都可以用来完成这些操作,一类是系统调用接口,对应的函数是open、close、read、write、lseek,另一类是标准I/O库接口,对应的函数是fopen、fclose、fread、fwrite、fseek。调用open函数可以打开或创建一个文件。
#include
#include
#include
int open(const char *pathname, int oflag,.../*, mode_t mode */);
返回:若成功为文件描述符,若出错为-1用creat函数创建一个新文件。
#include
#include
#include
int creat(const char * pathname, mode_t mode) ;
返回:若成功为只写打开的文件描述符,若出错为-1
此函数等效于:
open(pathname, O_WRONLY|O_CREAT|O_TRUNC, mode);close函数关闭一个打开文件:
#include
int close(int filedes);
返回:若成功为0,若出错为-1read函数从打开文件中读数据。
#include
ssize_t read(int filedes, void *buff, size_t nbyt s);
返回:读到的字节数,若已到文件尾为0,若出错为-1write函数向打开文件写数据。
#include
ssize_t write(int filedes, const void * buff, size_t nbytes);
返回:若成功为已写的字节数,若出错为-1用lseek显式地定位一个打开文件。
#include
#include
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可为正或负。