C语言:ini配置文件的编辑器(1)

2023-03-10 13:35:12 浏览数 (1)

前言

最近领了一个小任务,不能调用API,用C语言实现ini配置文件的读写,增删改查。一下是文件思路,相关代码片段。只是主体函数结构,功能性函数进行了说明,并没有详细到语句。随后会对小程序继续修改,一些功能增加,文本的对话框选择。

实现思路

程序流程:1、输入文件路径,存在文件打开,不存在选择是否创建。 2、文本内容的获取,将文本流存入内存中,随后对文本流操作。 3、功能模块实现,增删改查,显示,保存,退出等功能实现

代码片段

1.相关结构体定义

头文件、宏定义

代码语言:javascript复制
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
            
    #define _TCHAR char
          
    #define Len_Max 255 //路径最大长度
    #define Line_Max 1024 //最大行数
    #define Elem_Max 255 //元素最大个数
    #define Elem_Item_Max 255 //元素相最大个数
    #define Item_Name_Max 55 //Item Key 最大长度
    #define Item_Value_Max 55 //Item Value 最大长度
    #define Int_Len 12 //整形长度
            
    #define MAX_INPUT 4294967297
    #define MIN_INPUT -4294967298

.枚举变量:主要包含:控制命令,显示,增,删,改,查

代码语言:javascript复制
enum CtrlCmd{
	Show=0, //显示
	Add,//增
	Del,//删
	Modify,//改
	Search,//查
	Save,//保存文件
	Quit,//退出
	Help,//帮助
	Info,//软件信息
};

enum ShowCmd{
	ShowElemCount=0,//显示元素个数
	ShowElemName,//显示元素名称
	ShowElemStr, //显示文本内容
	ShowBack,//返回上一层
};

enum AddCmd{
	AddElem=0, //指定位置增加元素
	AddElem_Item, //元素指定位置增加子相
	AddBack,//返回上一层
};

enum DelCmd{
	DelElem=0,//删除指定元素
	DelElem_Item,//删除元素指定相
	DelBack,//返回上一层
};

enum ModifyCmd{
	ModifyElem_Name=0,//修改元素名称
	ModifyElem_Item, //修改指定元素指定子相
	ModifyElem_Item_Name,//修改指定元素子相名称
	ModifyElem_Item_Value,//修改指定元素子相值
	ModifyBack,//返回上一层
};

enum SearchCmd{
	SearchElem=0,//根据元素名查找元素
	SearchElem_Item_Name,//根据元素子相名查找对应子相
	SearchElem_Item_value,//根据元素子相值查找对应子相
	SearchWord,//根据字符串查找,模糊查找
	SearchBack,//返回上一层
};

结构体: Elem_str 节点的子相:包含 子相名称,子相值 Elem 元素:包含 元素名称,元素个数,子相容器组。

代码语言:javascript复制
struct Elem_str{
	char  item_name[Item_Name_Max];
	char  item_str[Item_Value_Max];
}; 

struct Elem{
	char * elem_name;
	int elem_count;
	Elem_str  m_Elem_str[Elem_Item_Max]; 
};

全局变量声明:文件名称,文件指针,全局唯一,实现指针的全局唯一,对指针单独操作,防止野指针问题。文件元素数组,实现对整个文本内容存储。

代码语言:javascript复制
Elem m_Elem[Elem_Max];

FILE * file; //定义文件指针
char filename[Len_Max]; //文件路径
int elem_count = 0;//元素个数

2.主函数

代码语言:javascript复制
int main(int argc, _TCHAR* argv[])
{
	printf("---------------Runing---------------n");
	bool Working = false;
	while (Working==false)
	{
		Working = Cmd_Init();
	}	
	return 0;
}

3.函数初始化:主要包含文件的打开,文件的操作

代码语言:javascript复制
/*初始化--文件打开*/
bool Cmd_Init()
{
	bool Working = true;
	printf("------------n0:继续;1:退出n------------n");
	int iCtrlFlag = -1;
	char CtrlFlag[Len_Max] ;
	while(iCtrlFlag == -1)
	{
		scanf("%s",CtrlFlag);
		if (CtrlFlag[0] == '0')
			iCtrlFlag = 0;
		else if (CtrlFlag[0] == '1')	
			iCtrlFlag =1;
		else{
			iCtrlFlag = -1;
			printf("输出指令错误!请重新输入n");
		}
	}
	if (iCtrlFlag == 1) {
		file = NULL;
		memset(&m_Elem,0,sizeof(m_Elem));
		exit(1);
	}		
	printf("请输入文件路径(注:相对路径或者绝对路径)n");
	while(GetFileName() == false);
	if(FileOpen() == false) return false;
	Read_File();
	while(Working)
	{
		Working = Cmd_FileCtrl(); 
	}
	return Working;
}

