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


相关推荐

  • Qt多线程:使用互斥锁

    1、官方文档:QMutex类提供线程间的访问序列化。QMutex的目的是保护一个对象、数据结构或代码片段,这样每次只有一个线程可以访问它(这类似于Javasynchronized关键字)。通常最好将互斥对象与QMutexLocker一起使用,因为这样可以很容易地确保一致地执行锁定和解锁。2、官方示例:QMutexmutex;intnumber=6;v…

    2022年4月8日
    443
  • 菲尼克斯FL SWITCH SFN 16TX多端口交换机

    菲尼克斯FL SWITCH SFN 16TX多端口交换机菲尼克斯FLSWITCHSFN16TX多端口交换机2891933可提供标准温度(0°C…60°C)和宽温(-40°C…75°C)型号的设备窄型金属壳体上有16个端口,带冗余输入电压自适应与自交叉检测简化了安装和设置电缆安全锁定备选本地诊断指示,带LEDRJ45端口的传输速率为10/100Mbps,光纤端口的传输速率为100Mbps尺寸宽度70mm高度135mm深度110mm环境条件保护等级IP20环境温度(运行)0°C…60°C环境温度(

    2022年6月22日
    27
  • 记使用 git clean -d -fx ” ” 命令悲催的一天(强列建议慎用)

    记使用 git clean -d -fx ” ” 命令悲催的一天(强列建议慎用)

    2022年2月10日
    93
  • pandas groupby 用法详解

    pandas groupby 用法详解1.分组groupby在日常数据分析过程中,经常有分组的需求。具体来说,就是根据一个或者多个字段,将数据划分为不同的组,然后进行进一步分析,比如求分组的数量,分组内的最大值最小值平均值等。在sql中,就是大名鼎鼎的groupby操作。pandas中,也有对应的groupby操作,下面我们就来看看pandas中的groupby怎么使用。2.groupby的数据结构首先我们看如下代码defddd():levels=[“L1″,”L1″,”L1″,”L2″,”L2″,”L3”,

    2022年5月13日
    64
  • murmurhash算法_MurmurHash与随机数

    murmurhash算法_MurmurHash与随机数unsignedintmurMurHash(constvoid*key,intlen){constunsignedintm=0x5bd1e995;constintr=24;constintseed=97;unsignedinth=seed^len;//Mix4bytesatatimeintothehashconstunsigne…

    2022年10月18日
    4
  • apache 和 nginx 的区别

    apache 和 nginx 的区别1、nginx比apache占用更少的内存及资源2、抗并发—–nginx处理请求是异步非阻塞的,而apache则是阻塞型的,在高并发下nginx能保持低资源低消耗高性能3、apache少bug,nginx的bug相对较多 4、nginx运行效率高,占用资源少,代理功能强大,很适合做前端响应服务器5、Apache在处理动态有优势,Nginx并发性比较好,CPU

    2022年6月5日
    28

发表回复

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

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