SX_c语言替换文件中的字符串_22

项目实现中有时候需要根据一些设置的值替换掉代码文件中的一些字符串,让显示有所变化,这里有两种方式替换

利用查找字符串方式:

c 复制代码
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

void replace_string_in_js_file(const char *filename, const char *old_str, const char *new_str) {
    // 打开文件
    FILE *file = fopen(filename, "r");
    if (file == NULL) {
        perror("Error opening file");
        return;
    }

    // 获取文件大小
    fseek(file, 0, SEEK_END);
    long file_size = ftell(file);
    fseek(file, 0, SEEK_SET);

    // 分配内存以存储文件内容
    char *file_content = (char *)malloc(file_size + 100);
    if (file_content == NULL) {
        perror("Memory allocation failed");
        fclose(file);
        return;
    }

    // 读取文件内容
    fread(file_content, 1, file_size, file);
    file_content[file_size] = '\0'; // 确保字符串正确终止

    // 替换字符串
    char *pos = file_content;
    long offset = 0; // 用于记录偏移量
    while ((pos = strstr(pos, old_str)) != NULL) {
        // 计算新字符串的长度差异
        int old_len = strlen(old_str);
        int new_len = strlen(new_str);
        int diff = new_len - old_len;
		//printf("%d\n", diff);
        // 移动剩余的字符串
        memmove(pos + new_len, pos + old_len, file_size - (pos - file_content) - old_len + 1);

        // 替换字符串
        memcpy(pos, new_str, new_len);

        // 更新文件大小
        file_size += diff;

        // 更新指针位置
        pos += new_len;

        // 更新偏移量
        offset += diff;
    }

    // 回到文件开始处并写入新内容
    fclose(file);
    file = fopen(filename, "w");  // 清空原本内容 
    fseek(file, 0, SEEK_SET);
    fwrite(file_content, 1, file_size, file);

    // 清理
    free(file_content);
    fclose(file);
}

int main() {
    const char *filename = "C:\\Users\\haiyangjiang\\Desktop\\example.js"; // 你的JS文件名
    replace_string_in_js_file(filename, "b", "linux");
	return 0;
}

将文件打开,利用strstr函数查找到第一个匹配的子字符串,将内容拼接后替换,该代码不能替换有换行的代码文本,且容易造成内存溢出,十分不稳定

利用linux命令替换:

c 复制代码
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

void replace_string_from_file(const char* file_path, int oemid) {
    const char* old_str[] = {"Time Server 3001", "Time Server 3002"};
    const char* new_str[] = {"Time Server 3001", "Time Server 3002"};
    char cmd[254];
    int len = sizeof(old_str) / sizeof(old_str[0]);
    
    for (int i = 0; i < len; i++) {
        if (i != oemid) {
            snprintf(cmd, sizeof(cmd), "sed -i \"s/%s/%s/\" %s", old_str[i], new_str[oemid], file_path);
            printf("replace string cmd: %s\n", cmd);
            system(cmd);
            memset(cmd, 0, sizeof(cmd));
        }
    }
}

int main() {
    const char* filename = "/home/saisi/Desktop/Myproject/a.js"; // 你的JS文件名
    replace_string_from_file(filename, 1);
    return 0;
}

linux中sed -i "s/old_string/new_string/" /etc/filename.txt能将文本中的filename文本中的所有old_string换成new_string如果匹配不到old_string就不替换,利用这个命令实现的函数能解决了上述函数的问题

linux查找文件sudo find / -name "*.txt" 可以对全局文件做查找操作,找到所有目标文件的位置

相关推荐
子兮曰4 小时前
OpenClaw入门:从零开始搭建你的私有化AI助手
前端·架构·github
吴仰晖4 小时前
使用github copliot chat的源码学习之Chromium Compositor
前端
1024小神4 小时前
github发布pages的几种状态记录
前端
不像程序员的程序媛6 小时前
Nginx日志切分
服务器·前端·nginx
Daniel李华6 小时前
echarts使用案例
android·javascript·echarts
北原_春希6 小时前
如何在Vue3项目中引入并使用Echarts图表
前端·javascript·echarts
JY-HPS6 小时前
echarts天气折线图
javascript·vue.js·echarts
尽意啊6 小时前
echarts树图动态添加子节点
前端·javascript·echarts
吃面必吃蒜6 小时前
echarts 极坐标柱状图 如何定义柱子颜色
前端·javascript·echarts
O_oStayPositive6 小时前
Vue3使用ECharts
前端·javascript·echarts