4.文件路径获取及打开读入内存:文件名的获取,判断后缀,判断是否为ini文件,以及文件名称长度。文件读取,将文本内容写入到对应容器当中,利用元素结构,快捷的实现对文本内容的相关操作。

代码语言:javascript复制
/*获取文件路径*/
bool GetFileName()
{	
	memset(filename,0,Len_Max);
	scanf("%s",filename);
	int len = strlen(filename);
	filename[len]='';
	if (len <=4) {printf("文件路径格式错误,请重新输入正确的文件路径!(注:文件格式后缀必须存在: .ini)n");return false;}
	if (filename[len-4] == '.'&&filename[len-3]=='i'&&filename[len-2] == 'n' && filename[len-1] == 'i') return true;
	else {printf("文件路径格式错误,请重新输入正确的文件路径!(注:文件格式后缀必须存在: .ini)n");return false;}
}

/*打开文件方式*/
bool FileOpen()
{
	file = NULL;
	file  = fopen(filename,"r");
	if (file==NULL)
	{
		printf("------------n文件不存在,是否要创建文件n");
		printf("0:创建;n1:不创建n------------n");
		bool bCreat = GetWorking();
		if (bCreat)
		{
			file  = fopen(filename,"a ");
			if (file!=NULL) printf("文件创建成功n");
			return true;
		}
		return false;
	}else
	{
		printf("文件打开成功n");
		return true;
	}	
}

/*读取文件*/
void Read_File()
{
	char buf[Item_Name_Max];
	int i = 0;
	elem_count = 0;
	while(fgets(buf,Line_Max,file) != NULL)
	{
		int len = strlen(buf);
		buf[len-1]='';
		if (buf[0]=='[')
		{
			m_Elem[i].elem_name = new char[len-1];

			memcpy(m_Elem[i].elem_name,&buf,len-1);
			m_Elem[i].elem_name[len-1] = '';
			m_Elem[i].elem_count=0;
	//		printf("elem_name:%sn",m_Elem[i].elem_name);
			elem_count   ;
			i  ;
		}else
		{
			int key_i = 0;int value_i = 0;
			bool flag = false;
			for (int j = 0;j<len;j  )
			{				
				if (buf[j] == '=')	{flag = true;continue;}
				if (flag == false)
				{
					m_Elem[i-1].m_Elem_str[m_Elem[i-1].elem_count].item_name[key_i] = buf[j];key_i  ;
				}else 
				{
					m_Elem[i-1].m_Elem_str[m_Elem[i-1].elem_count].item_str[value_i] = buf[j];value_i  ;
				}
			}
	//		printf("Key:%s Value:%sn",m_Elem[i-1].m_Elem_str[Elem_num].item_name,m_Elem[i-1].m_Elem_str[Elem_num].item_str);
			m_Elem[i-1].elem_count  ;
		}
	}	
}

5.主体功能模块:主要功能有,文件显示,增,删,改,查,文件保存,程序退出,返回上一层

