Linux系统编程——文件编程

1.文件编程概述

文件系统原理及访问机制
文件在内核中的管理机制
什么事文件信息节点inode
文件的共享
文件权限,其他用户对其权限
(应用为王)
Linux一切皆文件
文件系统(文件夹/文件)硬件设备 管道 数据库 Socket等

操作系统提供了一系列的API

如Linux系统:

  1. 打开 open
  2. 读写 write/read
  3. 光标定位 lseek
  4. 关闭 close

2.文件打开及创建

原型

NAME
       open, creat - open and possibly create a file or device

SYNOPSIS
       #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:

  1. O_RDONLY 只读打开
  2. O_WRONLY 只写打开
  3. O_RDWR 可读可写可打开

代码1:

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


int main()
{
   
    int fd;
    fd = open("./file1", 0600);
    if(fd == -1)
    {
   
        printf("open file1 failed\n");

    }


    return 0;
}

结果:

open file1 failed

代码2:

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


int main()
{
   
    int fd;
    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 file1 success!");
        }
    }


    return 0;
}

结果:

open file1 failed
create file1 success!

并且多了一个file1的文件

a.out  demo1.c  demo2.c  file1  lianbiao.c

  1. 可读 r 4
  2. 可写 w 2
  3. 执行 x 1
    0600
    当前用户 用户组 其他用户
    4+2
    可读可写
    通过
ls -l

可以查看文件的权限“-”表示普通文件

-rwxrwxr-x 1 book book 8427 Sep 28 17:06 a.out
-rw-rw-r-- 1 book book  226 Sep 28 16:39 demo1.c
-rw-rw-r-- 1 book book  369 Sep 28 17:06 demo2.c
-rw------- 1 book book    0 Sep 28 17:06 file1
-rw-rw-r-- 1 book book 1177 Sep 28 12:27 lianbiao.c

3.文件写入操作编程

原型

NAME
       write - write to a file descriptor

SYNOPSIS
       #include <unistd.h>

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

记得关闭文件

NAME
       close - close a file descriptor

SYNOPSIS
       #include <unistd.h>

       int close(int fd);

代码:

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

int main() {
   
    int fd;
    char *buf = "hello world!";
    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 file1 success!\n");
        }
    }
    write(fd, buf, strlen(buf));//此处不能用sizeof因为指针是根据操作系统占字节大小
    printf("open filed success!\n");
    close(fd);
    return 0;
}
宏表示 数字
S_IRUSR 4 可读
S_IWUSR 2 可写
S_IXUSR 1 可执行
S_IRWXU 7 可读、写、执行

4.文件读取操作

原型

NAME
       read - read from a file descriptor

SYNOPSIS
       #include <unistd.h>

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

代码:

#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <stdlib.h>
int main() {
   
    int fd;
    char *buf = "hello world!";
    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 file1 success!\n");
        }
    }
    int n_write = write(fd, buf, strlen(buf));
    if(n_write != -1)
    {
   
        printf("write %d bytes to buf, context:%s\n", n_write, buf);
    }
    close(fd);//注意关闭文件,不然将读不到信息,光标问题
    open("./file1", O_RDWR);
    char *readBuf  = (char *)malloc(sizeof(char)*n_write + 1);

    int n_read = read(fd, readBuf, n_write);
    printf("read from readBuf %d bytes, context:%s\n", n_read, readBuf);
    printf("open filed success!\n");
    close(fd);
    return 0;
}

结果:

open file1 failed
create file1 success!
write 12 bytes to buf, context:hello world!
read from readBuf 
  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值