linux下open()   read()  write()  lseek()   close()   函数的用法

 linux下open()   read()  write()  lseek()   close()   函数的用法

 

open()函数的用法

以下是函数的原型

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

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

      int creat(const char *pathname, mode_t mode);

返回值 : open函数打开文件,打开成功返回一个文件描述符,打开失败,返回-1

*pathname:  要打开的文件名(含文件路径,缺省为当前路径)

  flags:O_RDONLY               只读打开

              O_RWONLY              只写打开

              O_RDWR                   可读可写打开

当我们附带了权限后,打开的文件就只能按照这种权限来操作

 以上三种常数中应当只指定一个,下列常数是可以选择的

  1. O_CREAT 若文件不存在则创建他,使用此选项时,需同时说明第三个参数mode。用其说明该文件的存取许可权限

  2. O_EXCL   如果同时指定了O_CREAT.  而文件存在则出错

  3. O_APPEND   每次写时都加到文件的尾端;

  4. O_TRUNC     属性去打开文件时,如果文件中本来就有内容,而且为只读或只写成功打开,则将其长度改为0

         Mode:一定是在flag中使用了O_CREAT标志,mode记录待创建的文件的访问权限


int creat(const char *filename,mode_t mode)

filiname     要创建的文件名(包含路径,缺省为当前路径)

mode        创建模式(可读 可写 可执行)

常见创建模式

宏表示           数字

S_IRUSR       4                     可读

S_IWUSR      2                     可写

S_IXUSR      1                      可执行

S_IRWXU      7                     可读  可写  可执行


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

int main()
{
        int fd;
        fd = open("./fili1",O_RDWR);
        printf("%d",fd);
        return 0;
}

运行结果3   如果没有该文件返回值时-1

#include <sys/types.h> 
#include <sys/stat.h> 
#include <fcntl.h> 
#include<stdio.h> 
 
int main() 
{ 
        int fd; 
        fd = open("./fili1",O_RDWR); 
        if(fd = -1) 
        { 
                printf("open filied\n"); 
                fd = open("./fili1",O_RDWR|O_CREAT,0600); 
                if(fd > 0) 
                { 
                        printf("sucessful\n"); 
                } 
 
        } 
        printf("%d",fd); 
        return 0; 
}

如果没有fili1      则创建fili1

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

int main()
{
        int fd;
        fd = open("./fili1",O_RDWR);
        printf("%d",fd);
        return 0;
}

 

O_RDWR|O_CREAT|O_EXCL             文件不存在创建文件    文件存在则出错返回-1

#include<sys/types.h>
#include<sys/stat.h>
#include<fcntl.h>
#include <unistd.h>
#include<string.h>
int main()
{
        char *buf = "zhaoyujian";
        int fd = open("fili1",O_RDWR|O_TRUNC);
        write(fd,buf,strlen(buf));
        close(fd);
        return 0;
}

 

创建文件      文件权限可读可写可执行


ls -l           是把所有的清单列出来

-rw-r--r-- 1 CLC book    0 Dec 28 21:52 a.c
-rwxr-xr-x 1 CLC book 8479 Jan  9 22:27 a.out
-rw-r--r-- 1 CLC book  760 Jan  9 21:43 demo1.c
-rw-r--r-- 1 CLC book  711 Jan  9 21:43 demo2.c
-rw-r--r-- 1 CLC book  304 Jan  9 22:28 demo3.c
-rw-r--r-- 1 CLC book    0 Jan  2 00:16 demo3.config
-rw-r--r-- 1 CLC book  165 Jan  1 22:51 demo4.c
-rw-r--r-- 1 CLC book  440 Jan  9 22:25 demo5.c
-rw-r--r-- 1 CLC book  466 Jan  9 22:27 demo9.c
-rw-r--r-- 1 CLC book    0 Jan  1 23:50 tese.config
—                  代表当前文件

r                    代表可读权限

w                   代表可写权限

x                    可执行权限

可读是4      可写是2       可执行是1                                               0600      6表示4+2    后面的第一个0    表示同组    第二个0表示其他组

 


write()函数的用

以下是函数原型

#include <unistd.h>

       ssize_t write(int fd, const void *buf, size_t count);
读取成功返回的是字节数   读取失败返回的是-1


#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include<stdio.h>
#include <unistd.h>
#include<string.h>
main()
{
        int fd;
        char *buf = "zhaoyujian";
        fd = open("./fili1",O_RDWR);
        if(fd = -1)
        {
                printf("open filied\n");
                fd = open("./fili1",O_RDWR|O_CREAT,0600);
                if(fd > 0)
                {
                        printf("sucessful\n");
                }

        }
        printf("%d",fd);
        write(fd,buf,strlen(buf));
        close(fd);
        return 0;
}                                                                                                                                                                                                                                                                                                                                                                                                                                                                                           

运行结果是把buf中的数据写到fili1这个文件中-

#include<sys/types.h>
#include<sys/stat.h>
#include<fcntl.h>
#include <unistd.h>
#include<string.h>
int main()
{
        char *buf = "zhaoyujian";
        int fd = open("fili1",O_RDWR|O_APPEND);
        write(fd,buf,strlen(buf));
        close(fd);
        return 0;
}
~      

 

O_RDWR|O_APPEND           如果文件已经有东西  则在之后继续写入数据         不删除原有的文件

 

#include<sys/types.h>
#include<sys/stat.h>
#include<fcntl.h>
#include <unistd.h>
#include<string.h>
int main()
{
        char *buf = "zhaoyujian";
        int fd = open("fili1",O_RDWR|O_TRUNC);
        write(fd,buf,strlen(buf));
        close(fd);
        return 0;
}

 

O_RDWR|O_TRUNC              如果文件中有数据则把所有的数据删除以后再把数据写进去


