LINUX系统编程_文件编程_文件读取操作(read函数)

本文详细介绍了Linux系统中的read和write函数,包括它们的定义、参数说明以及使用示例。在示例代码中,演示了如何使用这两个函数进行文件的读写操作,并通过lseek函数移动文件指针。测试结果显示,文件内容成功写入并读取。
摘要由CSDN通过智能技术生成

文件写入操作(read函数)

1.read函数作用

向以打开的文件读取数据。

2.write函数的定义与参数

write函数在Linux下的定义:

#include <unistd.h>
ssize_t read(int fd, void *buf, size_t count);

返回值

成功 :则返回读到的字节数。
失败 :-1
若已达到文件尾,则返回0,因此读到的字节数可能小于count的值。

参数说明

fd:文件描述符
buf表示读出数据缓冲区地址,即指针,指向一段内存单元
count:*表示读出的字节数

补充:Linux系统查看C库函数
命令行输入

man 2 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 = "tianya";

 fd= open("./file2",O_RDWR);
if(fd==-1){
    printf("open files failed\n");
   fd=open("./file2",O_RDWR|O_CREAT,0600);
   if(fd>0)
   {
      printf("create file2 successful!\n");

   }
}

      printf("open successful: 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 file2\n",n_write);

   }
    close(fd);//先关闭文件

     fd= open("./file2",O_RDWR);//再打开文件,移动光标到文件头
     char *readBuf;
     //void *malloc(size_t size);malloc返回值是void *,readBuf是char *.需要强制转换
     readBuf =(char *)malloc(sizeof(char)*n_write+1);
     //ssize_t read(int fd, void *buf, size_t count);
     int n_read = read(fd,readBuf,n_write);
     printf("read %d,context:%s\n",n_read,readBuf);
    close(fd);
    return 0;
}

测试结果
内容已成功写入文件

tianya@tianya-virtual-machine:~$ gcc demo4.c
tianya@tianya-virtual-machine:~$ ./a.out 
open successful: fd = 3
write6 byte to file2
read 6,context:tianya

在这个程序中,在调用read函数之前,先调用了close函数和open函数,这是为了让光标移到文件的头,否则将读取失败。因此,就还需要用到lseek函数来移动文件中光标的位置。
通过调用lseek函数可以改变光标的位置,其函数原型为

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

off_t lseek(int fd, off_t offset, int whence);
其中,fd为文件描述符;offset指的是每一次读写操作所需移动距离,以字节为单位 ,可正可负,正值表示想文件尾部移动,负值表示向文件头部移动。whence表示当前位置的基点,主要有以下三个基点符号常量。

SEEK_SEK:将光标移到距离文件头出offset个字节;

SEEK_CUR:将光标移到当前位置前后offset个字节;

SEEK_END:将光标移到文件末尾前后offset个字节。

测试代码

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

 fd= open("./file2",O_RDWR);
if(fd==-1){
    printf("open files failed\n");
   fd=open("./file2",O_RDWR|O_CREAT,0600);
   if(fd>0)
   {
      printf("create file2 successful!\n");
   
   }
}

     printf("open successful: 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 file2\n",n_write);
   
   }
//    close(fd);
    
  //   fd= open("./file2",O_RDWR);
    
//off_t lseek(int fd, off_t offset, int whence);
    
     char *readBuf;
     readBuf =(char *)malloc(sizeof(char)*n_write+1);
    
      lseek(fd,0,SEEK_SET);

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

将光标移动到复制的起始位置,按一下大写V或小写v,(大写V是整行,小写是光标处),然后上下左右将光标移动到复制的末尾,然后按下y,移动到要粘贴的位置,按下大写P或小写p(大写P:光标之前粘贴,小写p光标之后粘贴)
测试结果

tianya@tianya-virtual-machine:~$ gcc demo5.c
tianya@tianya-virtual-machine:~$ ./a.out 
open successful: fd = 3
write6 byte to file2
read 6,context:tianya

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值