代码语言:javascript复制
/*对文件进行的操作*/
bool Cmd_FileCtrl()
{
	bool WokingState = true;
	bool ChildWorking = true;
	printf("n------------n0:显示文件内容;n1:增加文件内容;n2.删除文件内容;n3.修改文件内容;n4.查找文件内容;n5.保存文件;n6.程序退出;n7:帮助;n8:软件信息;nOther:返回上一层n------------n");
	int Cmd = 0;
	int iCmd = 0;
	Cmd = CmdVal();
	switch(Cmd)
	{
	case Show:
		while(ChildWorking)
		{
			printf("------------n0:显示元素个数;n1:显示元素名称;n2:显示文本全部内容;n3:返回上一层;n------------n");
			iCmd = CmdVal();
			ChildWorking = Cmd_ShowFile(iCmd);
		}
		break;
	case Add:
		while(ChildWorking)
		{
			printf("------------n0:指定位置增加元素;n1.元素指定位置增加子相;n2.返回上一层;n------------n");
			iCmd = CmdVal();
			ChildWorking = Cmd_Add(iCmd);
		}
		break;
	case Del:
		while (ChildWorking)
		{
			printf("------------n0:删除指定元素;n1:删除元素指定相;n2.返回上一层;n------------n");
			iCmd = CmdVal();
			ChildWorking = Del_Cmd(iCmd);
		}
		break;
	case Modify:
		while (ChildWorking)
		{
			printf("------------n0:修改元素名称;n1:修改元素指定子相;n2:修改指定元素子相名称;n3:修改指定元素子相值;n4.返回上一层n------------n");
			iCmd = CmdVal();
			ChildWorking = Cmd_Modify(iCmd);
		}
		break;
	case Search:
		while(ChildWorking)
		{
			printf("------------n0:根据元素名查找元素;n1:根据元素子相名查找对应子相;n2:根据元素子相值查找对应子相;n3.模糊查找;n4.返回上一层n------------n");
			iCmd = CmdVal();
			ChildWorking = Cmd_Search(iCmd);
		}
		break;
	case Save:
		printf("------------n正在保存文件n");
		if(File_Write() == true) printf("文件保存成功!n------------n"); else printf("文件保存失败!n------------n");
		printf("0:继续;other:退出n");
		WokingState =GetWorking();
		break;
	case Quit:
		printf("正在退出n");
		file = NULL;
		elem_count = 0;
		memset(&m_Elem,0,sizeof(m_Elem));
		exit(1);
		break;
	case Help:
		printf("操作信息n");
		printf("软件介绍:t软件主要实现(.ini)文件的编辑,主要包括文件的创建、增、删、改、查。nt创建:如果没有对应文件,则会有创建文件选项。n");
		printf("t增:主要有元素节点的增加、元素子相的增加。 nt删:主要有元素节点得到删除、元素子相节点的删除。n");
		printf("t改:主要有元素节点名称修改,元素子相修改,元素子相名称修改,元素子相值修改n");
		printf("t查:主要有根据元素名称查找对应元素、根据元素子相名称查找对应子相、根据子相值查找对应子相、模糊查找。(注:查找为全字符匹配)n");
		printf("操作方式:t根据提示信息输入对应键值即可进行相应操作。t");
		break;
	case Info:
		printf("软件名:配置信息文件(.ini)编辑软件n软件版本:V0.1n创建时间:2019.05.12n作者:wqn");
		break;
	default:
		file = NULL;
		memset(&m_Elem,0,sizeof*(m_Elem));
		WokingState =false;
		printf("正在返回上一层n");
		break;
	}
	return WokingState;
}

显示:包含,显示文件元素个数,元素名称,全部文本内容

代码语言:javascript复制
bool Cmd_ShowFile(int ShowFlag)
{
	bool Working = true;
	switch(ShowFlag)
	{
	case ShowElemCount:
		printf("元素个数为:%dn",elem_count);
		Working = true;
		break;
	case ShowElemName:
		for (int i = 0 ;i<elem_count;i  )
		{
			printf("Elem_Name:%sn",m_Elem[i].elem_name);
		}
		Working = true;
		break;
	case ShowElemStr:
		for (int i = 0 ;i<elem_count;i  )
		{
			printf("Elem_Name:%sn",m_Elem[i].elem_name);
			int Count = m_Elem[i].elem_count;
			for(int j = 0;j<Count;j  )
			{
				printf("Key:%s Value:%sn",m_Elem[i].m_Elem_str[j].item_name,m_Elem[i].m_Elem_str[j].item_str);
			}			
		}
		Working = true;
		break;
	case ShowBack:
		Working = false;
		break;
	default:
		printf("------------n无效操作符!是否返回上一层!n0:继续;other:返回n------------n");
		Working = GetWorking();
		break;
	}
	return Working;
}

增:包含,增加元素,增加元素子相,元素单次只能增加一个,子相可以增加多个。在增加元素时,首先选择元素插入节点位置,其他元素移位,从末尾向后移位,依次处理

