您的位置:首页 > 运维架构 > Linux

3、Linux程序设计入门--文件操作

2012-03-18 23:38 274 查看
Linux下文件的操作

前言:

我们在这一节将要讨论linux下文件操作的各个函数.

文件的创建和读写

文件的各个属性

目录文件的操作

管道文件


----------------------------------------------------------------------------

文件描述符用来表示一个打开的文件,文件描述符是一个非负整数。一般程序中默认打开了的文件描述符为

0:STDIN_FILENO,
1: STDOUT_FILENO,
2:STDERR_FILENO
1、文件的创建和读写

我假设你已经知道了标准级的文件操作的各个函数(fopen,fread,fwrite等等).当然如果你不清楚的话也不要着急.我们讨论的系统级的文件操作实际上是为标准级文件操作服务的.

当我们需要打开一个文件进行读写操作的时候,我们可以使用系统调用函数open.使用完成以后我们调用另外一个close函数进行关闭操作.

[cpp] view
plaincopy

#include <fcntl.h>

#include <unistd.h>

#include <sys/types.h>

#include <sys/stat.h>

int open(const char *pathname,int flags);

int open(const char *pathname,int flags,mode_t mode);

int close(int fd);

open函数有两个形式.其中pathname是我们要打开的文件名(包含路径名称,缺省是认为在当前路径下面).flags可以去下面的一个值或者是几个值的组合.

O_RDONLY: 以只读的方式打开文件.

O_WRONLY:以只写的方式打开文件.

O_RDWR: 以读写的方式打开文件.

O_APPEND: 以追加的方式打开文件.

O_CREAT: 创建一个文件.

O_EXEC: 如果使用了O_CREAT而且文件已经存在,就会发生一个错误.

O_NOBLOCK:以非阻塞的方式打开一个文件.

O_TRUNC: 如果文件已经存在,则删除文件的内容.
前面三个标志只能使用任意的一个.如果使用了O_CREATE标志,那么我们要使用open的第二种形式.还要指定mode标志,用来表示文件的访问权限.mode可以是以下情况的组合.

-----------------------------------------------------------------

S_IRUSR 用户可以读 S_IWUSR 用户可以写

S_IXUSR 用户可以执行 S_IRWXU 用户可以读写执行
-----------------------------------------------------------------

S_IRGRP 组可以读 S_IWGRP 组可以写

S_IXGRP组可以执行 S_IRWXG 组可以读写执行
-----------------------------------------------------------------

S_IROTH其他人可以读 S_IWOTH 其他人可以写

S_IXOTH 其他人可以执行 S_IRWXO 其他人可以读写执行
-----------------------------------------------------------------

S_ISUID 设置用户执行ID S_ISGID 设置组的执行ID
-----------------------------------------------------------------

我们也可以用数字来代表各个位的标志.Linux总共用5个数字来表示文件的各种权限.00000.第一位表示设置用户ID.第二位表示设置组ID,第三位表示用户自己的权限位,第四位表示组的权限,最后一位表示其他人的权限.每个数字可以取1(执行权限),2(写权限),4(读权限),0(什么也没有)或者是这几个值的和

..

比如我们要创建一个用户读写执行,组没有权限,其他人读执行的文件.设置用户ID位那么我们可以使用的模式是--1(设置用户ID)0(组没有设置)7(1+2+4)0(没有权限,使用缺省) 5(1+4)即10705:open("temp",O_CREAT,10705);

如果我们打开文件成功,open会返回一个文件描述符.我们以后对文件的所有操作就可以对这个文件描述符进行操作了.

当我们操作完成以后,我们要关闭文件了,只要调用close就可以了,其中fd是我们要关闭的文件描述符.

文件打开了以后,我们就要对文件进行读写了.我们可以调用函数read和write进行文件的读写.

#include <unistd.h>
ssize_t read(int fd, void *buffer,size_t count);
ssize_t write(int fd, const void *buffer,size_t count);


fd是我们要进行读写操作的文件描述符,buffer是我们要写入文件内容或读出文件内容的内存地址.count是我们要读写的字节数.

对于普通的文件read从指定的文件(fd)中读取count字节到buffer缓冲区中(记住我们必须提供一个足够大的缓冲区),同时返回count.

如果read读到了文件的结尾或者被一个信号所中断,返回值会小于count.

