文件I/O编程学习笔记一(基于linux系统的文件编程)

文件I/O编程


1 基于linux系统的文件编程 


创建文件
int creat(const char * filename,mode_t mode)
filename:要创建的文件名
mode:创建模式
常见模式:
S_IRUSR 可读 4
S_IWUSR 可写 2
S_IXUSR 可执行 1

S_IRWXU 可读、写、执行

返回值为文件描述符,变化范围为0~file-max
文件描述符的限制有两个方面:用户级限制和系统级限制
查看本次会话文件描述符限制 ulimit -n
   临时修改:ulimit -SHn 10240000
   永久修改:/etc/security/limits.conf 



示例代码如下:
[retacn@localhost tmp]$ vi file_create.c
#include <stdio.h>
#include <stdlib.h>


#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>


void create_file(char *filename){
/*指定创建文件的属性*/
        if(creat(filename,0755)<0){
                printf("create file %s failure!\n",filename);
                exit(EXIT_FAILURE);
        }else{
                printf("create file %s success!\n",filename);
        }
}
int main(int argc,char *argv[]){
        int i;
        if(argc<2){
                perror("you haven't input the filename ,please try again!\n");
                exit(EXIT_FAILURE);
        }
        for(i=1;i<argc;i++){
                create_file(argv[i]);
        }
        exit(EXIT_SUCCESS);
        return 0;
}


编译
[retacn@localhost tmp]$ gcc file_create.c -o file_create
运行程序创建文件
[retacn@localhost tmp]$ ./file_create hello
create file hello success!




文件描述
所有打开的文件对应一个文件描述符


打开文件
函数原型
int open(const char *pathname,int flags)
int open(const char *pathname,int flags,mode_t mode)
pathname:要打开的文件名(包含路径,缺省为当前路径)
flags:打开标志
常见的打开标志:
O_RDONLY  只读方式打开
O_WRONLY  只写
O_RDWR    读写
O_CREAT  若打开文件不存在,则自动创建(需要指定文件的访问权限mode)
O_EXCL    如果O_CREAT被设置,此指令会检查文件是否存在
O_NOCTTY  如果打开的文件为终端设备,则不会将该终端机当成进程控制终端机
O_TRUNC   若文件存在并且以可写的方式打开,此时文件长度清零,资料消失
O_APPEND  追加方式打开
O_NONBLOCK 非阻塞方式打开,无论有无数据读取或等待,立即返回进程中


示例代码如下:
[retacn@localhost tmp]$ vi file_open.c
#include <stdio.h>
#include <stdlib.h>


#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>


int main(int argc,char *argv[]){
        int fd;
        if(argc<2){
                puts("please input the open file pathname!\n");
                exit(1);
        }
        if((fd=open(argv[1],O_CREAT | O_RDWR,0755))<0){
                perror("open file failure!\n");
                exit(1);
        }else{
                printf("open file %d success!\n",fd);
        }
        close(fd);
        exit(0);
        return 0;
}


[retacn@localhost tmp]$ gcc file_open.c -o file_open
[retacn@localhost tmp]$ ./file_open hello
open file 3 success!




文件的关闭
int close(int fd)
fd:文件描述符




读取文件内容
int read(int fd,const void *buf,size_t length)
fd:文件描述符
buf:读取数据放入缓冲区
length:数据长度


向文件中写入数据
int write(int fd,const void *buf,size_t length)


移动文件的读写位置
int lseek(int fd,offset_t offset,int whence)
文件的读写指针相对whence移动offset个字节
offset:如果取负值,表示向前移动
whence参数值:
SEEK_SET:相对文件开头
SEEK_CUR:相对文件读写指针的当前位置
SEEK_END:相对文件末尾
示例代码:
取得文件长度:
lseek(fd,0,SEEK_END)


文件的访问判断
int access(const char *pathname,int mode)
pathname:文件名
mode:要判断的访问权限
R_OK 可读
W_OK 可写
X_OK 可执行
F_OK 文件存在
正常返回0




示例代码如下:
/*实现文件的考贝*/
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <errno.h>


#include <sys/types.h>
#include <sys/stat.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);
        return0;
}


[retacn@localhost tmp]$ gcc file_cp.c -o file_cp

[retacn@localhost tmp]$ ./file_cp cp_test cp_test2


文件的描述词
int fcntl(int fd,int cmd)
int fcntl(int fd,int cmd,long arg)
int fcntl(int fd,int cmd,struct flock *lock)
fd:表示要设置的文件描述词
cmd:要操作的指令
 


文件的锁定
int flock(int fd,int operation)
fd:目标文件
operation:锁定、解除操作

posted @ 2013-01-26 19:55  retacn_yue  阅读(195)  评论(0编辑  收藏  举报