【回眸】技术干货——Linux内核(三)对文件的操作(读取、光标)
Linux内核(三)对文件的操作(读取、光标)
读取完整代码
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h> //开辟空间malloc需要这个头文件
int main(){
//定义要写入的内容
char *buf = "pjy!yyds!by鸭鸭";
int fd;
fd = open("./file1",O_RDWR);
if(fd == -1){
printf("file open error!\n");
//如果file1不存在,创建一个file1
fd = open("./file1",O_RDWR|O_CREAT,0600);
if(fd > 0)
{
printf("create file1 sucessfully!\n");
}
}
//如果已经存在file1,打印一句话并写入*buf的内容
printf ("open file1 successfully!&fd = %d\n",fd);
int n_write = write(fd,buf,strlen(buf));
//打印写入了多少字节
if (n_write != -1){
printf("write%d byte to file\n",n_write);
}
//关闭后再打开,若无以下两行代码,将无法读取文件内容,因为光标停在最后,后面没有内容了
close(fd);
fd = open("./file1",O_RDWR);
//读取文本内容
char *readBuf;
readBuf = (char*)malloc(sizeof(char)*n_write+1);
int n_read = read(fd,readBuf,n_write);
printf("read %d,context%s\n",n_read,readBuf);
//关闭上述文件
close(fd);
return 0;
}
运行效果图

时隔几个月后复习,加了一些内容及思路
思路:
//1.打开(什么方式)
//2.打印文件描述符
//3.如果打开失败(fd=-1)以可读写的方式再来一次
//4.记得加上或操作方便创建新文件,新文件的权限可以写0600
//5.内循环判断文件描述符大于0,打印创建成功
//6.定义n_write
//7.如果fd!=-1,打印(已经写入了多少byte)
//8.暂时关闭文件
//9.读写方式打开
//10.char指针readbuf
//11.赋值readbuf
//12.赋值nread read()api
//13.打印nread和readbuf,nread统计字数,readbuf记录内容
//14.关闭文件
#include<stdio.h>
#include<sys/types.h>
#include<sys/stat.h>
#include<fcntl.h>
#include<unistd.h>
#include<string.h>
#include<stdlib.h>
int main(){
int fd;
char *buf = "pjy yyds!";
open("./file1",O_RDWR);
printf("fd = %d\n",fd);
if (fd = -1)
{
fd = open("./file1",O_RDWR|O_CREAT,0600);
printf("file not exit!\n");
if(fd > 0){
printf("create file successfully!\n");
}
}
//6.定义n_write
int n_write = write(fd,buf,strlen(buf));
//7.如果fd!=-1,打印(已经写入了多少byte)
if(fd!=-1){
printf("write %d byte already\n",n_write);
}
//8.暂时关闭文件
close(fd);
//9.读写方式打开
fd = open("./file1",O_RDWR);
//10.char指针readbuf
char *readBuf;
//11.赋值readbuf
readBuf = (char*)malloc(sizeof(char)*n_write+1);
//12.赋值nread read()api
int n_read = read(fd,readBuf,n_write);
//13.打印nread和readbuf,nread统计字数,readbuf记录内容
printf("write %d byte,and the text is :%s\n",n_read,readBuf);
//14.关闭文件
close(fd);
return 0;
}
以下代码也可实现这个效果
光标移到最开始,偏移值为0
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h> //开辟空间malloc需要这个头文件
int main(){
//定义要写入的内容
char *buf = "pjy!yyds!by鸭鸭";
int fd;
fd = open("./file1",O_RDWR);
if(fd == -1){
printf("file open error!\n");
//如果file1不存在,创建一个file1
fd = open("./file1",O_RDWR|O_CREAT,0600);
if(fd > 0)
{
printf("create file1 sucessfully!\n");
}
}
//如果已经存在file1,打印一句话并写入*buf的内容
printf ("open file1 successfully!&fd = %d\n",fd);
int n_write = write(fd,buf,strlen(buf));
//打印写入了多少字节
if (n_write != -1){
printf("write%d byte to file\n",n_write);
}
//读取文本内容
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;
}
光标停留在当下(最后),偏移值为【-所占字节】
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h> //开辟空间malloc需要这个头文件
int main(){
//定义要写入的内容
char *buf = "pjy!yyds!by鸭鸭";
int fd;
fd = open("./file1",O_RDWR);
if(fd == -1){
printf("file open error!\n");
//如果file1不存在,创建一个file1
fd = open("./file1",O_RDWR|O_CREAT,0600);
if(fd > 0)
{
printf("create file1 sucessfully!\n");
}
}
//如果已经存在file1,打印一句话并写入*buf的内容
printf ("open file1 successfully!&fd = %d\n",fd);
int n_write = write(fd,buf,strlen(buf));
//打印写入了多少字节
if (n_write != -1){
printf("write%d byte to file\n",n_write);
}
//读取文本内容
char *readBuf;
readBuf = (char*)malloc(sizeof(char)*n_write+1);
lseek(fd,-n_write,SEEK_CUR);
int n_read = read(fd,readBuf,n_write);
printf("read %d,context%s\n",n_read,readBuf);
//关闭上述文件
close(fd);
return 0;
}
使用lseek()函数计算已存在的文件大小
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h> //开辟空间malloc需要这个头文件
int main(){
//定义要写入的内容
char *buf = "pjy!yyds!by鸭鸭";
int fd;
fd = open("./file1",O_RDWR);
//计算文件大小
int fileSize = lseek(fd,0,SEEK_END);
printf("this file's size is %d",fileSize);
close(fd);
return 0;
}
使用lseek()函数计算已存在的文件大小的效果图

后记碎碎念
Linux是一个系列,可以点击专栏查看同系列的其他文章,希望能帮到屏幕前的每一位应届生往届生,该博文最初发表在CSDN上。
#央国企投递记录##26届秋招投递记录##找工作有哪些冷知识##薪资爆料##校招求职有谈薪空间吗#应届生必学实用物联网技术 文章被收录于专栏
本专栏助应届生从物联网小白成长为企业争抢的技术人才,聚焦三大核心技术:传感器应用(环境监测)、嵌入式开发(STM32/Arduino)、通信协议(LoRa/NB-IoT/MQTT),配合10+实战项目(如智能温湿度监控系统)积累项目经验。覆盖智能硬件、工业物联网、智能家居领域岗位需求,解析企业招聘技术重点与面试题,帮电子、计算机、自动化等专业学生构建知识体系,提前锁定名企Offer!