项目案例-读写配置文件(C提高)

//FILE *fopen(char *filename,char *type);

//int fclose(FILE *stream);

//int fcloseall(void);

//int fget(FILE *stream);

//int fput(int ch , FILE *stream);   stream = stdout --> 打到屏幕

//putc()与fputc()等价,getc()与fgetc()等价

//putchar(c)相当于fputc(c,stdout);

//getchar()相当于fgetc(stdin);

//feof()函数来测试是否到了文件尾

//ferror()函数来测试是否出错

//gets()-->遇到换行符和文件结束标示,则结束

//char * fputs(char * str, len , FILE *stream);

//fprintf(FILE * stream,"格式",数据)向流指针指向的文件输出-->文件相当于屏幕

//fscanf()从文件中读到指定的数组

//getch()从控制台读取一个字符,并且不回显在屏幕上

//int fflush(FILE *stream);

//int flushall();

//设置文件缓冲区函数

//void setbuf(FILE *stream,char *buf);

//void setbuf(FILE *stream,char *buf,int type,unsigned size);

//type:

//      ---> _IOFBF  文件全部缓冲区,即缓冲区装满后,才能堆文件读写

//      ---> _IOLBF  文件行缓冲,即缓冲区接收到一个换行符时,才能对文件读写

//      ---> _IONBF  文件不缓冲

//移动文件指针的函数:

//  long ftell(FILE *stream);   -->得到文件指针离开文件开头的偏移量,返回-1时表示出错

//  int  rewind(FILE *stream);  -->用于文件指针移到文件开头,成功返回0,不成功非0

//  fseek(FILE *stream,long offset,int origin); -->用于把文件指针以origin为起点移动offset个字节

//  origin:

//          SEEK_SET    0       文件开头

//          SEEK_CUR    1       文件指针当前位置

//          SEEK_END    2       文件尾   

//int fread(void *ptr,int nitems,FILE *stream);

//int fwrite(void *ptr,int size,int nitems,FILE *stream);

//非标准文件的读写  <io.h>

//int open(char *filename , int access);

//access:

//      O_RDONLY    -->只读

//      O_WRONLY    -->只写

//      O_RDWR      -->读写

//      O_BINARY    -->打开一个二进制文件

//      O_TEXT      -->打开一个文字文件

//      O_APPEND    -->文件指针指向末尾

//      O_CREAT     -->文件不存在时创建文件,属性按基本模式属性

//      O_TRUNC     -->若文件存在,将其长度缩为0,属性不变

// 

//int close(int handle);

//读写函数

//int read(int handle,void *buf,int count);  

//              -->从handle相连的文件中,读取count个字节放到buf所指的缓冲区中

//              -->返回值:-1表示出错,返回0表示文件结束

//int write(int handle,void *buf,int count);

//              -->把count个字节从buf指向的缓冲区写入与handle相连的文件中

//              -->返回实际写入的字节数

//int lseek(int handle,long offset,int fromwhere);

//long tell(int handle);


#include <iostream>
#include <string.h>

using namespace std;

#define		MAXSIZE			1024
#define		FILENAME		"F:/config/config1.txt"