代码语言:javascript复制
bool Cmd_Add(int AddFlag)
{
	bool working = true;
	int iElemPos = 0;
	char * elem_name;
	int iItemCount = 0;
	char  cItemName[Item_Name_Max],cItemValue[Item_Value_Max];
	int iItemPos = 0;
	switch(AddFlag)
	{
	case AddElem:
		printf("请输入元素写入位置n");
		iElemPos = InputVal();
		if (iElemPos>=elem_count)
		{
			iElemPos = elem_count;
		}
		if(iElemPos < elem_count)
		{		
			for (int i = elem_count;i>iElemPos;i--)
			{
				memset(&m_Elem[i],0,sizeof(Elem));
				m_Elem[i].elem_name = new char[Item_Name_Max];
				char * buf_name	 = new char[strlen(m_Elem[i-1].elem_name)];
				buf_name = m_Elem[i-1].elem_name;
				memcpy(&m_Elem[i].elem_name,&m_Elem[i-1].elem_name,strlen(m_Elem[i-1].elem_name));
				m_Elem[i].elem_name[strlen(m_Elem[i-1].elem_name)] = '';
				m_Elem[i].elem_count = m_Elem[i-1].elem_count;
				for(int j = 0;j<m_Elem[i-1].elem_count;j  )
				{
					memcpy(&m_Elem[i].m_Elem_str[j].item_name,&m_Elem[i-1].m_Elem_str[j].item_name,strlen(m_Elem[i-1].m_Elem_str[j].item_name));
					memcpy(&m_Elem[i].m_Elem_str[j].item_str,&m_Elem[i-1].m_Elem_str[j].item_str,strlen(m_Elem[i-1].m_Elem_str[j].item_str));
					m_Elem[i].m_Elem_str[j].item_name[strlen(m_Elem[i-1].m_Elem_str[j].item_name)]='';
					m_Elem[i].m_Elem_str[j].item_str[strlen(m_Elem[i-1].m_Elem_str[j].item_str)]='';
				}
			}
		}else
		{
			memset(&m_Elem[iElemPos],0,sizeof(Elem));
			m_Elem[iElemPos].elem_name = new char[Item_Name_Max];
		}
			printf("请输入元素名字,元素子相个数n");
			elem_name = new char[Item_Name_Max];
			scanf("%s",elem_name);
			m_Elem[iElemPos].elem_name = new char(strlen(elem_name));
			memcpy(m_Elem[iElemPos].elem_name,elem_name,strlen(elem_name));
			m_Elem[iElemPos].elem_name[strlen(elem_name)] = '';
			elem_name = NULL;
			iItemCount = InputVal();
			if(iItemCount>Elem_Item_Max) iItemCount = Elem_Item_Max;
			m_Elem[iElemPos].elem_count = iItemCount;
			for (int j = 0;j<iItemCount;j  )
			{
				printf("请输入第%d个子相,子相名称,子相值n",j);
				memset(&m_Elem[iElemPos].m_Elem_str[j],0,sizeof(Elem_str));
				scanf("%s",m_Elem[iElemPos].m_Elem_str[j].item_name);	
				scanf("%s",m_Elem[iElemPos].m_Elem_str[j].item_str);
				m_Elem[iElemPos].m_Elem_str[j].item_name[strlen(m_Elem[iElemPos].m_Elem_str[j].item_name)]='';
				m_Elem[iElemPos].m_Elem_str[j].item_str[strlen(m_Elem[iElemPos].m_Elem_str[j].item_str)] = '';
			}
		
		elem_count  ;
		working = true; 
		break;
	case AddElem_Item:
		printf("请输入增加子相元素位置n");
		iElemPos = InputVal();
		if (iElemPos>=elem_count)
		{
			break;
		}else
		{
			printf("请输入插入子相位置n");
			iItemPos = InputVal();
			if (iItemPos>=m_Elem[iElemPos].elem_count)
			{
				iItemPos = m_Elem[iElemPos].elem_count;
			} 
			for (int i = m_Elem[iElemPos].elem_count;i>iItemPos;i--)
			{
				memcpy(&m_Elem[iElemPos].m_Elem_str[i].item_name,&m_Elem[iElemPos].m_Elem_str[i-1].item_name,strlen(m_Elem[iElemPos].m_Elem_str[i-1].item_name));
				memcpy(&m_Elem[iElemPos].m_Elem_str[i].item_str,&m_Elem[iElemPos].m_Elem_str[i-1].item_str,strlen(m_Elem[iElemPos].m_Elem_str[i-1].item_str));
				m_Elem[iElemPos].m_Elem_str[i].item_name[strlen(m_Elem[iElemPos].m_Elem_str[i-1].item_name)]='';
				m_Elem[iElemPos].m_Elem_str[i].item_str[strlen(m_Elem[iElemPos].m_Elem_str[i-1].item_str)]='';
			}
			printf("请输入插入子相名称,子相值n");
			memset(&m_Elem[iElemPos].m_Elem_str[iItemPos],0,sizeof(Elem_str));
			scanf("%s",&m_Elem[iElemPos].m_Elem_str[iItemPos].item_name);
			scanf("%s",&m_Elem[iElemPos].m_Elem_str[iItemPos].item_str);
			m_Elem[iElemPos].m_Elem_str[iItemPos].item_name[strlen(m_Elem[iElemPos].m_Elem_str[iItemPos].item_name)]='';
			m_Elem[iElemPos].m_Elem_str[iItemPos].item_str[strlen(m_Elem[iElemPos].m_Elem_str[iItemPos].item_str)]='';
			m_Elem[iElemPos].elem_count  ;
		}
		working = true;
		break;
	case AddBack:
		working = false;
		break;
	default:
		printf("------------n无效操作符!是否返回上一层!n0:继续;other:返回n------------n");
		working = GetWorking();
		break;
	}
	delete elem_name;
	elem_name = NULL;
	return working;
}

