我的配置文件中是json格式的数据,直接读取进行打印:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
const static char* json_config_file = "./json_configfile.json";
static int read_config_file(char ** outbuff)
{
FILE* m_File = fopen(json_config_file, "r");
if(m_File == NULL)
{
printf("read file error.");
return -1;
}
fseek(m_File, 0, SEEK_END);
size_t size = ftell(m_File);
printf("config file name is :%s, file size is %lu", json_config_file, size);
char* filebuff = (char*) malloc((size +10)*sizeof(char));
memset(filebuff, 0, (size +10)*sizeof(char));
fseek(m_File, 0, SEEK_SET);
fread(filebuff, sizeof(const char), size, m_File);
fclose(m_File);
*outbuff = filebuff;
printf("config file is: %s \n", *outbuff);
return 0;
}
int main()
{
char * filebuff;
read_config_file(&filebuff);
printf("read buff is :[%s] \n", filebuff);
if(filebuff != NULL)
{
free(filebuff);
}
return 0;
}