如果是由信号中断引起返回,而且没有返回数据,read会返回-1,且设置errno为EINTR.

当程序读到了文件结尾的时候,read会返回0.

write从buffer中写count字节到文件fd中,成功时返回实际所写的字节数.

下面我们学习一个实例,这个实例用来拷贝文件.

[cpp] view
plaincopy

#include <unistd.h>

#include <fcntl.h>

#include <stdio.h>

#include <sys/types.h>

#include <sys/stat.h>

#include <errno.h>

#include <string.h>

#define BUFFER_SIZE 1024

int main(int argc,char **argv)

{

int from_fd,to_fd;

int bytes_read,bytes_write;

char buffer[BUFFER_SIZE];

char *ptr;

if(argc!=3)

{

fprintf(stderr,"Usage:%s fromfile tofile/n/a",argv[0]);

exit(1);

}

/* 打开源文件 */

if((from_fd=open(argv[1],O_RDONLY))==-1)

{

fprintf(stderr,"Open %s Error:%s/n",argv[1],strerror(errno));

exit(1);

}

/* 创建目的文件 */

if((to_fd=open(argv[2],O_WRONLY|O_CREAT,S_IRUSR|S_IWUSR))==-1)

{

fprintf(stderr,"Open %s Error:%s/n",argv[2],strerror(errno));

exit(1);

}

/* 以下代码是一个经典的拷贝文件的代码 */

while(bytes_read=read(from_fd,buffer,BUFFER_SIZE))

{

/* 一个致命的错误发生了 */

if((bytes_read==-1)&&(errno!=EINTR))

break;

else if(bytes_read>0)

{

ptr=buffer;

while(bytes_write=write(to_fd,ptr,bytes_read))

{

/* 一个致命错误发生了 */

if((bytes_write==-1)&&(errno!=EINTR))break;

/* 写完了所有读的字节 */

else if(bytes_write==bytes_read) break;

/* 只写了一部分,继续写 */

else if(bytes_write>0)

{

ptr+=bytes_write;

bytes_read-=bytes_write;

}

}

/* 写的时候发生的致命错误 */

if(bytes_write==-1)break;

}

}

close(from_fd);

close(to_fd);

exit(0);

}

2、文件的各个属性

文件具有各种各样的属性,除了我们上面所知道的文件权限以外,文件还有创建时间,大小等等属性.

有时侯我们要判断文件是否可以进行某种操作(读,写等等).这个时候我们可以使用access函数.

[cpp] view
plaincopy

#include <unistd.h>

int access(const char *pathname,int mode);

pathname:是文件名称;mode是我们要判断的属性.可以取以下值或者是他们的组合.

R_OK文件可以读,

W_OK文件可以写

X_OK文件可以执行,

F_OK文件存在.

当我们测试成功时,函数返回0,否则如果有一个条件不符时,返回-1.

如果我们要获得文件的其他属性,我们可以使用函数stat或者fstat.

[cpp] view
plaincopy

#include <sys/stat.h>

#include <unistd.h>

int stat(const char *file_name,struct stat *buf);

int fstat(int filedes,struct stat *buf);

struct stat {

dev_t st_dev; /* 设备 */

ino_t st_ino; /* 节点 */

mode_t st_mode; /* 模式 */

nlink_t st_nlink; /* 硬连接 */

uid_t st_uid; /* 用户ID */

gid_t st_gid; /* 组ID */

dev_t st_rdev; /* 设备类型 */

off_t st_off; /* 文件字节数 */

unsigned long st_blksize; /* 块大小 */

unsigned long st_blocks; /* 块数 */

time_t st_atime; /* 最后一次访问时间 */

time_t st_mtime; /* 最后一次修改时间 */

time_t st_ctime; /* 最后一次改变时间(指属性) */

};

stat用来判断没有打开的文件,而fstat用来判断打开的文件.我们使用最多的属性是st_mode.通过着属性我们可以判断给定的文件是一个普通文件还是一个目录,连接等等.可以使用下面几个宏来判断.

S_ISLNK(st_mode): 是否是一个连接.

S_ISREG 是否是一个常规文件.

S_ISDIR 是否是一个目录

S_ISCHR 是否是一个字符设备.

S_ISBLK 是否是一个块设备

S_ISFIFO 是否 是一个FIFO文件.

