一,文件IO基础
1,open(2或3)函数原型
open通常用来打开一个文件。
返回值:
>=0
文件成功打开,返回文件描述符。
=-1
文件打开失败。
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
int open(const char *pathname, int flags, mode_t mode);
const char *pathname//相应文件,设备的路径。
int flags//打开文件的一些选项。
必选:O_RDONLY(只读),O_WRONLY(只写),O_RDWR(读写)
可选:O_APPEND 每次写时都追加到文件的尾端。
O_CREAT 文件不存在则创建它,使用该选项需要第三个参数mode
O_TRUNC 如果文件存在,而且为只写或读写成功打开,则将其长度截取为0;
O_NONBLOCK 如果path是一个FIFO、块设备、字符特殊文件,此选项将为文件的本次打开和后续的I/O操作
设置非阻塞模式方式。
mode_t mode//若int flags选择了O_CREAT选项创建文件,这时必须指定创建文件的权限模式,如0660。否则不需要mode这一参数。
2,create(2)函数原型
创建一个新文件,如果原来该文件存在,会将这个文件的长度截短为0(即覆盖掉了)。
返回值:
>=0
文件成功打开,返回文件描述符。
=-1
创建文件失败。
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
int creat(const char *pathname, mode_t mode);
const char *pathname//相应文件,设备的路径,或当前工作路径下的文件名。
mode_t mode//创建的文件的访问权限模式。
//其等效于
open(pathname, O_WRONLY | O_CREAT | O_TRUNC, mode);
3,close(1)函数原型
用来关闭一个文件。
返回值:
=0
文件成功关闭。
=-1
关闭文件失败。
#include <unistd.h>
int close(int fd);
int fd//对应文件的文件描述符。
4,write(3)函数原型
对文件进行写操作。
返回值:
>=0
成功写入文件,返回写入的字节数有多少。
=-1
写入文件失败。
#include <unistd.h>
ssize_t write(int fd, const void *buf, size_t count);
int fd//要写入的文件对应的文件描述符。
const void *buf//要写入的内容的地址。
size_t count//写入内容的大小。
5,read(3)函数原型
用来读取一个文件的内容。
返回值:
>=0
成功读取文件,返回读取了多少字节。
=-1
读取文件失败。
#include <unistd.h>
ssize_t read(int fd, void *buf, size_t count);
int fd//要读的文件对应的文件描述符。
void *buf//读出的内容存入此buf之中。
size_t count//从文件中读取多少字节的内容。
6,dup(1)函数原型
用来复制文件描述符,使多个文件描述符指向同一个文件。dup()返回的新文件描述符一定是当前可用文件描述符中的最小数值。
返回值:
>=0
成功,返回复制了的文件描述符。
=-1
失败。
#include <unistd.h>
int dup(int oldfd);
int oldfd//要复制的文件描述符。
7,dup2(2)函数原型
复制文件描述符,使多个文件描述符指向同一个文件。这两个系统调用经常用在标准输入、标准输出、标准出错重定向。dup2可以用newfd参数来指定新的文件描述符。如果newfd已经打开,则先将其关闭,再复制文件描述符。如果newfd等于oldfd,则dup2返回newfd, 而不关闭它。
返回值:
>=0
成功,返回复制了的文件描述符或和oldfd相同的newfd。
=-1
失败。
#include <unistd.h>
int dup2(int oldfd, int newfd);
int oldfd//要复制的文件描述符。
int newfd//指定的新文件描述符。
二,出错处理
1,perror(1)函数原型
用来打印错误信息,但是perror()的使用功能比较简单且只能打印到标准输出上,无法格式化控制输出或者打印到日志上。
返回值:无返回值。
#include <stdio.h>
#include <errno.h>
void perror(const char *s);
const char *s//一个字符串参数s。他会将错误的原因打印到标准输出上,其内容是字符串s,后面紧跟冒号和字符串形式的错误原因。需要注意的是字符串s里不要加换行符,因为错误原因里面会自带换行。
2,strerror(1)函数原型
打印错误信息,相比与perror(),它用来将整形类型的错误原因errno装换成相应的字符串形式,这样在任何使用字符串的地方我们都可以使用它了。
返回值:返回值为char * 类型 。指向描述错误的错误字符串的指针。
#include <string.h>
#include <errno.h>
char *strerror(int errnum);
int errnum//发生的错误相对应的整形值。
三,文件偏移量
1,lseek(3)函数原型
用来调整文件偏移量,文件偏移量是类似与word中光标一样的东西,每次read,write的时候文件偏移量的位置都会被重新设置,lseek便是用来调整这个“光标”的位置的。
返回值:lseek允许超过文件结尾设置偏移量,文件会因此被扩展!
>=0
成功:返回较起始位置偏移量(可以用来获取文件的大小)
=-1
失败
#include <sys/types.h>
#include <unistd.h>
off_t lseek(int fd, off_t offset, int whence);
int fd//要调整文件偏移量的文件所对应的文件描述符。
off_t offset//偏移多少,即偏移量。
int whence//文件偏移的起始位置。
SEEK_SET 文件头:往后offset位
SEEK_CUR 当前位置:当前位置往后offset位
SEEK_END 文件尾:文件尾往前offset位
四,文件和目录
1,stat(2)函数原型
用来返回文件或目录的相关信息。返回fd对应的文件信息并且存储到struct stat结构体中。
返回值:
=0
成功
=-1
失败
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
int stat(const char *pathname, struct stat *statbuf);
const char *pathname//要查看相关信息的文件名。
struct stat *statbuf//存储信息的结构体。
//存储文件信息的结构体
struct stat
{
dev_t st_dev; /* ID of device containing file 文件设备编号*/
ino_t st_ino; /* inode number 节点号*/
mode_t st_mode; /* protection 文件的类型和存取的权限*/
nlink_t st_nlink; /* number of hard links 连到该文件的硬连接数目,刚建立的文件值为1*/
uid_t st_uid; /* user ID of owner 用户ID*/
gid_t st_gid; /* group ID of owner 组ID*/
dev_t st_rdev; /* device ID (设备类型)若此文件尾设备文件,则为其设备编号*/
off_t st_size; /* total size, in bytes 文件字节数(文件大小)*/
blksize_t st_blksize;/* blocksize for filesystem I/O
块大小(文件系统的I/O缓冲区大小),类型为unsigned long类型*/
blkcnt_t st_blocks; /* number of 512B blocks allocated
分配的512字节的块数,类型为unsigned long类型*/
time_t st_atime; /* time of last access 最后一个访问时间*/
time_t st_mtime; /* time of last modification 最后更改的时间*/
time_t st_ctime; /* time of last status change inode的更改时间*/
};
2,fstat(2)函数原型
用来返回文件或目录的相关信息。返回fd对应的文件并且存储到struct stat结构体中。stat和fstat不同的是fstat会打开对应的文件描述符。而stat不会。
返回值:
=0
成功
=-1
失败
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
int fstat(int fd, struct stat *statbuf);
int fd//对应文件的文件描述符。
struct stat *statbuf//存储信息的结构体。
3,lstat(2)函数原型
lstat函数类似于stat,但当文件是一个链接类型时,lstat返回该链接类型文件的有关信息,而不是由此链接引用的文件的信息。
返回值:
=0
成功
=-1
失败
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
int lstat(const char *pathname, struct stat *statbuf);
const char *pathname//对应文件的文件描述符。
struct stat *statbuf//存储信息的结构体。
4,access(2)函数原型
用来测试文件是否存在或测试其权限位。
返回值:
=0
成功
=-1
失败
#include <unistd.h>
int access(const char *pathname, int mode);
const char *pathname//相应的文件路径名。
int mode//要测试的模式,模式说明:
R_OK 测试读许可权
W_OK 测试写许可权
X_OK 测试执行许可权
F_OK 测试文件是否存在
5,unlink(1)函数原型
该系统调用可以用来删除文件,其本质是让文件的链接记数自减。调用该函数将使path指定的文件的链接数减1,如果对该文件还有其他链接存在,则仍可以通过其他链接访问该文件的数据。只有当链接记数达到0时,该文件的内容才可被删除。如果有进程打开了该文件,其内容也不能被删除。关闭一个文件时,内核首先检查打开该文件的进程个数,如果这个记数达到0,内核再去检查它的链接记数,如果记数也是0,那么就删除该文件内容。
返回值:
=0
成功
=-1
失败
#include <unistd.h>
int unlink(const char *pathname);
const char *pathname//相应的文件路径名。
6,rename(2)函数原型
用来重命名一个文件。
返回值:
=0
成功
=-1
失败
#include <stdio.h>
int rename(const char *oldpath, const char *newpath);
const char *oldpath//旧的文件路径名。
const char *newpath//新的文件路径名。
7,mkdir(2)函数原型
用来创建一个文件。
返回值:
=0
成功
=-1
失败
#include <sys/stat.h>
#include <sys/types.h>
int mkdir(const char *pathname, mode_t mode);
const char *pathname//文件路径名。
mode_t mode//文件权限。
8,rmdir(1)函数原型
用来删除一个文件夹。
返回值:
=0
成功
=-1
失败
#include <unistd.h>
int rmdir(const char *pathname);
const char *pathname//文件夹的名字。
9,opendir(1)函数原型
用来打开一个文件夹,并且返回一个DIR类型的结构体指针。
返回值:
=*DIR
成功返回一个*DIR
=-1
失败
#include <sys/types.h>
#include <dirent.h>
DIR *opendir(const char *name);
const char *name//文件夹的名字。
struct __dirstream
{
void *__fd;
char *__data;
int __entry_data;
char *__ptr;
int __entry_ptr;
size_t __allocation;
size_t __size;
__libc_lock_define (, __lock)
};
typedef struct __dirstream DIR;
10,readdir(1)函数原型
用来读取一个文件夹的信息并且返回保存到dirent结构体里面。
返回值:
=*dirent
成功返回一个struct *dirent指针
=-1
失败
#include <dirent.h>
struct dirent *readdir(DIR *dirp);
DIR *dirp//要被读取文件夹的DIR指针。
//文件夹信息
struct dirent
{
long d_ino; /* inode number 索引节点号 */
off_t d_off; /* offset to this dirent 在目录文件中的偏移 */
unsigned short d_reclen; /* length of this d_name 文件名长 */
unsigned char d_type; /* the type of d_name 文件类型 */
char d_name [NAME_MAX+1]; /* file name (null-terminated) 文件名,最长255字符 */
}
11,closedir(1)函数原型
用来关闭一个文件夹。
返回值:
=0
成功
=-1
失败
#include <sys/types.h>
#include <dirent.h>
int closedir(DIR *dirp);
DIR *dirp//文件夹对应的DIR指针。
12,chdir(1)函数原型
用来改变工作目录。
返回值:
=0
成功
=-1
失败
#include <unistd.h>
int chdir(const char *path);
const char *path//将文件目录更改到哪里。
13,fchdir(1)函数原型
用来改变工作目录。更改为 fd 所指定的目录。
返回值:
=0
成功
=-1
失败
#include <unistd.h>
int fchdir(int fd);
int fd//将进程的当前工作目录更改为 fd 文件描述符所指定的目录(譬如使用 open 函数打开一个目录)。