删:包含,删除元素,删除子相

代码语言:javascript复制
/*删除操作*/
bool Del_Cmd(int DelFlag)
{
	int iElem_Pos=0;
	int iItem_Pos=0;
	bool Working = true;
	switch(DelFlag)
	{
	case DelElem:
		printf("请输入删除元素位置n");
		iElem_Pos = InputVal();
		if(iElem_Pos>=elem_count) {printf("此位置无元素n");break;}
 		memset(&m_Elem[iElem_Pos],0,sizeof(Elem));
		if (iElem_Pos == elem_count-1){elem_count--;break;} 
		
		for(int i = iElem_Pos;i<elem_count-1;i  )
		{
			if(m_Elem[i].elem_name == NULL)  
				m_Elem[i].elem_name =new char[Item_Name_Max];
			else{
				memset(m_Elem[i].elem_name,0,strlen(m_Elem[i 1].elem_name));
				m_Elem[i].elem_name =new char[Item_Name_Max];
			}
			memcpy(m_Elem[i].elem_name,m_Elem[i 1].elem_name,strlen(m_Elem[i 1].elem_name));
			m_Elem[i].elem_name[strlen(m_Elem[i 1].elem_name)] = '';
			m_Elem[i].elem_count = m_Elem[i 1].elem_count;
			for(int j = 0;j<m_Elem[i 1].elem_count;j  )
			{
				memcpy(&m_Elem[i].m_Elem_str[j].item_name,&m_Elem[i 1].m_Elem_str[j].item_name,strlen(m_Elem[i 1].m_Elem_str[j].item_name));
				memcpy(&m_Elem[i].m_Elem_str[j].item_str,&m_Elem[i 1].m_Elem_str[j].item_str,strlen(m_Elem[i 1].m_Elem_str[j].item_str));
				m_Elem[i].m_Elem_str[j].item_name[strlen(m_Elem[i 1].m_Elem_str[j].item_name)]='';
				m_Elem[i].m_Elem_str[j].item_str[strlen(m_Elem[i 1].m_Elem_str[j].item_str)]='';
			}
		}
		memset(&m_Elem[elem_count-1],0,sizeof(Elem));
		elem_count--;
		Working = true;
		break;
	case DelElem_Item:
		printf("请输入元素位置,删除相位置n");
		iElem_Pos = InputVal();
		if(iElem_Pos>=elem_count) {printf("该位置不存在元素n");break;}
		iItem_Pos = InputVal();
		if(iItem_Pos>=m_Elem[iItem_Pos].elem_count) {printf("该位置不存在子相n");break;}
		memset(&m_Elem[iElem_Pos].m_Elem_str[iItem_Pos],0,sizeof(Elem_str));
		for(int i = iItem_Pos;i<m_Elem[iItem_Pos].elem_count-1;i  )
		{
			memcpy(&m_Elem[iElem_Pos].m_Elem_str[i].item_name,&m_Elem[iElem_Pos].m_Elem_str[i 1].item_name,strlen(m_Elem[iElem_Pos].m_Elem_str[i 1].item_name));
			memcpy(&m_Elem[iElem_Pos].m_Elem_str[i].item_str,&m_Elem[iElem_Pos].m_Elem_str[i 1].item_str,strlen(m_Elem[iElem_Pos].m_Elem_str[i 1].item_str));
			m_Elem[iElem_Pos].m_Elem_str[i].item_name[strlen(m_Elem[iElem_Pos].m_Elem_str[i 1].item_name)]='';
			m_Elem[iElem_Pos].m_Elem_str[i].item_str[strlen(m_Elem[iElem_Pos].m_Elem_str[i 1].item_str)]='';
		}
		memset(&m_Elem[iElem_Pos].m_Elem_str[elem_count-1],0,sizeof(Elem_str));
		m_Elem[iItem_Pos].elem_count--;
		Working = true;
		break;
	case DelBack:
		Working = false;
		break;
	default:
		printf("------------n未识别命令,是否返回上一层!n0:继续;other:返回n------------n");
		Working = GetWorking();
		break;
	}
	return Working;
}

