目的
读出类似这样的配置文件
abc:abc
并且对文件进行一个简单的加密操作
注意的点
- 配置信息结构体key和value要自己设定,这不是在python中
- 对于C语言中动态内存分配记得要提供“一个返回有效行数的函数”
config.h
| #include <stdio.h> |
| #include <stdlib.h> |
| #include <string.h> |
| |
| |
| struct ConfigInfo |
| { |
| char key[64]; |
| char value[64]; |
| }; |
| |
| |
| int getFileLines(const char *filePath); |
| |
| |
| int isValidLines(const char* str); |
| |
| |
| void parseFile(const char *filePath,int lines,struct ConfigInfo ** configinfo); |
| |
| |
| char* getInfoByKey(const char * key, struct ConfigInfo * configinfo,int len); |
| |
| |
| void freeConfigInfo(struct ConfigInfo* configinfo); |
config.c
| #include "config.h" |
| |
| int getFileLines(const char *filePath) |
| { |
| FILE *file=fopen(filePath, "r"); |
| if (file == NULL) { |
| return -1; |
| } |
| char buf[1024] = { 0 }; |
| int lines=0; |
| while (fgets(buf, 1024, file) != NULL) |
| { |
| if (isValidLines(buf)) |
| { |
| lines++; |
| } |
| memset(buf, 0, 1024); |
| } |
| return lines; |
| fclose(file); |
| } |
| |
| int isValidLines(const char* str) |
| { |
| if (strchr(str, ':') == NULL) |
| { |
| return 0; |
| } |
| return 1; |
| } |
| |
| void parseFile(const char *filePath, int lines, struct ConfigInfo ** configinfo) |
| { |
| struct ConfigInfo *info=(ConfigInfo *)malloc(sizeof(struct ConfigInfo)*lines); |
| if (info == NULL) |
| { |
| return; |
| } |
| FILE * file = fopen(filePath, "r"); |
| if (file == NULL) { |
| return; |
| } |
| char buf[1024] = { 0 }; |
| int index = 0; |
| while (fgets(buf, 1024, file) != NULL) |
| { |
| if (isValidLines(buf)) |
| { |
| |
| |
| memset(info[index].key, 0, 64); |
| memset(info[index].value, 0, 64); |
| char * pos = strchr(buf, ':'); |
| strncpy(info[index].key, buf, pos - buf); |
| strncpy(info[index].value, pos+1, strlen(pos+1)-1); |
| index++; |
| } |
| memset(buf, 0, 1024); |
| } |
| *configinfo = info; |
| } |
| |
| char* getInfoByKey(const char * key, struct ConfigInfo * configinfo, int len) |
| { |
| for (int i = 0; i < len; i++) { |
| if (strcmp(key, configinfo[i].key) == 0) |
| { |
| return configinfo[i].value; |
| } |
| } |
| } |
| |
| |
| void freeConfigInfo(struct ConfigInfo* configinfo) |
| { |
| if (configinfo != NULL) |
| { |
| free(configinfo); |
| configinfo = NULL; |
| } |
| } |
code.h
| #include <string.h> |
| #include <stdlib.h> |
| #include <time.h> |
| |
| void codeFile(const char *sourceFilePath, const char *destFilePath); |
| |
| void decodeFile(const char *sourceFilePath, const char *destFilePath); |
code.c
| #include "code.h" |
| |
| void codeFile(const char *sourceFilePath, const char *destFilePath) |
| { |
| srand((unsigned int)time(NULL)); |
| FILE *fp1 = fopen(sourceFilePath, "r"); |
| FILE *fp2 = fopen(destFilePath, "w"); |
| |
| if (!fp1 || !fp2) { |
| printf("文件打开失败\n"); |
| return; |
| } |
| char ch; |
| |
| while ((ch = fgetc(fp1)) != EOF) { |
| |
| |
| |
| short temp = (short)ch; |
| temp = temp << 4; |
| |
| |
| |
| |
| temp = temp | 0x8000; |
| |
| |
| temp += rand() % 16; |
| fprintf(fp2, "%hd", temp); |
| } |
| fclose(fp1); |
| fclose(fp2); |
| } |
| |
| |
| void decodeFile(const char *sourceFilePath, const char *destFilePath) |
| { |
| FILE *fp1 = fopen(sourceFilePath, "r"); |
| FILE *fp2 = fopen(destFilePath, "w"); |
| |
| if (!fp1 || !fp2) { |
| printf("文件打开失败\n"); |
| return; |
| } |
| |
| |
| |
| short temp; |
| |
| while (!feof(fp1)) |
| { |
| fscanf(fp1, "%hd", &temp); |
| temp = temp << 1; |
| temp = temp >> 5; |
| char ch = (char)temp; |
| fputc(ch, fp2); |
| } |
| fclose(fp1); |
| fclose(fp2); |
| } |
使用
| |
| #include "config.h" |
| #include <stdio.h> |
| #include <stdlib.h> |
| #include <string.h> |
| #include "code.h" |
| int main_6() { |
| const char *filePath = "./config.txt"; |
| |
| int len = getFileLines(filePath); |
| printf("文件的有效行数为:%d\n", len); |
| |
| struct ConfigInfo *configInfo = NULL; |
| parseFile(filePath, len, &configInfo); |
| |
| printf("heroid=%s\n", getInfoByKey("heroid", configInfo, len)); |
| printf("herowe=%s\n", getInfoByKey("herowe", configInfo, len)); |
| |
| freeConfigInfo(configInfo); |
| configInfo = NULL; |
| |
| |
| codeFile("./config.txt", "./加密文件.txt"); |
| |
| decodeFile("./加密文件.txt", "./解密文件.txt"); |
| getchar(); |
| return 0; |
| } |
| |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· 写一个简单的SQL生成工具
· AI 智能体引爆开源社区「GitHub 热点速览」
· C#/.NET/.NET Core技术前沿周刊 | 第 29 期(2025年3.1-3.9)