linux编程-文件读取操作简单上手

这里写一个最简单的文件读取的操作流程:

我的系统是centos7,默认是不带有linux函数系统手册的,所以有需要的要先安装一下,具体的命令如下:yum -y install man-pages
出现如下提示,说明安装成功;

Running transaction 正在安装 : man-pages-3.53-5.el7.noarch
1/1 验证中 : man-pages-3.53-5.el7.noarch
1/1

已安装: man-pages.noarch 0:3.53-5.el7

完毕!

这里就开始着手写我们的程序了;

  1. 我们这里随便建一个c程序,就叫open_test1.c,首先写一个main函数

int main(void)
{
return 0;
}

  1. 下面我们需要查询手册,来看linux系统是怎么打开一个文件的;这里我们要使用man 2 open 命令查看:
OPEN(2)                  Linux Programmer's Manual                  OPEN(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);

具体的描述如下,所以我们根据他给我们的提示,写一个打开文件的操作,我们的程序中增加如下代码:

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



int main(void)
{
        int fd = 0;
        fd = open("./file.txt",O_RDWR);
        if(-1 == fd)
        {
                printf("open fail\n");
                return 0;
        }
        else
        {
                printf("open ok\n");
        }
        
         // 关闭文件
        close(fd);
}

如果文件打开正常,会打印出,open ok;

  1. 然后再看下往文件中写是怎么操作的,同样的我们去查看手册,命令为:man 2 write;
WRITE(2)                 Linux Programmer's Manual                 WRITE(2)

NAME
       write - write to a file descriptor

SYNOPSIS
       #include <unistd.h>

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

所以我们的程序中增加代码如下:

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

int main(void)
{
        int fd = 0;
        // 打开file.txt文件
        fd = open("./file.txt",O_RDWR);

        if(-1 == fd)
        {
                printf("open fail\n");
                return 0;
        }
        else
        {
                printf("open ok\n");
        }
        
        // 往file.txt文件里面写入hello world
        char buf[] = "hello world";
        write(fd,(void *)buf,11);

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

	    // 关闭文件
        close(fd);
}
  1. 读取file.txt文件里的内容到buf2中,然后输出,同样读文件的函数查看命令用man
    2 read:
READ(2)                  Linux Programmer's Manual                  READ(2)

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 <unistd.h>
#include <stdio.h>



int main(void)
{
        int fd = 0;
        // 打开file.txt文件
        fd = open("./file.txt",O_RDWR);

        if(-1 == fd)
        {
                printf("open fail\n");
                return 0;
        }
        else
        {
                printf("open ok\n");
        }
        // 往file.txt文件里面写入hello world
        char buf[] = "hello world";
        write(fd,(void *)buf,11);

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

        // 把文件指向调到文件头部位置
        lseek(fd, SEEK_SET, 0);
        //off_t lseek(int fd, off_t offset, int whence);

        // 读取file.txt的内容到buf2中,然后输出
        char buf2[30] = {0};
        read(fd, buf2, sizeof(buf2));

        printf("buf2 = %s\n", buf2);

        // 关闭文件
        close(fd);
        
        return 0;
}

到此为止,我们的程序基本就算是写完了!

  1. 那么让我们编译运行一下:

[root@localhost linux]# gcc open_test1.c
[root@localhost linux]#

如果没有报错,则说明我们的程序没有问题:编译后会出现一个a.out文件

[root@localhost linux]# ll
总用量 20
-rwxr-xr-x. 1 root root 8704 5月 28 10:19 a.out
-rw-r–r--. 1 root root 11 5月 28 09:52 file.txt
-rw-r–r--. 1 root root 757 5月 28 10:12 open_test1.c
[root@localhost linux]#

然后让我们运行一下看一下结果:

[root@localhost linux]# ./a.out
open ok
buf2 = hello world
[root@localhost linux]#

成功输出!!!

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值