改:实现整个元素修改,子相修改,子相名称修改,子相值修改

代码语言:javascript复制
   /*修改*/
    bool Cmd_Modify(int iModify)
    {
    	int iElem_Pos = 0;
    	int iItem_Pos = 0;
    	char Elem_name[Item_Name_Max];
    	char Item_Name[Item_Name_Max];
    	char Item_Value[Item_Value_Max];
    	bool Working = true;
    	switch(iModify)
    	{
    	case ModifyElem_Name:
    		printf("请输入修改元素位置n");
    		iElem_Pos = InputVal();
    		if(iElem_Pos>=elem_count) {printf("元素不存在n");break;}
    		printf("请输入元素名称n");
    		scanf("%s",Elem_name);
    		memcpy(m_Elem[iElem_Pos].elem_name,&Elem_name,strlen(Elem_name));
    		m_Elem[iElem_Pos].elem_name[strlen(Elem_name)] = '';
    		Working = true;
    		break;
    	case ModifyElem_Item:
    		printf("请输入修改元素位置n");
    		iElem_Pos = InputVal();
    		if(iElem_Pos>=elem_count) {printf("元素不存在n");break;}
    		printf("请输入修改子相位置n");
    		iItem_Pos = InputVal();
    		if (iItem_Pos>=m_Elem[iElem_Pos].elem_count)	{printf("元素子相不存在n");break;}
    		printf("请输入修改子相名称n");
    		scanf("%s",Item_Name);
    		memcpy(&m_Elem[iElem_Pos].m_Elem_str[iItem_Pos].item_name,&Item_Name,strlen(Item_Name));
    		m_Elem[iElem_Pos].m_Elem_str[iItem_Pos].item_name[strlen(Item_Name)] = '';
    		printf("请输入修改子相值n");
    		scanf("%s",Item_Value);
    		memcpy(&m_Elem[iElem_Pos].m_Elem_str[iItem_Pos].item_str,&Item_Value,strlen(Item_Value));
    		m_Elem[iElem_Pos].m_Elem_str[iItem_Pos].item_str[strlen(Item_Value)] = '';
    		Working = true;
    		break;
    	case ModifyElem_Item_Name:
    		printf("请输入修改元素位置n");
    		iElem_Pos = InputVal();
    		if(iElem_Pos>=elem_count) {printf("元素不存在n");break;}
    		printf("请输入修改子相位置n");
    		iItem_Pos = InputVal();
    		if (iItem_Pos>=m_Elem[iElem_Pos].elem_count)	{printf("元素子相不存在n");break;}
    		printf("请输入修改子相名称n");
    		scanf("%s",Item_Name);
    		memcpy(&m_Elem[iElem_Pos].m_Elem_str[iItem_Pos].item_name,&Item_Name,strlen(Item_Name));
    		m_Elem[iElem_Pos].m_Elem_str[iItem_Pos].item_name[strlen(Item_Name)] = '';
    		Working = true;
    		break;
    	case ModifyElem_Item_Value:
    		printf("请输入修改元素位置n");
    		iElem_Pos = InputVal();
    		if(iElem_Pos>=elem_count) {printf("元素不存在n");break;}
    		printf("请输入修改子相位置n");
    		iItem_Pos = InputVal();
    		if (iItem_Pos>=m_Elem[iElem_Pos].elem_count)	{printf("元素子相不存在n");break;}
    		printf("请输入修改子相值n");
    		scanf("%s",Item_Value);
    		memcpy(&m_Elem[iElem_Pos].m_Elem_str[iItem_Pos].item_str,&Item_Value,strlen(Item_Value));
    		m_Elem[iElem_Pos].m_Elem_str[iItem_Pos].item_str[strlen(Item_Value)] = '';
    		Working = true;
    		break;
    	case ModifyBack:
    		Working = false;
    		break;
    	default:
    		printf("------------n无效操作符!是否返回上一层!n0:继续;other:返回n------------n");
    		Working = GetWorking();
    		break;
    	}
    	return Working;
    }

