深入Linux文件编程核心,编织数据的经纬,构建高效系统的坚实基石!#Linux系统编程中的文件编程(中)

前言

  本篇博文深入浅出地介绍了Linux系统编程中的文件编程(中),从文件操作的基本原理出发,逐步过渡到实际应用。通过模拟实现cp命令、修改程序配置文件、将整数及结构体写入文件等丰富实例,不仅巩固了文件操作的基础知识,还展示了这些技能在编程实践中的广泛应用。对于热爱Linux文件编程的您而言,这无疑是一份不可多得的宝贵资源,建议先点赞收藏,以便日后细细品味,逐步提升您的编程技艺。

预备知识

  一、C变量
  二、基本输入输出
  三、流程控制
  四、函数
  五、指针
  六、字符串
  七、Linux系统基本操作命令如mkdir,ls -l等。

  如果以上知识不清楚,请自行学习后再来浏览。如果我有没例出的,请在评论区写一下。谢谢啦!

一、 文件操作原理简述

1.1 文件描述符补充

在这里插入图片描述
请添加图片描述
请添加图片描述

1.2 动静态文件

在这里插入图片描述
请添加图片描述

1.3 Linux文件管理简述

在这里插入图片描述

二、 文件操作小应用之实现cp指令

2.1 实现CP指令思路

请添加图片描述

2.2 参数测试

2.2.1 参数测试程序代码
#include <stdio.h>
argc为参数的个数,argv为具体参数名
int main(int argc,char **argv)
{
        printf("Parameter number %d\n",argc);          输出参数个数
        printf("Parameter number 1 is %s\n",argv[0]);  输出参数1的名字
        printf("Parameter number 2 is %s\n",argv[1]);  输出参数2的名字
        printf("Parameter number 3 is %s\n",argv[2]);  输出参数3的名字

        return 0;
}
2.2.2参数测试程序运行结果
CLC@Embed_Learn:~/Linux_System_Programming/Linux_file_programming/07_File_CP_order$ gcc text.c 
CLC@Embed_Learn:~/Linux_System_Programming/Linux_file_programming/07_File_CP_order$ ./a.out src des
Parameter number 3
Parameter number 1 is ./a.out
Parameter number 2 is src
Parameter number 3 is des

2.3 实现cp指令程序

2.3.1 实现cp指令程序代码
#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 argc,char **argv)
{
        int fdSrc;
        int fdDes;
        int size;
        char *readBuf = NULL;
        
        if(argc < 3)
        {
                printf("Program error!\n");
                exit(-1);
        }
        
        fdSrc = open(argv[1],O_RDWR);        1打开源文件
        size =lseek(fdSrc,0,SEEK_END);       2计算源文件大小
        lseek(fdSrc,0,SEEK_SET);             3光标归位
        readBuf = (char *)malloc(sizeof(char) * (size+1)); 4为读取数据缓冲区分配空间
        read(fdSrc,readBuf,size +1);         5读取源文件
        printf("readBuf = %s\n",readBuf);    6输出从源文件读取到的内容   

        fdDes = open(argv[2],O_RDWR|O_CREAT,0600); 7打开或创建目标文件,记得给权限0600,不然会写入失败
        write(fdDes,readBuf,strlen(readBuf));8向目标文件写入数据

        close(fdSrc);                        9关闭源文件
        close(fdDes);                       10关闭目标文件

        return 0;
}
2.3.2 实现cp指令程序运行结果
CLC@Embed_Learn:~/Linux_System_Programming/Linux_file_programming/07_File_CP_order$ gcc File_programming1.c -o mycp
CLC@Embed_Learn:~/Linux_System_Programming/Linux_file_programming/07_File_CP_order$ ./mycp text.c text2.c 
readBuf = #include <stdio.h>

