C语言文件操作-文件读写

学习了文件的一些操作,使用文件操作来实现简易的文件的读取和写入功能。

功能:写入名称和对应的属性,根据名称查询出对应的属性

1.文件读取

int file_read(char *name){
	char linebuf[1024];//放文件的一行的值
	char *check;//测试有没有  1.“=” ;2.对应名称
	file = fopen(filename, "r");
	if (file == NULL){
		return 0;	//没有内容
	}
	while (!feof(file)){
		memset(linebuf, 0, sizeof(linebuf));
		fgets(linebuf, 1024, file);
		//printf("%s", linebuf);

		check=strchr(linebuf, '=');
		if (check == NULL){//没有=,继续下一行
			continue;
		}
		check = strstr(linebuf, name);
		if (check == NULL)//没有对应的名称
		{
			continue;
		}
		check = strstr(check, "=");
		check += 1;//移动指针位置到属性值

		printf("value:%s", check);
	}
	fclose(file);
	return 1;
}

2.写内容

int file_write(char*name, char*value){
		file = fopen(filename, "a+");
		fprintf(file, "%s=%s\n", name, value);
		fclose(file);
	return 1;
}
发布了15 篇原创文章 · 获赞 15 · 访问量 968

猜你喜欢

转载自blog.csdn.net/weixin_42204569/article/details/102846231
今日推荐