查找:指定查找:元素名称查找,子相值查找,子相名称查找。模糊查找:.目前只实现全字匹配,大小写匹配

代码语言:javascript复制
/*查找*/
bool Cmd_Search(int iSearch)
{
	char * Search_str = new char[Item_Name_Max];
	bool bFlag = false;
	bool Working = true;
	switch(iSearch)
	{
	case SearchElem:
		printf("请输入需要查找元素名称n");
		scanf("%s",Search_str);
		Search_str[strlen(Search_str)] = '';
		while(Search_str[0] != '[' || Search_str[strlen(Search_str)-1] != ']')
		{
			printf("请重新输入需要查找元素名称n");
			scanf("%s",Search_str);
			Search_str[strlen(Search_str)] = '';
		}
		
		for (int i = 0 ;i<elem_count;i  )
		{
			if(strcmp(m_Elem[i].elem_name,Search_str)==0)
			{
				bFlag = true;
				printf("元素位置:%dn元素名称:%sn",i,m_Elem[i].elem_name);
				for(int j = 0;j<m_Elem[i].elem_count;j  )
				{
					printf("Key:%s Value:%sn",m_Elem[i].m_Elem_str[j].item_name,m_Elem[i].m_Elem_str[j].item_str);
				}	
			}
		}
		if (bFlag==false) printf("不存在此元素n");
		Working = true;
		break;
	case SearchElem_Item_Name:
		printf("请输入需要查找子相名称n");
		scanf("%s",Search_str);
		Search_str[strlen(Search_str)] = '';
		for (int i = 0 ;i<elem_count;i  )
		{
			for(int j = 0;j<m_Elem[i].elem_count;j  )
			{
				if(strcmp(m_Elem[i].m_Elem_str[j].item_name,Search_str)==0)
				{
					bFlag = true;
					printf("元素位置:%d,元素名称:%sn",i,m_Elem[i].elem_name);
					printf("Key:%s Value:%sn",m_Elem[i].m_Elem_str[j].item_name,m_Elem[i].m_Elem_str[j].item_str);
				}
			}	
		}
		if (bFlag==false) printf("不存在此元素n");
		Working = true;
		break;
	case SearchElem_Item_value:
		printf("请输入需要查找子相值n");
		scanf("%s",Search_str);
		Search_str[strlen(Search_str)] = '';
		for (int i = 0 ;i<elem_count;i  )
		{
			for(int j = 0;j<m_Elem[i].elem_count;j  )
			{
				if(strcmp(m_Elem[i].m_Elem_str[j].item_str,Search_str)==0)
				{
					bFlag = true;
					printf("元素位置:%d,元素名称:%sn",i,m_Elem[i].elem_name);
					printf("Key:%s Value:%sn",m_Elem[i].m_Elem_str[j].item_name,m_Elem[i].m_Elem_str[j].item_str);
				}
			}	
		}
		if (bFlag==false) printf("不存在此元素n");
		Working = true;
		break;
	case SearchWord:
		printf("请输入搜索值n");
		scanf("%s",Search_str);
		Search_str[strlen(Search_str)] = '';
		if(Search_str[0] == '[' && Search_str[strlen(Search_str)-1] == ']')
		{
			for (int i = 0 ;i<elem_count;i  )
			{
				if(strcmp(m_Elem[i].elem_name,Search_str)==0)
				{
					bFlag = true;
					printf("元素名称相同,元素位置:%dn元素名称:%sn",i,m_Elem[i].elem_name);
					for(int j = 0;j<m_Elem[i].elem_count;j  )
					{
						printf("Key:%s Value:%sn",m_Elem[i].m_Elem_str[j].item_name,m_Elem[i].m_Elem_str[j].item_str);
					}	
				}
			}
		}else
		{
			for (int i = 0 ;i<elem_count;i  )
			{
				for(int j = 0;j<m_Elem[i].elem_count;j  )
				{
					if(strcmp(m_Elem[i].m_Elem_str[j].item_name,Search_str)==0)
					{
						bFlag = true;
						printf("元素子相名称相同,元素位置:%d,元素名称:%sn",i,m_Elem[i].elem_name);
						printf("Key:%s Value:%sn",m_Elem[i].m_Elem_str[j].item_name,m_Elem[i].m_Elem_str[j].item_str);
					}
					if(strcmp(m_Elem[i].m_Elem_str[j].item_str,Search_str)==0)
					{
						bFlag = true;
						printf("元素子相值相同,元素位置:%d,元素名称:%sn",i,m_Elem[i].elem_name);
						printf("Key:%s Value:%sn",m_Elem[i].m_Elem_str[j].item_name,m_Elem[i].m_Elem_str[j].item_str);
					}
				}	
			}
		}
		if (bFlag==false) printf("不存在此元素n");
		Working = true;
		break;
	case SearchBack:
		Working = false;
		break;
	default:
		printf("------------n无效操作符!是否返回上一层!n0:继续;other:返回n------------n");
		Working = GetWorking();
		break;
	}
	delete Search_str;
	Search_str = NULL;
	return Working;
}

