栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

C语言简单实现对文件的KV存储

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

C语言简单实现对文件的KV存储

最近工作中需要实现对文件的读取,删除,写入操作,就参考各路大佬的博文,自己使用c语言简单的实现了对文件的增删改查操作,主要实现如下

kv.c文件主要实现对文件的读,写,删除操作接口的实现,

一.

*函数名 : RemoveKeyValue
 *函数功能描述 :如果指定文件存在key就删除key及value
 *函数入参:  char *fileName 文件名,char *key 键名

二.

三.

具体代码如下:

#include "kv.h"


void RemoveKeyValue(char *fileName,char *key)
{
	char buf[ABUP_JSON_VALUE_SIZE] = "";
	int isExist = 0;
	int i = 0;
	
	FILE *fp,*temp;
	fp = fopen(fileName,"r");
	temp = fopen("temp.txt","w");
	
	if(fp == NULL)
    {
		printf("----------该文件不存在-----n");
		goto finish;
	}
	fgets(buf,ABUP_JSON_VALUE_SIZE,fp);
	do{
		while(buf[i] == ' ')
        {
			i++;
		}
		
		if(strncmp(&buf[i],key,strlen(key))==0)
        {
			continue;
		}
        else
        {
			fputs(buf,temp);
		}	

	}while(fgets(buf,ABUP_JSON_VALUE_SIZE,fp));
	fclose(fp);
	fclose(temp);
	
	fp = fopen(fileName,"w");
	temp = fopen("temp.txt","r");

	while(1)
    {
		fgets(buf,sizeof(buf),temp);
		if(feof(temp))
        {
			printf("删除成功n");
			break;
		}
		fputs(buf,fp);
		
	}
	fclose(fp);
	fclose(temp);
	remove("temp.txt");
	
finish:
    return ; 
}


void WriteKeyValue(char *fileName,char *key,char *value)
{
	char buf[ABUP_JSON_VALUE_SIZE] = "";
	int i = 0;
	int isExist = 0;
	
	FILE *fp,*temp;
	fp = fopen(fileName,"r");

	if(fp == NULL)
    {
		fp = fopen(fileName,"w");
		fprintf(fp,"%s = %sn",key,value);
		fclose(fp);
		printf("-------写入成功----------n");
	}
	else
	{
		temp = fopen("temp.txt","w");
		fgets(buf,ABUP_JSON_VALUE_SIZE,fp);
		do{
			while(buf[i] == ' ')
			{
				i++;
			}
			
			if(strncmp(&buf[i],key,strlen(key))==0)
			{
				fprintf(temp,"%s = %sn",key,value);
				isExist++;
			}
			else
			{
				fputs(buf,temp);
			}	

		}while(fgets(buf,ABUP_JSON_VALUE_SIZE,fp));
		if(isExist == 0)
		{
			fprintf(temp,"%s = %sn",key,value);
		}
		fclose(fp);
		fclose(temp);
		
		fp = fopen(fileName,"w");
		temp = fopen("temp.txt","r");

		while(1)
		{
			fgets(buf,sizeof(buf),temp);
			if(feof(temp))
			{
				printf("-------写入成功----------n");
				break;
			}
			fputs(buf,fp);
		}
		fclose(fp);
		fclose(temp);
		remove("temp.txt");
	}
}


char* ReadValue(char *fileName,char *key)
{
	char buf[ABUP_JSON_VALUE_SIZE] = "";
	int i = 0;
	char *value = NULL;//  value 
	int valueLen = 0;//value长度
	char *valueBuf = NULL;
	int isExist = 0;
	FILE *fp;
	
	fp = fopen(fileName,"r");
	if(fp == NULL)
    {
		goto finish;
	}

	fgets(buf,ABUP_JSON_VALUE_SIZE,fp);
	do{
		
		while(buf[i] == ' ')
        {
			i++;
		}
		
		if(strncmp(&buf[i],key,strlen(key))==0)
        {
			value = strstr(&buf[i],"=")+1;
			valueLen = strlen(value);
			valueBuf = (char*)malloc(sizeof(char)*(valueLen+1));
			i = 0;
			while(i 

kv.h文件主要实现增删改查接口函数声明,具体代码如下:

#ifndef __KV_H__
#define __KV_H__

#ifdef __cplusplus
extern "C" {
#endif

#include 
#include 
#include 


#define ABUP_JSON_VALUE_SIZE  1024
#define ABUP_JSON_KEY_SIZE    128



void WriteKeyValue(char *fileName,char *key,char *value);

char* ReadValue(char *fileName,char *key);

void RemoveKeyValue(char *fileName,char *key);

#ifdef __cplusplus
}
#endif

#endif // __KV_H__

demo.c文件中测试接口函数写好,main函数前面还实现了一个生成随机字符串的函数,根据自己的需要将代码注释打开进行测试。

demo.c:主要实现对kv.c各个函数接口的调用,代码如下:

#include "kv.h"
#include 
#include 

//产生长度为length-1长度的随机字符串
char* genRandomString(int length)
{
	int flag, i;
	char* string;
	srand((unsigned) time(NULL ));
	if ((string = (char*) malloc(length)) == NULL )
	{
		return NULL ;
	}
 
	for (i = 0; i < length - 1; i++)
	{
		flag = rand() % 3;
		switch (flag)
		{
			case 0:
				string[i] = 'A' + rand() % 26;
				break;
			case 1:
				string[i] = 'a' + rand() % 26;
				break;
			case 2:
				string[i] = '0' + rand() % 10;
				break;
			default:
				string[i] = 'x';
				break;
		}
	}
	string[length - 1] = '';
	return string;
}

int main(int argc, char const *argv[])
{
	char fileName[50] = "test";//文件名
	char fileName1[50] = "test1";//文件名
	char key[ABUP_JSON_KEY_SIZE] = "10";//键
	char *key1 = NULL;
	char *value1 = NULL;
	char value[ABUP_JSON_KEY_SIZE] = "hello";//值
	char *temp = NULL;
	int i=0;

	


	//WriteKeyValue(fileName,key,value);
	


	return 0;
}

才学疏浅,如有错误,还望各路大佬指正,在下不甚感激...

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/1032627.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号