linux 文件编程—open/read/write

此文章为个人学习笔记,其中的内容可能有些地方解释或者表达不是很清晰,所以本笔记 可参考 可借鉴 可指点!

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

其中

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

Flags:     O_RDONLY 只读打开        O_WRONLY 只写打开        O_RDWR 可读可写打开  

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

以上这三个常数中应必须只指定一 个。

eg:open("./file1",O_RDWR|O_CREAT,0600);代表打开file1文件,若file1文件不存在则创建他,且是以可读可写(0600)的权限创建

下列常数是可选择的:            

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

O_EXCL 如果同时指定了OCREAT,而文件已经存在,则出错返回 -1。用于判断是否存在该文件  

O_APPEND 每次写时都加到文件的尾端,即原有的数据不会被删除,而是在文件内原有的数据的尾部插入新写入的数据。      

 O_TRUNC 属性去打开文件时,如果这个文件中本来是有内容的,而且为只读或只写成功打开,则将其长度截短为0;如果是新写入内容,则将原有的内容全部覆盖。

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

创建文件

以下为 打开(open) | 创建(creat) 文件 代码

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

int main()
{
        int fd;
        fd=open("./file1",O_RDWR);//打开file1文件
                   //如果打开失败,则返回文件描述符为-1
        if(fd==-1)
        {
                printf("open file1 failed !\n");
                fd=open("./file1",O_RDWR|O_CREAT,0600);
                //重新调用open,且加标志O_CREAT和权限0600,重新创建file1文件,失败返回-1,成功返回非负数整数
                if(fd>=0)
                {
                        printf("creat file1 \n",fd);
                        printf("fd = %d \n",fd);
                }
                return 0;
        }
}
~  

write

#include <unistd.h>

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

ssize_t 返回的是一个整数,即写入的字符个数,
int fd 为被写进去的文件(文件描述符)
const void *buf 为需要写入的内容的缓冲区
size_t count 为写入内容的大小,多半用strlen来计算

上代码

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

int main()
{
        int fd;
        char *buf="hello word !";
        fd=open("./file1",O_RDWR);
        if(fd==-1)
        {
                printf("open file1 failed !\n");
                fd=open("./file1",O_RDWR|O_CREAT,0600);
                if(fd>=0)
                {
                        printf("creat file1 \n",fd);
                        printf("fd = %d \n",fd);
                }
        }
        printf("creat file1 susceess: fd = %d \n",fd);//返回的文件描述符最低都是从3开始的
        int n_write;
        n_write=write(fd,buf,strlen(buf));
        printf("n_write = %d !",n_write);
        close(fd);//打开了文件,一定要关掉,不让容易造成一些段错误或文件的损坏
        return 0;
}
~      

注意:当向某个文件里面写入了数据,写完之后光标会定位在数据的尾端,下次打开光标依旧在尾端(上次打开没有关闭close或者lseek),确保每次写完数据之后,光标能够定位在文件内数据的最开头,则要么在对该文件操作完之后关闭,要么就用lseek函数将光标重新定位。


关闭文件close

 #include <unistd.h>

 int close(int fd);


read

#include <unistd.h>

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

ssize_t 返回的是一个整数,即读出的字符个数,
int fd 为被读的文件(文件描述符),如果没有该文件,则会阻塞
void *buf 为读出来的内容放置的缓冲区
size_t count 为读出内容的大小,用strlen来计算,strlen(buf)

上代码 

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

int main()
{
        int fd;
        char *buf="hello word !";
        fd=open("./file1",O_RDWR);
        if(fd==-1)
        {
                printf("open file1 failed !\n");
                fd=open("./file1",O_RDWR|O_CREAT,0600);
                if(fd>=0)
                {
                        printf("creat file1 \n",fd);
                        printf("fd = %d \n",fd);
                }
        }
        printf("open file1 susceess: fd = %d \n",fd);

        //ssize_t write(int fd, const void *buf, size_t count);
        int n_write=write(fd,buf,strlen(buf));
        if(n_write!=-1)
        {
                printf("wrie %d byte to file1\n",n_write);
        }

        close(fd);
        fd=open("./file1",O_RDWR);

        //ssize_t read(int fd, void *buf, size_t count);
        char *readbuf=NULL;
        readbuf=(char *)malloc(sizeof(char)*n_write);
        int n_read=read(fd,readbuf,n_write);
        printf("read %d byte from file1: %s \n",n_read,readbuf);

        close(fd);

        return 0;
}

 运行结果


lseek

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

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


off_t 返回一个整数,该数也可用于计算该文件的数据大小
int fd 需要操作的文件(文件描述符)
off_t offset 光标偏移值
int whence 如下

SEEK_SET
The offset is set to offset bytes.
偏移量设置为偏移字节。

SEEK_CUR
The offset is set to its current location plus offset bytes.
偏移量设置为其当前位置加上偏移量字节。

SEEK_END
The offset is set to the size of the file plus offset bytes.
偏移量设置为文件大小加上偏移字节。
lseek(fd,0,SEEK_SET);
将光标放在该文件内数据的头

lseek(fd,0.SEEK_END);
将光标放在该文件内数据的尾部

lseek(fd,+n/-n,SEEK_CUR);
将光标向 左/右 偏移n个字节位置

获取文件指针当前位置:int len = lseek(fd, 0, SEEK_CUR);
获取文件长度:int len = lseek(fd, 0, SEEK_END);


文件描述符

ls -l

文件权限

可读       r    4

可写      w    2

可执行   x    1

  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值