int  WriteConfig(const char *filename,char *key_buf,char *valude_buf)
{
	int  ret = 0;
	int  len = 0;
	int  flag = 0;
	char filebuf[MAXSIZE * 8];
	char linebuf[MAXSIZE];
	char *ptmp = NULL;
	FILE *fp;

	if (filename == NULL || key_buf == NULL || valude_buf == NULL)
	{
		ret = -1;
		printf("filename == NULL || key_buf == NULL || valude_buf == NULL:");
		return ret;
	}
	fp = fopen(filename,"w+t");	//没有该问价则创建一个
	if (fp == NULL)
	{
		ret = -2;
		printf("fopen(filename) error:");
		return ret;
	}
	//判断文件的是否满了
	fseek(fp,0L,SEEK_END);
	len = ftell(fp);
	if (len >= MAXSIZE)
	{
		ret = -3;
		printf("file will full error:len >= MAXSIZ:");
		return ret;
	}
	rewind(fp);		//回到文件头部
	
	//将每行数据取出来,放到linefile-->找key
	while (!feof(fp))
	{
		memset(linebuf,0,sizeof(linebuf));
		fgets(linebuf,MAXSIZE,fp);
		//判断改行有没有key
		ptmp = strstr(linebuf, key_buf);
		if (ptmp == NULL)    //如果没有-->把改行内容暂存在filebuf
		{
			strcat(filebuf,linebuf);
			continue;
		}
		else				//如果存在-->重写linebuf,然后再缓存到filebuf
		{
			sprintf(linebuf,"%s=%s\n",key_buf,valude_buf);
			strcat(filebuf,linebuf);
			flag = 1;
			continue;
		}
	}

	if (flag == 0)		//不存在key-->将key和valude写到文件末尾
	{
		char tmp[MAXSIZE];
		sprintf(tmp,"%s=%s\n",key_buf,valude_buf);
		ret = fputs(tmp,fp);
		if (ret == EOF)
		{
			ret = -5;
			printf("ret = fputs(tmp,fp):");
			return ret;
		}
	}
	else				//如果存在-->将filebuf写到文件中
	{
		fseek(fp,SEEK_END,SEEK_SET);
		fputs(filebuf, fp);
	}
	fclose(fp);
	return ret;
}
void trimspace(char *srcstr, char *outstr)
{
	//printf("%s\n",srcstr);
	int left = 0;
	int right = strlen(srcstr) - 2;		//有 ‘\n’

	while (isspace(srcstr[left]))
	{
		left++;
	}
	while (isspace(srcstr[right]))
	{
		right--;
	}
	
		memset(outstr,0,sizeof(outstr));
		strncpy(outstr,srcstr+left,right-left+1);
		outstr[right - left+1] = '\0';
	
	
	return;
}

void ToWriteConfig()			//写配置文件
{
	int ret = 0;
	char key_buf[MAXSIZE];
	char valude_buf[MAXSIZE];

	printf("请输入key: ");
	scanf("%s",&key_buf);
	printf("\n请输入valude: ");
	scanf("%s", &valude_buf);

	ret = WriteConfig(FILENAME,key_buf,valude_buf);
	if (ret != 0)
	{
		printf("WriteConfig() is error:%d",ret);
	}

	return;
}
int ReadConfig(const char *filename , char *key_buf , char *valude_buf,int *valude_len)
{
	int ret = 0;
	char linebuf[MAXSIZE];
	char *ptmp = NULL;
	FILE *fp;

	if (filename == NULL || key_buf == NULL || valude_buf == NULL || valude_len == NULL)
	{
		ret = -1;
		printf("ilename == NULL || key_buf == NULL || valude_buf == NULL || valude_len == NULL");
		return ret;
	}

	fp = fopen(filename , "r");
	if (fp == NULL)
	{
		ret = -2;
		printf("fopen() error");
		return ret;
	}

	while (!feof(fp))
	{
		memset(linebuf,0,sizeof(linebuf));
		fgets(linebuf,MAXSIZE,fp);
		//printf("%s\n", linebuf);
		//寻找等号
		ptmp = strchr(linebuf,'=');
		if (ptmp == NULL)
		{
			continue;
		}

		//寻找key
		ptmp = strstr(linebuf,key_buf);
		if (ptmp == NULL)
		{
			continue;
		}
		ptmp = ptmp + strlen(key_buf);
		//寻找 = 号
		ptmp = strchr(ptmp,'=');
		ptmp += 1;
		
		//去掉空格
		trimspace(ptmp,valude_buf);
		*valude_len = strlen(valude_buf);
	}
	fclose(fp);
	return ret;
}
void ToReadConfig()				//读配置文件
{
	int  ret = 0;
	int  valude_len = 0;
	char key_buf[MAXSIZE];
	char valude_buf[MAXSIZE];
	
	printf("请你输入key:");
	scanf("%s",&key_buf);
	ret = ReadConfig(FILENAME,key_buf,valude_buf,&valude_len);
	if (ret != 0)
	{
		printf(" ReadConfig() error:%d\n",ret);
	}

	printf("%s		",valude_buf);
	printf("%d\n", valude_len);

	return;
}
void menu()
{
	printf("==================================\n");
	printf("===           1.写文件         ===\n");
	printf("===           2.读文件         ===\n");
	printf("===           0.退出           ===\n");
	printf("==================================\n");
}

int main()
{
	int choice;

	while (1)
	{
		menu();
		scanf("%d",&choice);
		switch (choice)
		{
			case 1:		//写配置文件
				ToWriteConfig();
				break;
			case 2:		//读配置文件
				ToReadConfig();
				break;
			case 0:
				exit(0);
		default:
			break;
		}

	}


	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

@陈一言

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值