Linux 文件编程

1.文件操作流程

打开/创建文件 --> 编辑文档 --> 保存文档 --> 关闭文档

2.函数使用

2.1 文件打开/创建 open

/*函数原型: int open(const char *pathname, int flags);
int open(const char *pathname, int flags, mode_t mode);*/
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>

int main()
{
        int fd;
		//未使用O_CREAT 可省略mode参数
        fd = open("./file1",O_RDWR);
        if(fd == -1){
                printf("open file1 failed\n");
                //这里的|是或运算符;
                //0600是mode_t mode这个参数,它是所创建文件的权限;
                fd = open("./file1",O_RDWR|O_CREAT,0600);
                if(fd > 0){
                        printf("create success\n");
                }
        }
        return 0;
}

2.2 文件写入 write

#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
#include <unistd.h>
#include <string.h>
int main()
{
        int fd;
        char *buf = "chenLiChen hen shuai!\n";
        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("create success\n");
                }
        }
        printf("open  success : fd = %d\n",fd);
        //ssize_t write(int fd, const void *buf, size_t count);
        ssize_t  temp=write(fd,buf,strlen(buf));//fd是对相对应的文件写;buf是要写入文件的内容;注意,这里不能用sizeof算大小,因为它算的是指针的长度,是8个字节(Linux系统分配指针大小是8字节)所以要用字符串的strlen函数算出字符串的大小。
		//write返回实际写入的数据长度  temp就是实际写入的长度
        close(fd);//关闭文件函数,想关闭哪个就写哪个文件的描述符
        return 0;
}

2.3 文件读取 read

注意:同一文件写入数据后不能立即读数据,涉及到文件光标,需要写完数据后,将光标置于文件头,再去读取数据;或者写完数据后把文件关闭,重新打开文件再去读取数据

#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 = "chenLiChen hen shuai!\n";

        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("create success\n");
                }
        }
        printf("open  success : fd = %d\n",fd);
        //ssize_t write(int fd, const void *buf, size_t count);
        int n_write = write(fd,buf,strlen(buf));//write的返回值成功是写进了多少个字节,不成功是返回-1.
        if(n_write != -1){
                printf("write %d byte to file\n",n_write);
        }
        char *readBuf;
        readBuf = (char *)malloc(sizeof(char)*n_write+1);//malloc老用法了,记得带头文件。
        lseek(fd,0,SEEK_SET);//让光标到文件的头位置,偏移0;这个函数就可以使得read函数读取到文件的内容。
        //ssize_t read(int fd, void *buf, size_t count);
        int n_read = read(fd,readBuf,n_write);//read函数是对哪一个文件读取就写哪一个对应的文件描述符,readBuf是文件读取存放的内容,N_write是读取多少个字节。另外,read函数的返回值成功是读取多少个字节,失败即返回-1.
        printf("read %d,context:%s\n",n_read,readBuf);
        close(fd);
        return 0;
}

2.4 文件偏移 lseek

使用lseek获取文件的大小

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

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

        //ssize_t read(int fd, void *buf, size_t count);
        int fileSize = lseek(fd,0,SEEK_END);//巧妙的利用光标移动的返回值算出文件的大小。

        printf("file's size is:%d\n",fileSize);
        close(fd);

        return 0;

}

2.5.文件打开创建的补充

2.5.1 O_EXCL的使用

O_EXCL与O_CREAT常一起使用;当O_EXCL和O_CREAT一起使用时,有文件存在,fd返回值是-1,没有文件时候fd是非负整数。

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

int main()
{
        int fd;
        
        fd = open("./file1",O_RDWR|O_CREAT|O_EXCL,0600);//当O_EXCL和O_CREAT一起使用时,有文件存在,fd返回值是-1,没有文件时候fd是非负整数。
        if(fd == -1){
                printf("file already exist\n");
        }
        
        return 0;

}
                                                                                    

2.5.2 O_APPEND的使用

以O_APPEND的方式打开文件,写入的数据会追加到文件尾部

#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 = "chenLiChen hen shuai!\n";

        fd = open("./file1",O_RDWR|O_APPEND);//每次写(write函数操作)时,都加到文件的尾端

        printf("open  success : 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("write %d byte to file\n",n_write);
        }
        close(fd);

        return 0;

}

2.5.2 O_TRUNC的使用

使用O_TRUNC打开文件,每次打开文件都会把文件之前的内容全部干掉,使文件长度变成0。

#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 = "test";

        fd = open("./file1",O_RDWR|O_TRUNC);//每次打开文件都会把文件之前的内容全部干掉,使文件长度变成0。

        printf("open  success : 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("write %d byte to file\n",n_write);
        }

        close(fd);

        return 0;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值