S_ISSOCK 是否是一个SOCKET文件.

我们会在下面说明如何使用这几个宏的.

3、目录文件的操作

在我们编写程序的时候,有时候会要得到我们当前的工作路径。C库函数提供了getcwd来解决这个问题。

[cpp] view
plaincopy

#include <unistd.h>

char *getcwd(char *buffer,size_t size);

我们提供一个size大小的buffer,getcwd会把我们当前的路径考到buffer中.如果buffer太小,函数会返回-1和一个错误号.

Linux提供了大量的目录操作函数,我们学习几个比较简单和常用的函数.

[cpp] view
plaincopy

#include <dirent.h>

#include <unistd.h>

#include <fcntl.h>

#include <sys/types.h>

#include <sys/stat.h>

int mkdir(const char *path,mode_t mode);

DIR *opendir(const char *path);

struct dirent *readdir(DIR *dir);

void rewinddir(DIR *dir);

off_t telldir(DIR *dir);

void seekdir(DIR *dir,off_t off);

int closedir(DIR *dir);

struct dirent {

long d_ino;

off_t d_off;

unsigned short d_reclen;

char d_name[NAME_MAX+1]; /* 文件名称 */

mkdir 很容易就是我们创建一个目录,

opendir 打开一个目录为以后读做准备.

readdir 读一个打开的目录.

rewinddir 是用来重读目录的和我们学的rewind函数一样.

closedir 是关闭一个目录.

telldirseekdir类似与ftee和fseek函数.

下面我们开发一个小程序,这个程序有一个参数.如果这个参数是一个文件名,我们输出这个文件的大小和最后修改的时间,如果是一个目录我们输出这个目录下所有文件的大小和修改时间.

[cpp] view
plaincopy

#include <unistd.h>

#include <stdio.h>

#include <errno.h>

#include <sys/types.h>

#include <sys/stat.h>

#include <dirent.h>

#include <time.h>

static int get_file_size_time(const char *filename)

{

struct stat statbuf;

if(stat(filename,&statbuf)==-1)

{

printf("Get stat on %s Error:%s/n",

filename,strerror(errno));

return(-1);

}

if(S_ISDIR(statbuf.st_mode))

return(1);

if(S_ISREG(statbuf.st_mode))

printf("%s size:%ld bytes/tmodified at %s",filename,statbuf.st_size,ctime(&statbuf.st_mtime));

return(0);

}

int main(int argc,char **argv)

{

DIR *dirp;

struct dirent *direntp;

int stats;

if(argc!=2)

{

printf("Usage:%s filename/n/a",argv[0]);

exit(1);

}

if(((stats=get_file_size_time(argv[1]))==0)||(stats==-1))

exit(1);

if((dirp=opendir(argv[1]))==NULL)

{

printf("Open Directory %s Error:%s/n",argv[1],strerror(errno));

exit(1);

}

while((direntp=readdir(dirp))!=NULL)

{

if(get_file_size_time(direntp-<d_name)==-1)

break;

}

closedir(dirp);

exit(1);

}

4、管道文件

Linux提供了许多的过滤和重定向程序,比如more cat等等.还提供了< > | <<等等重定向操作符.在这些过滤和重 定向程序当中,都用到了管道这种特殊的文件.系统调用pipe可以创建一个管道.

[cpp] view
plaincopy

#include<unistd.h>

int pipe(int fildes[2]);

pipe调用可以创建一个管道(通信缓冲区).当调用成功时,我们可以访问文件描述符fildes[0],fildes[1].其中fildes[0]是用来读的文件描述符,而fildes[1]是用来写的文件描述符.

在实际使用中我们是通过创建一个子进程,然后一个进程写,一个进程读来使用的.

关于进程通信的详细情况请查看进程通信

[cpp] view
plaincopy

#include <stdio.h>

#include <stdlib.h>

#include <unistd.h>

#include <string.h>

#include <errno.h>

#include <sys/types.h>

#include <sys/wait.h>

#define BUFFER 255

int main(int argc,char **argv)

{

char buffer[BUFFER+1];

int fd[2];

if(argc!=2)

{

fprintf(stderr,"Usage:%s string/n/a",argv[0]);

exit(1);

}

if(pipe(fd)!=0)

{

fprintf(stderr,"Pipe Error:%s/n/a",strerror(errno));

exit(1);

}

if(fork()==0)

{

close(fd[0]);

printf("Child[%d] Write to pipe/n/a",getpid());

snprintf(buffer,BUFFER,"%s",argv[1]);

write(fd[1],buffer,strlen(buffer));

printf("Child[%d] Quit/n/a",getpid());

exit(0);

}

else

{

close(fd[1]);

printf("Parent[%d] Read from pipe/n/a",getpid());

memset(buffer,'/0',BUFFER+1);

read(fd[0],buffer,BUFFER);

printf("Parent[%d] Read:%s/n",getpid(),buffer);

exit(1);

}

}

为了实现重定向操作,我们需要调用另外一个函数dup2.

[cpp] view
plaincopy

#include <unistd.h>

int dup2(int oldfd,int newfd);

dup2将用oldfd文件描述符来代替newfd文件描述符,同时关闭newfd文件描述符.也就是说,所有向newfd操作都转到oldfd上面.下面我们学习一个例子,这个例子将标准输出重定向到一个文件.

[cpp] view
plaincopy

#include <unistd.h>

#include <stdio.h>

#include <errno.h>

#include <fcntl.h>

#include <string.h>

#include <sys/types.h>

#include <sys/stat.h>

#define BUFFER_SIZE 1024

int main(int argc,char **argv)

{

int fd;

char buffer[BUFFER_SIZE];

if(argc!=2)

{

fprintf(stderr,"Usage:%s outfilename/n/a",argv[0]);

exit(1);

}

if((fd=open(argv[1],O_WRONLY|O_CREAT|O_TRUNC,S_IRUSR|S_IWUSR))==-1)

{

fprintf(stderr,"Open %s Error:%s/n/a",argv[1],strerror(errno));

exit(1);

}

if(dup2(fd,STDOUT_FILENO)==-1)

{

fprintf(stderr,"Redirect Standard Out Error:%s/n/a",strerror(errno));

exit(1);

}

fprintf(stderr,"Now,please input string");

fprintf(stderr,"(To quit use CTRL+D)/n");

while(1)

{

fgets(buffer,BUFFER_SIZE,stdin);

if(feof(stdin))

[cpp] view
plaincopy

break;

write(STDOUT_FILENO,buffer,strlen(buffer));

}

exit(0);

}

好了,文件一章我们就暂时先讨论到这里,学习好了文件的操作我们其实已经可以写出一些比较有用的程序了.我们可以编写一个实现例如dir,mkdir,cp,mv等等常用的文件操作命令了.想不想自己写几个试一试呢?

补充函数:

scandir函数:读取特定的目录数据

表头文件:#include <dirent.h>

定义函数:int scandir(const char *dir, struct dirent **namelist, int (*select) (const struct dirent *), int (*compar) (const struct dirent **, const struct dirent**));

函数说明: scandir()会扫描参数dir指定的目录文件,经由参数select指定的函数来挑选目录结构至参数namelist数组中,最后再调用参数compar指定的函数来排序namelist 数组中的目录数据。每次从目录文件中读取一个目录结构后便将此结构传给参数select所指的函数,
select函数若不想要将此目录结构复制到namelist数组就返回0,若select为空指针则代表选择所有的目录结构。scandir()会调用 qsort()来排序数据,参数compar则为qsort()的参数,若是要排列目录名称字母则可使用alphasort(). 结构dirent定义请参考readdir()

返回值 :成功则返回复制到namelist数组中的数据结构数目,有错误发生则返回-1

错误代码:ENOMEM 核心内存不足

stat函数:读取指定文件的属性

表头文件: #include <sys/stat.h>

#include <unistd.h>

定义函数: int stat(const char *file_name, struct stat *buf);

函数说明: 通过文件名filename获取文件信息,并保存在buf所指的结构体stat中

返回值: 执行成功则返回0,失败返回-1,错误代码存于errno

stat详细

判断文件属性的几个宏:
S_ISLNK(st_mode): 是否是一个连接.

S_ISREG 是否是一个常规文件.

S_ISDIR 是否是一个目录

S_ISCHR 是否是一个字符设备.

S_ISBLK 是否是一个块设备

S_ISFIFO 是否是一个FIFO文件.

S_ISSOCK 是否是一个SOCKET文件
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息