Linux学习日记(十五)—— Ubuntu下基本文件操作

                                             Ubuntu下基本文件操作

linux系统环境下可以使用C语言进行编程,要操作该环境下的文件可以使用C便准库定义的接口函数,也可以使用POSIX标准定义的接口函数。其中:open()、read()、write()、lseek()、close()这组函数接口的特点是不带缓存,能够直接对文件(设备)进行操作,属于POSIX标准定义的接口函数,不属于ANSI C定义的接口函数。

函数说明

1)open()函数用于打开或创建文件,在打开或创建文件时可以指定文件属性及用户权         限

2)close()函数用于关闭被打开的文件

3)read()函数用于从一个指定文件读取指定长度的数据到一个指定的缓冲区

4)write()函数用于向一个打开的文件写入数据,写操作是从文件的当前位置开始的

5)lseek()函数用于在指定的文件描述符中将文件指针定位到相应的位置

open()函数

       相关头文件:

              #include<sys/types.h>

              #include<sys/stat.h>

              #include<fcntl.h>

       函数原型:

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

       函数参数:

              pathname:

                     带路径的文件名

              flags:

                     文件打开方式,可选参数包括:O_RDONLY、O_WRONLY、O_RDWR、O_CREAT、                            O_EXCL、O_NOCTTY、O_TRUNC、O_APPEND

              mode:

                     存取操作权限,可选参数包括:S_I[R/W/X][USR/GRP/OTH]

                            例如:S_IRUSR | S_IWUSR 表示文件所有者可读可写

                     也可以使用数字表示法

       函数返回值:

              成功:

                     返回文件描述符

              失败:

                     -1

      close()函数

       相关头文件:

              #include<unistd.h>

       函数原型:

              int close(int fd);

       函数参数:

              fd:

                     文件描述符

       函数返回值:

              成功:

                     0

              失败:

                     -1

 

read ()函数

       相关头文件:

              #include<unistd.h>

       函数原型:

              ssize_t read(int fd, void *buf, size_t count);

       函数参数:

              fd:

                     文件描述符

              buf:

                     指定的数据缓冲区

              count:

                     读取数据的指定大小

       函数返回值:

              成功:

                     读取到的字节数

              文件尾:

                     0

              出错:

                     -1

 

write ()函数

       相关头文件:

              #include<unistd.h>

       函数原型:

              ssize_t write(int fd, const void *buf, size_t count);

       函数参数:

              fd:

                     文件描述符

              buf:

                     指定的数据缓冲区

              count:

                     写入数据的指定大小

       函数返回值:

              成功:

                     写入的字节数

              出错:

                     -1

 

lseek ()函数

       相关头文件:

              #include<unistd.h>

              #include<sys/types.h>

       函数原型:

              off_t lseek(int fd, off_t offset, int whence);

       函数参数:

              fd:

                     文件描述符

              offset:

                     偏移量,单位为字节

              whence:

                     当前的位置基点,可选项包括:SEEK_SET、SEEK_CUR、SEEK_END

       函数返回值:

              成功:

                     文件的当前位移

              出错:

                     -1

       示例代码

#include<sys/types.h>

#include<sys/stat.h>

#include<fcntl.h>

#include<unistd.h>

#include<stdio.h>

#include<stdlib.h>





int main(void)

{

        int flag,ret;

        char readBuff[2048];



        // 判断文件是否存在 需要O_CREAT、O_EXCL两个flag同时使用才能在open()函数调用过程中进行文件存在性判断

        // 以读写|创建模式 打开文件

        flag= open("test.txt",O_RDWR|O_CREAT,0777); // 以读写方式打开文件,如果文件不存在则创建该文件

        // 返回值测试

        if(-1 == flag)

        {

                perror("open file");

                exit(1);

        }



        // 数据写入 write()

        // 函数原型:ssize_t write(int fd,const void* buf,size_t count);

        ret= write(flag,"123456",7);

        if(-1 == ret)

        {

                perror("write file");

                // 关闭文件

                ret= close(flag);//依据文件描述符关闭文件

                if(-1 == ret)

                {

                        perror("close file");

                        exit(1);

                }



        }else

        {

                printf("\r\nwriting ok\r\n");

        }

        // 关闭文件

        ret= close(flag);//依据文件描述符关闭文件

        if(-1 == ret)

        {

                perror("close file");

                exit(1);

        }



        // read()函数的使用

        // 函数原型: ssize_t read(int fd,void* buf,size_t count);

        flag= open("test.txt",O_RDWR);

        ret= read(flag,readBuff,1024);

        if(-1 == ret)

        {

                perror("read file");

                ret= close(flag);

                if(-1 == ret)

                {

                        perror("close file");

                        exit(1);

                }



        }

        printf("get: %s\r\n",readBuff);



        printf("ret= %d\n",ret);



        // 关闭文件

        ret= close(flag);//依据文件描述符关闭文件

        if(-1 == ret)

        {

                perror("close file");

                exit(1);

        }





        return 0;

}









 

猜你喜欢

转载自blog.csdn.net/smallerxuan/article/details/81950711