The code is as follows, and the pointer to the dynamic memory allocated after execution is saved to result. Due to the dynamic allocation of memory, read the content is no longer used after the release of free, if not understand, please search the following dynamic memory allocation data.
#include <stdio.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <string.h>
#include <fcntl.h>
//Read all data from a file and save to result
//Parameter two: location of the file
//Return value: Successfully returned 1, failed to return 0
Int read_data (char **result,char *filename)
{
struct stat fileInfo;
FILE *filepointer;
Char *filedate;
if ((filename==null) | | (Result==null))
{
return 0;
if (! ( Filepointer=fopen (FileName, "RB")))
{return 0;}
stat (filename,&fileinfo);
filedate=malloc (sizeof (char) * (fileinfo.st_size+1));
fread (filedate,sizeof (char), fileinfo.st_size,filepointer);
fclose (filepointer);
filedate[fileinfo.st_size]=0;
printf ("\n%s\n", filedate);
*result=filedate;
return 1;
}
Linux C reads all data from any size file