C函数读写配置文件

配置文件的格式为:key=value,中间没有空格的。

函数中,key的长度为10,value的长度为100,没进行溢出的判断。

下面为C文件。


#include
#include
#include

void DeleteFile();
int WrtCfgIterm(char *pfile/*in*/, char *key/*in*/, char *valude/*in*/);
int GetCfgIterm();
void interface();
int CreateFile();


int main()
{
		interface();
		return 0;
}

//interface
void interface()
{
		int SelectNum = 0;
		char FileName[100];//文件名字
		char NewFileName[100];//新文件名字
		char Key[10];
		char valude[100];
		char N = '\0';//接收判断结果
		FileName[0] = '\0';
		NewFileName[0] = '\0';
		Key[0] = '\0';
		valude[0] = '\0';
		while(1)
		{
		printf("1. 创建配置文件\n");
		printf("2. 读配置文件\n");
		printf("3. 写配置文件\n");
		printf("4. 删除配置文件\n");
		printf("5. 退出\n");

		scanf("%d", &SelectNum);
		
		switch(SelectNum)
		{
				case 1:{
							   printf("请输入文件名称:\n");
							   scanf("%s", FileName);
							   if(CreateFile(FileName))
									   printf("create successful!\n");
					   }break;
				case 2:{
							   if(FileName[0] != '\0')
							   {
							   		printf("操作文件为:%s? Y/N \n", FileName);
									scanf("%s", &N);
									if(N == 'Y' || N == 'y')
									{
											printf("请输入查询的KEY:\n");
											scanf("%s", Key);
											if(	GetCfgIterm(FileName/*in*/, Key/*in*/, valude/*out*/))
															printf("value:%s\n", valude);
											else
															printf("do not get Iterm\n");
									}
									else
									{
									   printf("请输入打开文件名称:\n");
									   scanf("%s", NewFileName);
									   printf("请输入查询的KEY:\n");
									   scanf("%s", Key);
									   printf("file name : %s\n", NewFileName);
									   if(GetCfgIterm(NewFileName/*in*/, Key/*in*/, valude/*out*/))
											   printf("value:%s\n", valude);
									   else
											   printf("do not get Iterm\n");
									}
							   }
							   else
							   {
									   printf("请输入文件名称:\n");
									   scanf("%s", NewFileName);
									   printf("请输入查询的KEY:\n");
									   scanf("%s", Key);
									   printf("file name : %s\n", NewFileName);
									   if(GetCfgIterm(NewFileName/*in*/, Key/*in*/, valude/*out*/))
											   printf("value:%s\n", valude);
									   else
											   printf("do not get Iterm\n");
							   }

					   }break;
				case 3:{
							   if(FileName[0] != '\0')
							   {
							   		printf("操作文件为:%s? Y/N \n", FileName);
									scanf("%s", &N);
									if(N == 'Y' || N == 'y')
									{
											printf("请输入写入的KEY:\n");
											scanf("%s", Key);
											printf("请输入写入的Value:\n");
											scanf("%s", valude);
											if(WrtCfgIterm(FileName/*in*/, Key/*in*/, valude/*in*/))
													printf("value=%s\n", valude);
											else
													printf("write failed!\n");
									}
									else
									{
									   printf("请输入打开文件名称:\n");
									   scanf("%s", NewFileName);
									   printf("请输入写入的KEY:\n");
									   scanf("%s", Key);
									   printf("请输入写入的Value:\n");
									   scanf("%s", valude);

									   if(WrtCfgIterm(NewFileName/*in*/, Key/*in*/, valude/*in*/))
											   printf("value=%s\n", valude);
									   else
											   printf("write failed!\n");

									}
							   }
							   else
							   {
									   printf("请输入文件名称:\n");
									   scanf("%s", NewFileName);
									   printf("请输入写入的KEY:\n");
									   scanf("%s", Key);
									   printf("请输入写入的Value:\n");
									   scanf("%s", valude);

									   if(WrtCfgIterm(NewFileName/*in*/, Key/*in*/, valude/*in*/))
											   printf("\nvalue=%s\n", valude);
									   else
											   printf("write failed!\n");
							   }


					   }break;
				case 4:{
							   if(FileName[0] != '\0')
							   {
							   		printf("操作文件为:%s? Y/N \n", FileName);
									scanf("%s", &N);
									if(N == 'Y' || N == 'y')
									{
									   DeleteFile(FileName);
									}
							   }
							   else
							   {
									   printf("请输入文件名称:\n");
									   scanf("%s", NewFileName);
									   DeleteFile(NewFileName);
							   }

							
					   }break;
				case 5:{
							   exit(0);
					   }break;

		}
		}
}

//facution

int CreateFile(char *FileName)/*创建文件*/
{
		FILE *fp;
		if((fp = fopen(FileName, "w")) == NULL)
		{
				printf("create new file failed!\n");
				exit(0);
		}
		fclose(fp);
		return 1;
}
void DeleteFile(char *FileName)/*删除文件*/
{
		if(remove(FileName))
		{
				printf("Could not delete the file %s \n",FileName);
		}
		else
		{
				printf("Delete file successful! \n");
		}
}

int GetCfgIterm(char *pfile/*in*/, char *key/*in*/, char *valude/*out*/)/*读配置文件*/
{
		int i = 0, k = 0, j = 0;
		FILE *fp;
		char *p;
		char LineKey[10];
		char LineIterm[100];
		fp = fopen(pfile, "r");
		if(fp == NULL)
		{
				printf("open file failed!\n");
				fclose(fp);
				exit(0);
		}
		else
				printf("open file successful!\n");
		for(i = 0; ; i++)//循环读取每行
		{
				if((fgets(LineIterm, 100, fp)) == NULL)//读取每行的字符串
				{
						fclose(fp);
						break;
				}
				p = LineIterm;//利用p移动得到LineTterm字符串每个字符。LineIterm不能移动。
				k = 0;
				while(*p != '=')//得到每行字符串中的key的字符串
				{
						LineKey[k] = *p;
						k++;
						p++;
				}
				LineKey[k] = '\0';//key的字符串末尾补\0
				if(strcmp(LineKey, key) == 0)//判断每行的key和形参的key是否相等
				{
						p++;//越过字符‘=’
						j = 0;
						while(*p != '\0')//从每行字符串中得到valude
						{
								valude[j] = *p;
								j++;
								p++;
						}
						valude[j] = '\0';//value字符串末尾补‘\0’
						fclose(fp);
						return 1;
				}
		}
		fclose(fp);
		return 0;
}

int WrtCfgIterm(char *pfile/*in*/, char *key/*in*/, char *valude/*in*/)/*写配置文件*/
{
		int i = 0, k = 0, j = 0;
		FILE *fp;
		char LineIterm[100];
		fp = fopen(pfile, "a");
		if(fp == NULL)
		{
				printf("open file failed!\n");
				fclose(fp);
				exit(0);
		}
		k = 0;
		while(key[k] != '\0')//形参的key付给行字符串的前面。
		{
				LineIterm[k] = key[k];
				k++;
		}
		LineIterm[k] = '=';//行字符串后面补上’=‘
		k++;
		j = 0;
		while(valude[j] != '\0')//形参的value赋值给行字符串的后面。
		{
				LineIterm[k] = valude[j];
				j++;
				k++;
		}
		LineIterm[k] = '\n';//行字符串补全换行符
		k++;
		LineIterm[k] = '\0';//行字符串末尾补’\0‘
		if(fputs(LineIterm, fp) > 0)
				printf("wtrite successful!\n");
		fclose(fp);
		return 1;
}


你可能感兴趣的:(c)