#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>
#include <unistd.h>
char *GetFileContent(const char *psFileName)
{
int pf = open(psFileName, O_RDONLY);
if( 0 == pf)
{
printf("open file fail.\r\n");
return NULL;
}
int nFileLen = lseek(pf, 0, SEEK_END);
lseek(pf, 0, SEEK_SET);
char *psFileContent = (char *)malloc(nFileLen + 1);
memset(psFileContent, 0, nFileLen + 1);
if(NULL == psFileContent)
{
printf("malloc fail.\r\n");
close(pf);
return NULL;
}
read(pf, psFileContent, nFileLen);
close(pf);
return psFileContent;
}
int main(void) {
char *psContent = GetFileContent("/root/xxx.log");
printf("%s", psContent);
return EXIT_SUCCESS;
}
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>
#include <unistd.h>
char *GetFileContent(const char *psFileName)
{
int pf = open(psFileName, O_RDONLY);
if( 0 == pf)
{
printf("open file fail.\r\n");
return NULL;
}
int nFileLen = lseek(pf, 0, SEEK_END);
lseek(pf, 0, SEEK_SET);
char *psFileContent = (char *)malloc(nFileLen + 1);
memset(psFileContent, 0, nFileLen + 1);
if(NULL == psFileContent)
{
printf("malloc fail.\r\n");
close(pf);
return NULL;
}
read(pf, psFileContent, nFileLen);
close(pf);
return psFileContent;
}
int main(void) {
char *psContent = GetFileContent("/root/xxx.log");
printf("%s", psContent);
return EXIT_SUCCESS;
}