Linux修改特定字符或字符串
一 应用场景
在之前我们使用write
函数 都是改变一整个文件的内容。无法对特定内容进行修改。
例如在游戏中,人物的血量并不是一个特定的值。人物受到攻击时,血量会减少。人物吃到血包或者受到救助时,血量会升高。那么在程序运行过程中就要不断修改人物血量值。
二 对单个字符修改
我们创建一个名字为test的文件
内容如下
speed=5//速度
attack=200//攻击值
blood=250//血量
weapom=25//武器序号
我们所有的内容都是以字符的格式来保存的,数字也是字符的一种。
首先我们先更改speed
值,它的值与其他三个值不同。speed
值是单个字符。
修改speed
就需要在文件中检索,步骤如下
1.打开test文件
2.读取test文件内容
3.检索要修改的变量
4.修改
5.写入test文件
strstr函数
在第三步我们需要一个函数来帮我们完成
char *strstr(const char *haystack, const char *needle);
函数解读:
char * 这个函数返回类型为char型指针变量
const char *haystack 第一个参数为寻找的范围
const char *needle 第二个参数为我们需要寻找的字符
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>
//int open(const char *pathname, int flags);
//int open(const char *pathname, int flags, mode_t mode);
int main()
{
char *buf;
char *p;
buf = "speed=5 attack=200 blood=250 weapom=25";
p=strstr(buf,"blood=");
printf("%s",p);
}
运行结果为 blood=250 weapom=25
通过程序示例可以看出 strstr()函数返回的字符指针是我们所寻找字符串的首字母的指针
修改速度 值为8
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;//文件描述符
char *buf = NULL;//将test文件读到该缓冲区
char *p;//用来承接strstr返回值
fd = open("./test",O_RDWR);//打开test文件
if(fd==-1){
printf("error\n");
}else{
printf("success\n");
}
int size=lseek(fd,0,SEEK_END);//用光标来计算文件大小(可以看之前文章)
buf = (char*)malloc(sizeof(char)*size+8);//开辟文件缓冲区空间
lseek(fd,0,SEEK_SET);//将光标置于文件首,否则无法读取文件
int n_read=read(fd,buf,size+8);//读取文件
p = strstr(buf,"speed=");//搜索speed
p = p+strlen("speed=");//将指针移到(speed=5)5处
*p = '8';//赋值为8
lseek(fd,0,SEEK_SET);//将光标移动到开头,覆盖之前内容
int n_write=write(fd,buf,size);
close(fd);
printf("%s",buf);
}
三 对字符串进行修改
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>
//int open(const char *pathname, int flags);
//int open(const char *pathname, int flags, mode_t mode);
int main()
{
int fd;
char *buf = NULL;
char *p;
fd = open("./test",O_RDWR);
if(fd==-1){
printf("error\n");
}else{
printf("success\n");
}
int size=lseek(fd,0,SEEK_END);
buf = (char*)malloc(sizeof(char)*size+8);
lseek(fd,0,SEEK_SET);
int n_read=read(fd,buf,size+8);
p = strstr(buf,"speed=");
p = p+strlen("speed=");
*p = '800';//在此处修改 将speed改为800
lseek(fd,0,SEEK_SET);
int n_write=write(fd,buf,size);
close(fd);
printf("%s",buf);
}
运行本程序时,则会报错 multi-character character constant [-Wmultichar]
转换溢出,还有可能指针类型不匹配。我们需要将改变的数字转换为字符串
sprintf()函数
sprintf(string,formt,value)函数
本函数有三个参数 string就是指向要写入的那个字符串的指针
formt为转换的类型和精度
将value打印到string中
我们将学量改为800
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>
//int open(const char *pathname, int flags);
//int open(const char *pathname, int flags, mode_t mode);
int main()
{
int fd;
char *buf = NULL;
char *p;
char *string;
int i;
string = (char *)malloc(sizeof(int)*4);
fd = open("./test",O_RDWR);
if(fd==-1){
printf("error\n");
}else{
printf("success\n");
}
int size=lseek(fd,0,SEEK_END);
buf = (char*)malloc(sizeof(char)*size+8);
lseek(fd,0,SEEK_SET);
int n_read=read(fd,buf,size+8);
p = strstr(buf,"blood=");
p = p+strlen("blood=");
sprintf(string,"%d",800);//将800写入字符串中
for(i=0;i<strlen(string);i++){ //通过移动指针p一个个写入
*p = string[i];
p++;
}
lseek(fd,0,SEEK_SET);
int n_write=write(fd,buf,size);
close(fd);
printf("%s",buf);
}
血量已改为800
本此内容全部结束,有不懂的地方可以评论区留言哦