read()函数的用法

下面是函数原型

#include <unistd.h>

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

读取成功返回的是字节数   读取失败返回的是-1

#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include<stdio.h>
#include <unistd.h>
#include<string.h>
#include<stdlib.h>
main()
{
        int fd;
        char *buf = "zhaoyujian";
        fd = open("./fili1",O_RDWR);
        if(fd = -1)
        {
                printf("open filied\n");
                fd = open("./fili1",O_RDWR|O_CREAT,0600);
                if(fd > 0)
                {
                        printf("sucessful\n");
                }

        }
        printf("%d",fd);
        int n_write = write(fd,buf,strlen(buf));
        printf("write %d\n ",n_write);
        char *readbuf;
        readbuf = (char *)malloc(sizeof(char)*n_write+1);
        int n_read = read(fd,readbuf,n_write);
        printf("read %d,%s",n_read,readbuf);

        close(fd);
        return 0;
}

这个代码是只有写入没有读取          没有将光标移动到最前面       有两中解决方案    一种只把打开的文件关闭重新打开   一种是用光标函数移动

 close(fd);
 open("fili1",O_RDWR);

关闭以后重新打开


lseek()函数的用法

下面是函数原型

 #include <sys/types.h>
 #include <unistd.h>

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

 

返回值       偏移值的多少

whence     

             SEEK_SET             文件头

             SEEK_END            文件尾

             SEEK_CUR            当前位置


off_t  offset                                   偏移值 对whence的偏移值

#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include<stdio.h>
#include <unistd.h>
#include<string.h>
#include<stdlib.h>
main()
{
	int fd;
	char *buf = "zhaoyujian";
	fd = open("./fili1",O_RDWR);
	if(fd = -1)
	{
		printf("open filied\n");
		fd = open("./fili1",O_RDWR|O_CREAT,0600);
		if(fd > 0)
		{
			printf("sucessful\n");
		}

	}
	printf("%d",fd);
	int n_write = write(fd,buf,strlen(buf));
	printf("write %d\n ",n_write);
//	close(fd);
//	open("fili1",O_RDWR);
	lseek(fd,1,SEEK_SET);
	char *readbuf;
	readbuf = (char *)malloc(sizeof(char)*n_write+1);
	int n_read = read(fd,readbuf,n_write);
	printf("read %d,%s",n_read,readbuf);
	
	close(fd);
	return 0;
}

上面说明了lseek用法    光标设置到最前面    偏移值如果是0  就只最前面  如果是1 就是从第二个开始     正数是光标向后移动     负数是光标向前移动

#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include<stdio.h>
#include <unistd.h>
#include<string.h>
#include<stdlib.h>
main()
{
        int fd;
        fd = open("./fili1",O_RDWR);
        int n_lseek = lseek(fd,0,SEEK_END);
        printf("%d",n_lseek);

        close(fd);
        return 0;
}

lseek用来计算文件的大小


文件描述符:

1,对于内核而言,所有打开文件都有文件描述符引用,文件描述符是一个非负整数,当打开一个现存文件或者创建一个新文件时,内核向进程返回一个文件描述符,当读写一个文件时,用open或write返回的文件描述符标识该文件,将其作为参考传递给read和write

按照惯例,UNIX shell使用文件描述符0与进程的标准输入输出相结合,文件描述符1与标准输出相结合,文件描述符2与标准错误输出相结合,STDIN_FILEO   STDOUT_FILEO    STDERR_FILENO   这几个宏代替了 0  1  2  这几个数字

#include<sys/types.h>
#include<sys/stat.h>
#include<fcntl.h>
#include <unistd.h>
#include<string.h>
#include<stdlib.h>
int main()
{
        int fd;
        char *buf;
        buf = (char *)malloc(sizeof(char) * 5+1);
        read(0,buf,5);
        write(1,buf,5);

        return 0;
}

标准输入输出文件的举例说明

2文件描述符,这个数字在一个进程中表示一个特定的含义,但我们open一个函数时,操作系统在内存中构建一些数据结构来表示这个动态文件,然后返回给应用程序一个数字作为文件描述符,这个数字就和我们内存中维护这个文件的这些数据结构绑定上了,以后我们应用程序如果要操作这个动态文件,只需要用这个文件描述符区分

3,文件描述符的作用域就是当前进程,出了这个进程文件描述符就没有意义了

 

 

打开创建文件                   读取文件写入文件,               关闭文件

1:在Linux中要操作一个文件,一般是先open打开一个文件,得到文件描述符,然后对文件进行读写操作(或其他操作),最后close关闭文件即可

2:强调一点,我们对文件进行操作时,一定要先打开文件,打开成功后才能操作,如果打开失败,就不用进行后面的操作了,最有读写关闭后,一定要关闭文件,否则会造成文件损坏,

3: 文件平时是存放在块设备中文件系统文件中的,我们把这种文件叫静态文件,当我们去open打开一个文件时,linux内核做的操作包括,内核在进程中建立一个打开文件的数据结构,记录下我们打开的这个文件,内核在内存中申请一段内存,并且将静态文件的内容从这块设备中读取到内核中特定地址管理存放(叫动态文件)

4:打开文件以后,以后对这个文件的读写操作,都是针对内存中的这一份动态文件的,而并不是针对静态文件的,但我们对动态文件进行读写以后,此时内存中动态文件和块设备文件中的静态文件就不同步了,但我们close关闭动态文件时,close内部内核将内存中的动态文件的内容去更新,(同步)块设备中的静态文件

5:为什么这么设计,不直接对块设备直接进行操作   块设备本身读写非常不灵活,是 按块读写的,而内存是按字节单位操作的,而且可以随机操作,很灵活

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值