#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <errno.h>
#include <arpa/inet.h>
#include <fcntl.h>
int CopyFile(char *pcDstFile, char *pcSrcFile)
{
FILE *fpr = NULL;
FILE *fpw = NULL;
int iLen = 0;
int iFileLen = 0;
int iSize = 0;
char acBuf[4096];
if (NULL == pcDstFile || NULL == pcSrcFile)
{
return -1;
}
fpr = fopen(pcSrcFile, "rb");
if (fpr == NULL)
{
perror("open read file:");
return -1;
}
fpw = fopen(pcDstFile, "wb");
if (fpw == NULL)
{
printf("[%s]open write file %s\n", __FUNCTION__, pcDstFile);
perror("open write file error:");
return -1;
}
while (1)
{
iLen = fread(acBuf, 1, sizeof(acBuf), fpr);
if (iLen <= 0)
{
printf("read file over\n");
break;
}
iSize = fwrite(acBuf, 1, iLen, fpw);
if (iSize != iLen)
{
perror("wirte file error:");
fclose(fpr);
fclose(fpw);
return -1;
}
iFileLen += iLen;
}
fflush(fpw);
fsync(fileno(fpw));
fclose(fpr);
fclose(fpw);
return iFileLen;
}
int ModifyParam(char *pcFileName, char *pcKey, char *pcValue)
{
FILE *pOldFile = NULL;
FILE *pNewFile = NULL;
char acTmpName[128];
char acBuf[256];
int iFlag = 0;
pOldFile = fopen(pcFileName, "rb");
if (NULL == pOldFile)
{
printf("[%s] open file %s error\n", __FUNCTION__, pcFileName);
perror(":");
return -1;
}
snprintf(acTmpName, sizeof(acTmpName), "%s~", pcFileName);
pNewFile = fopen(acTmpName, "wb");
if (NULL == pNewFile)
{
printf("[%s] open file %s error\n", __FUNCTION__, acTmpName);
perror(":");
fclose(pOldFile);
return -1;
}
while (fgets(acBuf, sizeof(acBuf), pOldFile))
{
if (memcmp(acBuf, pcKey, strlen(pcKey)) == 0)
{
snprintf(acBuf, sizeof(acBuf), "%s=%s\n", pcKey, pcValue);
fputs(acBuf, pNewFile);
iFlag++;
}
else
{
fputs(acBuf, pNewFile);
}
}
// 如果没有此项,则添加一项
if (0 == iFlag)
{
snprintf(acBuf, sizeof(acBuf), "%s=%s\n", pcKey, pcValue);
fputs(acBuf, pNewFile);
}
fflush(pNewFile);
fsync(fileno(pNewFile));
fclose(pOldFile);
fclose(pNewFile);
//rename(acTmpName, pcFileName);
CopyFile(pcFileName, acTmpName);
return 0;
}
int ReadParam(char *pcFileName, char *pcKey, char *pcValue)
{
FILE *pFile = NULL;
char acBuf[128];
char *pcPos = NULL;
int iLen = 0;
int iRet = -1;
pFile = fopen(pcFileName, "rb");
if (NULL == pFile)
{
printf("[%s] open file %s error\n", __FUNCTION__, pcFileName);
perror(":");
return -1;
}
while (fgets(acBuf, sizeof(acBuf), pFile))
{
if (memcmp(acBuf, pcKey, strlen(pcKey)) == 0)
{
pcPos = acBuf;
strsep(&pcPos, "=");
if (NULL == pcPos)
{
printf("line:%s, param error!!!\n", acBuf);
break;
}
iLen = strlen(pcPos);
if (iLen > 0)
pcPos[iLen-1] = '\0';
strcpy(pcValue, pcPos);
iRet = 0;
break;
}
}
fclose(pFile);
return iRet;
}
linux c,ini配置文件读写模块
需积分: 5 65 浏览量
2022-10-17
20:56:16
上传
评论
收藏 1KB ZIP 举报
flypig哗啦啦
- 粉丝: 5914
- 资源: 24
最新资源
- 基于Vue+Echarts实现风力发电机中传感器的数据展示监控可视化系统+源代码+文档说明(高分课程设计)
- 基于单片机的风力发电机转速控制源码
- 基于C++实现的风力发电气动平衡监测系统+源代码+测量数据(高分课程设计)
- 毕业设计- 基于STM32F103C8T6 单片机,物联网技术的太阳能发电装置+源代码+文档说明+架构图+界面截图
- 基于 LSTM(长短期记忆)(即改进的循环神经网络)预测风力发电厂中风力涡轮机产生的功率+源代码+文档说明
- 基于stm32f103+空心杯电机+oled按键+运动算法
- 《CKA/CKAD应试指南/从docker到kubernetes 完全攻略》学习笔记 第1章docker基础(1.1-1.4)
- 基于python实现的水下压缩空气储能互补系统建模仿真与经济效益分析+源代码+论文
- 华中科技大学-自然语言处理实验,Bi-LSTM+CRF的中文分词框架,并且利用基于深度学习的方法进行中文命名实体识别++源码报告
- 基于动态罚函数的铁路车流分配与径路优化模型python源码
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
评论0