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


相关推荐

  • js获取url链接中的域名部分

    js获取url链接中的域名部分

    2021年10月30日
    51
  • [bzoj3884] 上帝与集合的正确用法

    [bzoj3884] 上帝与集合的正确用法

    2022年3月7日
    33
  • InnoDB数据库隔离级别

    InnoDB数据库隔离级别事务隔离级别分为四种(级别递减):1、Serializable(串行化):最严格的级别,事务串行执行,资源消耗最大;2、REPEATABLEREAD(重复读):保证了一个事务不会修改已经由另一个事务读取但未提交(回滚)的数据。避免了“脏读取”和“不可重复读取”的情况,但不能避免“幻读”,但是带来了更多的性能损失。3、READCOMMITTED(提交读):大多数主流数据库的默认事…

    2022年5月18日
    30
  • linux系统644、755、777权限详解「建议收藏」

    linux系统644、755、777权限详解「建议收藏」常用的linux文件权限:444r–r–r–600rw——-644rw-r–r–666rw-rw-rw-700rwx——744rwxr–r–755rwxr-xr-x777rwxrwxrwx从左至右,1-3位数字代表文件所有者的权限,4-6位数字代表同组用户的权限,7-9数字代表其他用户的权限。而具体的权限是由数字

    2022年6月17日
    26
  • Nodecache最适合国内地区的免备案CDN推荐!

    Nodecache最适合国内地区的免备案CDN推荐!自己的一个网站,因为服务器是海外的,虽然速度还不错,但延迟和丢包问题经常困扰着我。尤其是到了晚上,丢包情况就更加严重,所以需要一款免备案、有香港节点的CDN来提升一下访问体验。查了下资料,很多人推荐使用Nodecache,用了一段时间后感觉Nodecache确实还不错,下面给大家介绍一下。Nodecache是GlobalCacheTechnologyCo.,Ltd.旗下品牌,致力于为客户提供一站式的在线业务加速服务。Nodecache主要提供的就是免备案CDN加速、SSL证书、DNS

    2022年9月11日
    0
  • 2021.4.14永久激活码_通用破解码

    2021.4.14永久激活码_通用破解码,https://javaforall.net/100143.html。详细ieda激活码不妨到全栈程序员必看教程网一起来了解一下吧!

    2022年3月16日
    1.4K

发表回复

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

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