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


相关推荐

  • Changing Leisure Activities_register to do

    Changing Leisure Activities_register to dojsp: &lt;inputname="test"id="test"value=""class="validate[required,funcCall[myTest]]"&gt;   js:  不能使用ajax异步数据,可以参考:http://yuhaijunll.iteye.com/admin/blogs/1765620 functionmyTest…

    2022年10月4日
    2
  • linux常用命令50个_docmd常用命令详解

    linux常用命令50个_docmd常用命令详解1. find基本语法参数如下:find[PATH][option][action]#与时间有关的参数:-mtimen:n为数字,意思为在n天之前的“一天内”被更改过的

    2022年8月2日
    7
  • connectionStrings字符串连接

    connectionStrings字符串连接以前在学校学习的时候,自己曾经做过一个项目再连接数据中。碰到了很多关于connectionStrings字符串连接问题。在那时自己的印象中,mdf数据库必须附加到sqlserver2005或2008等工具上才可以使用。今天才知道原来只要有数据库文件就行,没有必要附加上去。下面是连接字符串语句:<connectionStrings><addname=”…

    2022年5月21日
    31
  • Origin Pro2022教育版官方申请、安装及汉化、续期

    Origin Pro2022教育版官方申请、安装及汉化、续期OriginLab官网简介绝大多数学生都使用过Origin来绘图,并将这些图作为自己设计报告或者论文中的重要组成部分,这一点也是源于Origin在数据显示专业性。教育版有一些限制,比如序列号仅6个月有效期,窗口限制12个(窗口限制表示Origin项目中可访问的窗口数,包括所有类型的窗口-WorkBook,Matrix,Graph,Notes,Layout等总共限制为12个)。但是6个月后可重复文章步骤重新申请新的序列号与激活码(可以使用相同邮箱),且12个窗口可以在多数情况下能够满足需要。

    2022年5月18日
    154
  • python 平均值/MAX/MIN值 计算从入门到精通「建议收藏」

    python 平均值/MAX/MIN值 计算从入门到精通「建议收藏」##入门级计算####1、算数平均值#样本:S=[s1,s2,s3,…,sn]#算术平均值:m=(s1+s2+s3+…+sn)/nNumpy中的写法m=

    2022年7月6日
    23
  • evaluateJavascript_jquery form

    evaluateJavascript_jquery formhttp://www.position-relative.net/creation/formValidator/demos/demoValidators.html      $(function(){      $(“#enrolment_Form”).validationEngine(“attach”);      va

    2022年10月3日
    1

发表回复

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

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