文件到buffer
char* from_file_to_buf(char *file_path)
{
FILE *f = NULL;
unsigned long len = 0;
char prefix_buf[64] = {0};
int add_len = 0;
int f_len = 0;
char *data = NULL;
f=fopen(file_path, "rb");
if (f == NULL)
{
return NULL;
}
fseek(f, 0, SEEK_END);
len=ftell(f);
fseek(f, 0, SEEK_SET);
data=(char*)malloc(len + 512);
if (data == NULL)
{
fclose(f);
return NULL;
}
f_len = fread(data,1,len,f);
if(f_len <= 0)
{
fclose(f);
free(data);
data = NULL;
return NULL;
}
fclose(f);
return data;
}
buffer到文件
int from_buf_to_file(char *file_path, char *file_content)
{
FILE *f;
long len;
f=fopen(file_path,"w");
if(f == NULL)
{
printf("ERROR:%s\n", "Open file failed.");
return -1;
}
len = strlen(file_content);
file_content[len] = '\0';
fwrite(file_content,1,len,f);
fclose(f);
}