int main(int argc,char **argv)
{
	printf("Parameter number %d\n",argc);
	printf("Parameter number 1 is %s\n",argv[0]);
	printf("Parameter number 2 is %s\n",argv[1]);
	printf("Parameter number 3 is %s\n",argv[2]);

	return 0;
}
CLC@Embed_Learn:~/Linux_System_Programming/Linux_file_programming/07_File_CP_order$ ls
a.out  File_programming1.c  mycp  text2.c  text.c
2.3.3 bug优化
将这一句
fdDes = open(argv[2],O_RDWR|O_CREAT,0600); 7打开或创建目标文件,记得给权限0600,不然会写入失败
修改为:
fdDes = open(argv[2],O_RDWR|O_CREAT|O_TRUNC,0600); 7打开或创建目标文件,记得给权限0600,不然会写入失败

  这样,当复制出的文件已包含内容时,其内容将被更新为当前复制的新内容。

三、 文件编程小应用之修改程序的配置文件

3.1 修改配置文件思路

请添加图片描述

3.2 man手册strstr函数详解

STRSTR(3)                Linux Programmer's Manual                STRSTR(3)

NAME
       strstr, strcasestr - locate a substring

SYNOPSIS
       #include <string.h>

       char *strstr(const char *haystack, const char *needle);
	   haystack:被查询的字符串
	   needle:	 需要查找的字符串
       #define _GNU_SOURCE         /* See feature_test_macros(7) */

       #include <string.h>

       char *strcasestr(const char *haystack, const char *needle);

DESCRIPTION
       The  strstr()  function  finds the first occurrence of the substring
       needle in the string haystack.  The terminating  null  bytes  ('\0')
       are not compared.

       The  strcasestr() function is like strstr(), but ignores the case of
       both arguments.

RETURN VALUE
       These functions return a pointer to the beginning of the  substring,
       or NULL if the substring is not found.

  译:
STRSTR(3)  Linux 程序员手册  STRSTR(3)

  名字
strstr, strcasestr - 查找子字符串

  简介
  #包括<string.h>

   char *strstr(const char *haystack, const char *needle);;

   #define _GNU_SOURCE         /* 请参阅 feature_test_macros(7) */

  #包括<string.h>

   char *strcasestr(const char *haystack, const char *needle);;

  描述
  函数 strstr() 用于在字符串 haystack 中查找子字符串 needle 的首次出现。终止空字节(\0)不进行比较。

   strcasestr() 函数与 strstr() 类似,但忽略两个参数的大小写。

  返回值
  这些函数返回指向子字符串开头的指针,如果未找到子字符串,则返回 NULL。

3.3 程序代码

#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 argc,char **argv)
{
        int fdSrc;
        int size;
        char *readBuf = NULL;
        char *p = NULL;

if(argc < 2)
        {
                printf("Program error!\n");
                exit(-1);
        }

        fdSrc = open(argv[1],O_RDWR);                          1打开配置文件
        size =lseek(fdSrc,0,SEEK_END);                         2计算配置文件大小
        lseek(fdSrc,0,SEEK_SET);                               3光标回位
        readBuf = (char *)malloc(sizeof(char) * (size+1));     4给读取数据缓冲器分配配置文件大小的空间
        read(fdSrc,readBuf,size +1);                           5读取配置文件
        printf("readBuf = \n%s\n",readBuf);                    6输出配置文件

        p = strstr(readBuf,"LENG  = ");						   7查找配置文件中要修改的内容
        if(p == NULL)                                          8判断是否查找成功
        {
                printf("Search failure!");					
                exit(-1);									   9查找失败退出程序
        }
        p = p + strlen("LENG  = ");                           10查找成功后返回值的字符首地址偏移查找字符的长度个单位
       *p = '5';                                              11修改偏移后的字符为字符5
        lseek(fdSrc,0,SEEK_SET);							  12重新回归光标位置
        write(fdSrc,readBuf,strlen(readBuf));				  13向配置文件中写入修改好的内容

        lseek(fdSrc,0,SEEK_SET);							  14再次回归光标位置
        read(fdSrc,readBuf,size +1);						  15重新读取配置文件
        printf("readBuf = \n%s\n",readBuf);					  16输出配置文件
  

        close(fdSrc);										  17关闭配置文件

        return 0;
}