文件写入:只有做文件写入操作时,才会实现对文本内容的修改,其他操作只是对内存操作,对应保存功能。文件写入是将容器内容直接写入文本。

代码语言:javascript复制
/*写入文件*/
bool File_Write()
{
	bool state = false;
	fclose(file);
	file  = fopen(filename,"w ");
	for (int i = 0 ;i<elem_count;i  )
	{			
		fprintf(file,"%sn",m_Elem[i].elem_name);
		for(int j = 0;j<m_Elem[i].elem_count;j  )
		{
			fprintf(file,"%s=%sn",m_Elem[i].m_Elem_str[j].item_name,m_Elem[i].m_Elem_str[j].item_str);
			if(i==elem_count-1 && j==m_Elem[i].elem_count-1) state = true;
		}
		
	}
	fclose(file);
	return state;
}

6.辅助函数 字符串输入检测,兼容误输

代码语言:javascript复制
/*输入字符转换,兼容误输*/
int InputVal()
{
	int outputval =0;
	char * input = new char[Int_Len];
	unsigned int initPos = 0;	
	scanf("%s",input);
	int len =  strlen(input);
	if(input[0] == ' ' || input[0] =='-') {initPos =1;len = len-1;} else initPos = 0;
	for (int i = initPos;i<strlen(input);i  )
	{
		if(input[i]>='0'&&input[i]<='9') outputval = outputval*10 int(input[i])-0x30;
		else return 0;
	}
	if(input[0] == '-') outputval = 0;
	delete input;input = NULL;
	return outputval;
}

/*Cmd检测*/
int CmdVal()
{
	char * input = new char[Int_Len];
	int iOutput = 0;
	scanf("%s",input);
	int len =  strlen(input);
	if(input[0]>='0'&& input[0]<='8' && len ==1) iOutput = input[0]-0x30;
	else iOutput = -1;	
	delete input;input = NULL;
	return iOutput;
}

工作状态获取

代码语言:javascript复制
bool GetWorking()
{
	char * input = new char[2];	
	scanf("%s",input);
	int len =  strlen(input);
	if(input[0]=='0' && len ==1) {delete input;input =NULL;return true;}
	else {delete input;input =NULL;return false;}
}

其他:字符串补位,计划实现功能,对元素名称输入时,实现对元素名称的检测。

代码语言:javascript复制
/*字符串补位,第一位填'[',最后一位填']'*/
void str_Move(char * str,int len,bool flag)
{
	if (flag)
	{		
		str[len]= '';str[len 1] = '';
		len   ;
		for(int i = len-1;i>0;i--)
		{
			str[i] = str[i-1];
		}
		str[0] = '[';
	}else
	{
		len   ;
		str[len-1] = ']';
		str[len] = '';
	}
}
/*文件名称检测*/
void ElemName_Modify(char* name,int len)
{
	if (name[0]!='[')
	{
		str_Move(name,len,true);
	}else if(name[len-1] != ']')
	{
		str_Move(name,len,false);
	}
}


bool ElemName_Check(char * name,int len)
{
	if(name[0] == '[' && name[len-1] == ']') return true;
	else return false;
}

/*int 输入字符越界判断*/
bool IntputCheck(int intputVal)
{
	if (intputVal>MAX_INPUT || intputVal<MIN_INPUT )	return false;
	return true;
}

源码:

源码

0 人点赞