Linux文件之strstr函数、将一个整数,结构体和结构体数组写进文件里

Linux文件之strstr函数、将一个整数,结构体和结构体数组写进文件里linux

大家好,又见面了,我是你们的朋友全栈君。如果您正在找激活码,请点击查看最新教程,关注关注公众号 “全栈程序员社区” 获取激活教程,可能之前旧版本教程已经失效.最新Idea2022.1教程亲测有效,一键激活。

Jetbrains全系列IDE使用 1年只要46元 售后保障 童叟无欺

1.首先我们前面介绍了那么多关于文件的api,今天来记录一下strstr函数。
函数原型:

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

返回值:返回一个char型的指针,(返回一个指针指向目的字符串开头位置的指针),如果没有找到的话,则返回NULL

作用:用于判断字符串needle是否是haystack的子串;如果是,则该函数返回needlehaystack中首次出现的地址;否则返回NULL

haystack:将要被查找的目标字符串。
needle:将要被查找的对象字符串。

上代码:

 pstr = strstr(readBuf,"YTRE=");
        if(pstr == NULL)
        { 
   
                printf("faild to found\n");
                exit(-1);
        }

        pstr = pstr + strlen("YTRE=");
        *pstr = '7';

修改YTRE=后面的数字:在readBuf缓冲区中读取“YTRE=”的字符串的首位,并返回给指针pstr,指针接收到后进行指针的偏移“YTRE=”那么长的长度,再将偏移后的指针的位置的内容修改即可,最后写回原来的文件中。

直接上代码:

#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 argc, char **argv)
{ 
   
        if(argc != 2)
        { 
   
                printf("parameter error!!\n");
                exit(-1);
        }

        int fd;
        int fd_size;
        char *readBuf = NULL;
        char *pstr = NULL;


        fd = open(argv[1],O_RDWR);
        fd_size = lseek(fd,0,SEEK_END);
        lseek(fd,0,SEEK_SET);

        readBuf = (char *)malloc(sizeof(char)*fd_size+3);

        read(fd,readBuf,fd_size);
        
        pstr = strstr(readBuf,"YTRE=");
        if(pstr == NULL)
        { 
   
                printf("faild to found\n");
                exit(-1);
        }

        pstr = pstr + strlen("YTRE=");
        *pstr = '7';

        lseek(fd,0,SEEK_SET);
        write(fd,readBuf,fd_size);

        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>

char* change(char *read,char *str)
{ 
   
        char *pstr = NULL;
        pstr = strstr(read,str);

        if(pstr == NULL)
        { 
   
                printf("Sorry ,faild to found\n");
                exit(-1);
        }

        pstr = pstr + strlen(str);
        *pstr = '3';

        return pstr;
}

int main(int argc, char **argv)
{ 
   
        if(argc != 2)
        { 
   
                printf("parameter error!!\n");
                exit(-1);
        }

        int fd;
        int fd_size;
        char *readBuf = NULL;
        char *pstr = NULL;


        fd = open(argv[1],O_RDWR);
        fd_size = lseek(fd,0,SEEK_END);
        lseek(fd,0,SEEK_SET);

        readBuf = (char *)malloc(sizeof(char)*fd_size+3);

        read(fd,readBuf,fd_size);

        pstr = change(readBuf,"YTRE=");

        lseek(fd,0,SEEK_SET);
        write(fd,readBuf,fd_size);

        close(fd);
        return 0;
}

2.分别将一个整数,结构体和结构数组写进文件里。

(1)将一个整数写进文件里,直接上代码:

#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;
        int data1 = 10;
        int data2;

        fd = open("./file1",O_RDWR);

        write(fd,&data1,sizeof(int));

        lseek(fd,0,SEEK_SET);
        read(fd,&data2,sizeof(int));
        printf("read:%d\n",data2);

        close(fd);
        return 0;
}

(2)将一个结构体写进文件里

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

struct Test
{ 
   
        char a;
        int ab;
};

int main()
{ 
   
        int fd;
        struct Test data1 = { 
   'd',15};
        struct Test data2;

        fd = open("./file1",O_RDWR);

        write(fd,&data1,sizeof(struct Test));

        lseek(fd,0,SEEK_SET);
        read(fd,&data2,sizeof(struct Test));
        printf("read:%c, %d\n",data2.a,data2.ab);

        close(fd);
        return 0;
}

(3)将一个结构体数组写进文件里
直接上代码:

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

struct Test
{ 
   
        char a;
        int ab;
};

int main()
{ 
   
        int fd;
        struct Test data1[2] = { 
   { 
   'd',15},{ 
   'a',13}};
        struct Test data2[2];

        fd = open("./file1",O_RDWR);

        write(fd,data1,sizeof(struct Test)*2);

        lseek(fd,0,SEEK_SET);
        read(fd,data2,sizeof(struct Test)*2);

        printf("read: %c, %d\n",data2[0].a,data2[0].ab);
        printf("read: %c, %d\n",data2[1].a,data2[1].ab);
       
        close(fd);
        return 0;
}

学习笔记,仅供参考。

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/184391.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • Werkzeug_vuze怎么用

    Werkzeug_vuze怎么用原文链接:http://werkzeug.pocoo.org/docs/tutorial/欢迎来到Werkzeug教程,这里我们将会创建一个仿制TinyURL的应用,将URLs存储到一个redis实例。为了这个应用,我们将会使用的库包括,用于模板的Jinja2、用于数据库层的redis和用于WSGI层的Werkzeug。你可以使用pip安装需要的库:[plai

    2022年10月7日
    2
  • Mybatis源码研究1:从JDBC到Mybatis

    Mybatis源码研究1:从JDBC到Mybatis

    2021年9月5日
    50
  • Python数组切片_python print数组

    Python数组切片_python print数组文章目录numpy数组切片操作一维数组1、一个参数:a[i]2、两个参数:b=a[i:j]3、三个参数:格式b=a[i:j:s]二维数组numpy数组切片操作通过冒号分隔切片参数start:stop:step来进行切片操作:一维数组importnumpyasnpa=[1,2,3.4,5]print(a)[12345]1、一个参数:a[i]如[2],将…

    2022年9月15日
    3
  • python的lambda表达式详细讲解

    python的lambda表达式详细讲解前言:Lambda表达式是一个匿名函数,Lambda表达式基于数学中的λ演算得名,直接对应于其中的lambda抽象,是一个匿名函数,即没有函数名的函数。Lambda表达式可以表示闭包。缺点:只有一个运算式,不适用于复杂的计算。不够直观,难于理解,增加了维护成本python的lambda可以和java中的stream流一样,可以将lambda函数作为参数传递给其他函数比如说结合map、filter、sorted、reduce等一些Python内置函数使用接下来我们用代码来演示一下lambda(1)简单用法

    2022年10月17日
    3
  • Java学习之AWT GUI编程

    Java学习之AWT GUI编程

    2020年11月20日
    170
  • HBase实战:记一次Safepoint导致长时间STW的踩坑之旅

    HBase实战:记一次Safepoint导致长时间STW的踩坑之旅本文记录了HBase中Safepoint导致长时间STW此问题的解决思路及办法。上篇文章回顾:HBaseReplication详解过程记录现象:小米有一个比较大的公共离线HBase集群,用户很多,每天有大量的MapReduce或Spark离线分析任务在进行访问,同时有很多其他在线集群Replication过来的数据写入,集群因为读写压力较大,且离线分析任务对延迟不…

    2025年5月25日
    3

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号