设为首页 加入收藏

TOP

C读写配置文件(二)
2015-01-22 21:37:18 来源: 作者: 【 】 浏览:179
Tags:读写 配置 文件
冲区 char *pLine = NULL; // 每行缓冲区数据的指针 if(filepath == NULL || handle == NULL) { ret = -1; printf("fun init error:%d from (filepath == NULL || handler == NULL)\n",ret); return ret; } ph = (PROPS_HANDLE *)malloc(sizeof(PROPS_HANDLE)); if (ph == NULL) { ret = -2; printf("fun init malloc handle error:%d",ret); return ret; } memset(ph, 0, sizeof(PROPS_HANDLE)); // 打开文件 fp = fopen(filepath, "r"); if (!fp) { ret = -3; printf("fun init open file error:%d from %s\n",ret,filepath); return ret; } // 创建头节点 ret = createPropsNode(&pHead); if (ret != 0) { fclose(fp); // 关闭文件 printf("fun init create head node error:%d\n",ret); return ret; } memset(pHead, 0, sizeof(Properties)); // 保存链表头节点和文件路径到handle中 ph->pHead = pHead; ph->filepath = (char *)malloc(strlen(filepath) + 1); strcpy(ph->filepath, filepath); pCurrent = pHead; // 读取配置文件中的所有数据 while (!feof(fp)) { if(fgets(line, LINE_BUF_SIZE, fp) == NULL) { break; } // 找等号 if ((pLine = strstr(line, "=")) == NULL) { // 没有等号,继续读取下一行 continue; } // 循环创建节点 ret = createPropsNode(&pMalloc); if (ret != 0) { fclose(fp); // 关闭文件 release((void **)&ph); // 创建节点失败,释放所有资源 printf("create new node error:%d\n",ret); return ret; } // 设置Key memcpy(keybuff, line, pLine-line); trimeSpace(keybuff, pMalloc->key); // 将keybuff去空格后放到pMallock.key中 // 设置Value pLine += 1; trimeSpace(pLine, valuebuff); strcpy(pMalloc->value, valuebuff); // 将新节点入链表 pMalloc->pNext = NULL; pCurrent->pNext = pMalloc; pCurrent = pMalloc; // 当前节点下移 // 重置key,value memset(keybuff, 0, KEY_SIZE); memset(valuebuff, 0, VALUE_SIZE); } // 设置环境句柄给调用者 *handle = ph; // 关闭文件 fclose(fp); return ret; } // 获取属性数量,成功返回0,失败返回非0值 int getCount(void *handle, int *count) { int ret = 0,cn = 0; PROPS_HANDLE *ph = NULL; Properties *pCurrent = NULL; if (handle == NULL || count == NULL) { ret = -1; printf("fun getCount error:%d from (handle == NULL || count == NULL)\n",ret); return ret; } ph = (PROPS_HANDLE *)handle; pCurrent = ph->pHead->pNext; while (pCurrent != NULL) { cn++; pCurrent = pCurrent->pNext; } *count = cn; return ret; } // 根据KEY获取值,找到返回0,如果未找到返回非0值 int getValue(void *handle, const char *key, char *value) { int ret = 0; PROPS_HANDLE *ph = NULL; Properties *pCurrent = NULL; if (handle == NULL || key == NULL || value == NULL) { ret = -1; printf("getValue error:%d from (handle == NULL || key == NULL || value == NULL)\n",ret); return ret; } ph = (PROPS_HANDLE *)handle; pCurrent = ph->pHead->pNext; while (pCurrent != NULL) { if (strcmp(pCurrent->key,key) == 0) { break; } pCurrent = pCurrent->pNext; } if (pCurrent == NULL) { ret = -2; printf("fun getValue warning: not found the key:%s\n",key); return ret; } strcpy(value, pCurrent->value); return ret; } // 修改key对应的属性值,修改成功返回0,失败返回非0值 int setValue(void *handle, const char *key, const char *value) { int ret = 0; PROPS_HANDLE *ph = NULL; Properties *pCurrent = NULL; if (handle == NULL || key == NULL || value == NULL) { ret = -1; printf("fun setValue error:%d from (handle == NULL || key == NULL || value == NULL)\n",ret); return ret; } // 获得环境句柄 ph = (PROPS_HANDLE *)handle; // 从环境句柄中获取头节点 pCurrent = ph->pHead->pNext; while (pCurrent != NULL) { if (strcmp(pCurrent->key, key) == 0) { // 找到 break; } pCurrent = pCurrent->pNext; } if (pCurrent == NULL) { // 未找到key ret = -2; printf("fun setValue warning: not found the key:%s\n",key); return ret; } // 修改key的value strcpy(pCurrent->value, value); if (strchr(value, '\n') == NULL) { // 加一个换行符 strcat(pCurrent->value, "\n"); } // 将修改的配置项写入到文件 ret = saveConfig(ph->filepath, ph->pHead); return ret; } // 添加一个属性,添加成功返回0,失败返回非0值 i
首页 上一页 1 2 3 4 5 下一页 尾页 2/5/5
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇C语言之预处理 下一篇C语言结构体点滴

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容: