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)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • sklearn.KFold「建议收藏」

    sklearn.KFold「建议收藏」K折交叉验证:将样本切成K份,每次取其中一份做为测试集,剩余的K-1份做为训练集。在sklearn.model_selection中提供了几种K折交叉验证。生成样本&amp;gt;&amp;gt;&amp;gt;fromsklearn.datasetsimportmake_classification&amp;gt;&amp;gt;&amp;gt;data,target=make_classification(n_…

    2026年1月30日
    3
  • 云邦互联免费空间(免备案,无广告)「建议收藏」

    云邦互联免费空间(免备案,无广告)「建议收藏」【1G免费全能空间,免备案,无广告】1G全能空间+100M数据库(Mysql5.5)支持的脚本:ASP、PHP(5.2-7.0)、.NET(2.0/4.0)没有任何限制,详细功能请访问:https://www.yunzz.net/host/free.html(云邦互联)推广员:ftp257684p…

    2022年6月18日
    27
  • [模板] Dijkstra单源最短路径

    [模板] Dijkstra单源最短路径十分基础的Dijkstra算法详见代码#definemaxn100000#definemaxm100000usingnamespacestd;constintINF=999999999;structHeapNode{ intd,u; booloperator<(constHeapNode&rhs)const { returnd>rh

    2022年5月30日
    35
  • [Android 动画]简要分析一下Animator 与 Animation

    [Android 动画]简要分析一下Animator 与 Animation

    2022年2月6日
    63
  • ubuntu开机进入tty1_ubuntu tty模式

    ubuntu开机进入tty1_ubuntu tty模式1、一个作为宿主机的Linux;本文使用的是RedhatEnterpriseLinux5.4;2、在宿主机上提供一块额外的硬盘作为新系统的存储盘,为了降低复杂度,这里添加使用一块IDE接口的新硬盘;3、Linux内核源码,busybox源码;本文使用的是目前最新版的linux-2.6.34.1和busybox-1.16.1。说明:本文是一个stepbystep的…

    2022年8月12日
    8
  • QPainter::drawImage的用法说明

    QPainter::drawImage的用法说明Qt 帮助文档里的函数声明 voidQPainter drawImage constQRect amp target constQImage amp image constQRect amp source Qt ImageConvers Qt AutoColor 举例 QRecttarget 10 0 20 0 80 0

    2025年6月23日
    4

发表回复

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

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