3.4 程序运行结果

CLC@Embed_Learn:~/Linux_System_Programming/Linux_file_programming/08_Modiy_configuration_file$ gcc File_programming.c 
CLC@Embed_Learn:~/Linux_System_Programming/Linux_file_programming/08_Modiy_configuration_file$ ./a.out TEST.config 
readBuf = 
SPEED = 3
LENG  = 3
SCORE = 9
LEVEL = 9

readBuf = 
SPEED = 3
LENG  = 5
SCORE = 9
LEVEL = 9

四、 写一个整数到文件

4.1 写一个整数到文件的思路

请添加图片描述

4.2 写一个整数到文件程序代码

#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 argc,char **argv)
{
		1定义两个整型变量
        int data = 100;
        int data2 = 0;
        
        int fd;

        fd = open("./file",O_RDWR);				2打开文件file
        write(fd,&data,sizeof(int));			3写文件file:data-->file
        lseek(fd,0,SEEK_SET);					4移动光标到头
        read(fd,&data2,sizeof(int));			5读文件file:file-->data2
        printf("read %d\n",data2);				6输出data2
        close(fd);								7关闭文件file

        return 0;
}

4.3 写一个整数到文件程序运行结果

CLC@Embed_Learn:~/Linux_System_Programming/Linux_file_programming/09_Write_integer_to_file$ gcc File_programming.c 
CLC@Embed_Learn:~/Linux_System_Programming/Linux_file_programming/09_Write_integer_to_file$ ./a.out 
read 100

五、 写一个结构体到文件

5.1 写一个结构体到文件的思路

请添加图片描述

5.2 写一个结构体到文件程序代码

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

struct Text
{
        int a;
        char c;
};

int main(int argc,char **argv)
{
        int fd;
        struct Text data = {100,'c'};
        struct Text data2 = {00,'\0'};
        
        fd = open("./file",O_RDWR|O_CREAT,0600);
        write(fd,&data,sizeof(struct Text));
        lseek(fd,0,SEEK_SET);
        read(fd,&data2,sizeof(struct Text));
        printf("read %d %c\n",data2.a,data2.c);
        close(fd);

        return 0;
}

5.3 写一个结构体到文件程序运行结果

CLC@Embed_Learn:~/Linux_System_Programming/Linux_file_programming/10_Write_structure_to_file$ gcc File_programming.c CLC@Embed_Learn:~/Linux_System_Programming/Linux_file_programming/10_Write_structure_to_file$ ./a.out 
read 100 c

5.4 写一个结构体数组到文件程序代码

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

struct Text
{
        int a;
        char c;
};

int main(int argc,char **argv)
{
        int fd;
        struct Text data[2] = {{100,'c'},{200,'z'}};
        struct Text data2[2];

        fd = open("./file",O_RDWR|O_CREAT,0600);
        write(fd,&data,sizeof(struct Text) * 2);
        lseek(fd,0,SEEK_SET);
        read(fd,&data2,sizeof(struct Text) * 2);
        printf("read %d %c\n",data2[0].a,data2[0].c);
        printf("read %d %c\n",data2[1].a,data2[1].c);

        close(fd);

        return 0;
}

5.5 写一个结构体数组到文件程序运行结果

CLC@Embed_Learn:~/Linux_System_Programming/Linux_file_programming/10_Write_structure_to_file$ gcc File_programming2.c 
CLC@Embed_Learn:~/Linux_System_Programming/Linux_file_programming/10_Write_structure_to_file$ ./a.out 
read 100 c
read 200 z

结束语

  很高兴您能看到这里,点个赞再走呗。谢谢您啦!!!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值