1、2 linux文件编程(系统调用)

 

1、文件编程

       Linux中文件编程可以使用两种方法:

²        Linux系统调用

²        C语言库函数

  前者依赖于linux系统,后者与操作系统是独立的,在任何操作系统下,使用C语言库函数操作文件的方法是相同的。

 

       2、系统调用——创建

int creat(const char  *filename,mode_t mde)

    *      filename:要创建的文件名(包含路径,缺省为当前路径

    *      mode :创建模式

返回值:成功返回文件描述符,失败-1.

常见创建模式:

      S­_IRUSR 可读

     S_IWUSR 可写

      S_IXUSR  可执行

      S_IRWXU 可读、可写、可执行

        可读 4  可写 2 可执行 1  无任何权限 0

 

例:file_creat.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,0666)<0)               //返回值:成功时返回打开文件的描述符,//若出错则返回-1

    {

        printf("create file %s failure!\n",filename);

        exit(EXIT_FAILURE);

    }

    else

    {

        printf("create file %s success!\n",filename);

    }

}

 

int main(int argc,char *argv[])   //参数argc的值保存在argv[]中

{

    /*判断入参有没有传入文件名 */

   if(argc<2)                   //本程序文件名也算一个参数

    {

        printf("you haven't input the filename,please try again!\n");

        exit(EXIT_FAILURE);

    }

    create_file(argv[1]);   

    exit(EXIT_SUCCESS);

}

运行

# gcc file_creat.c –o file_creat

# ./file_creat  hello                     //在当前目录中创建一个hello文件

 

 

              文件描述:

       在linux系统中,所有打开的文件都对应一个文件描述符。文件描述符的本质是一个非负整数。当打开一个文件时,该证书由系统来分配。文件描述符的范围是0—OPEN-MAX.现在很多系统的OPEN-MAX=1023.表示最多同时打开1024个文件。

 

 

              3、系统调用—打开

       #include<fcntl.h>

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

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

Pathname:要打开的文件名(包含路径,没有则为当前路径)

   返回值:若成功返回文件描述符,若失败返回-1.

flags: 打开标志

          常见的打开标志

   O_   RDONLY  (0)     只读方式打开

   O_   WRONLY  (1)   只写方式打开

   O_   RDWR     (2)  读写方式打开

   O_   APPEND      追加方式打开

   O_   CREAT       创建一个文件

   O_   NOBLOCK    非阻塞方式打开

如果使用了O_  CREATE标志,则使用函数是:int open (const car *pathname,int flags,mode_t mode);

这时还需要指定mode来表示文件的访问权限。

 

例题:file_open.c

          #include<stdio.h>

          #include<stdlib.h>

          #include<sys/types.h>

          #include<sys/stst.h>

          #include<fcntl.h>

          int main(int argc ,char *argv[])

          {

                 int fd;

          if (argc<2)

          {puts(“please input the open file parhname!\n”);

          exit(1);}

                        //如果flag参数中有O_CREAT表示,该文件如果不存在,则创建此文件,//   该文件的权限由第三个参数决定。

                        //如果没有O_CREAT参数,则第三个参数不起作用,此时,如果要打开//不存在的文件,系统会报错

                 if(fd=open(argv[1], O_CREAT| O_RDWR,0755)<0)  //打开失败,返回-1

                 {

                               perror(“open file failure!\n”);

                               Exit(1);

}

else {

printf(“open file %d success\n”,fd);

}

close(fd);

exit(0);

}

 

       4、系统调用——读

int   read (int fd,const void *buf ,size_t length) //若成功返回读到的字节数,//若到文件结尾返回0,若失败返回-1

功能:

从文件描述符fd所指定的文件中读取length个字节到buf所指定的缓冲区中,返回值为实际读取的字节。

 

       5、系统调用—写

int write(int fd,const void *buf,size_t length)  //若成功返回读到的字节数,若失//败返回-1

功能:

把length个字节从buf指向的缓冲区写到fd所指向的文件中。

 

       6、系统调用—定位

int lseek(int fd,offset_t offset ,int whence)     //成功返回文件指针相对于

//件的位置,失败返回-1

功能:将文件读写指针相对whence移动offset个字节。

由于lseek函数的返回值为文件指针相对于头文件的位置,因此下面调用的返回值就是文件的长度

lseek(fd,0,SEEK_END

 

whence可以使用下面的值:

SEEK_SET 相对文件开头

SEEK_CUR 相对文件读写指针的当前位置

SEEK_END 相对文件末尾

 

offset可以取负值,表示向前移动,例如下述为文件指针相对当前位置向前移动5个字节。

lseek(fd,-5,SEEK_CUR)

 

              7、系统调用—访问判断

       判断文件是否可读,可写。。。。等操作。

 int  access(const char *pathname,int mode)

pathname 为文件名称,包含路径

mode    判断文件的访问权限。有:R_OK,文件可读。W_OK,可写 X_OK ,可执行。F_OK,文件存在

返回值:成功返回0.失败-1

例如:

    #include<unistd.h>

    int main()

                {

    if (assess(“/etc/passwd”,R_OK)==0)

 printf(“/etc/passwd can be reaf!\n”);

}

8、关闭

#include<inistd.h>

       int close (int filedes);

返回值:成功返回0 ,失败-1.

 

 

 

综合实例:file_cp.c

#include <sys/types.h>

#include <sys/stat.h>

#include <stdio.h>

#include <fcnl.h>

#include<errno.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)

    {

        printf("Usage:%s fromfile tofile\n",argv[0]);

        exit(1);

    }

 

/* 打开源文件 */

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

{

    fprintf("Open %s Error\n",argv[1]);

    exit(1);

}

 

/* 创建目的文件 */

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

{

    fprintf("Open %s Error\n",argv[2]);

    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);

}

    文件的拷贝。

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值