Linux读取文件的哦内存
这里是一个很好的读取文件到内存的例子!
#include <stdio.h>
#include <stdlib.h>
void main()
{
FILE *fp;
long int size;
char * buff;
if ((fp=fopen("in.db","rb"))==NULL){
printf("cannot open file\n");
exit(1);
}
fseek(fp,0,SEEK_END);
size=ftell(fp);
buff=(char *) malloc(size);
fseek(fp,0,SEEK_SET);
if (fread(buff,1,size,fp)!=size) {
printf("read error!!");
}
free(buff);
}
#include <stdlib.h>
void main()
{
FILE *fp;
long int size;
char * buff;
if ((fp=fopen("in.db","rb"))==NULL){
printf("cannot open file\n");
exit(1);
}
fseek(fp,0,SEEK_END);
size=ftell(fp);
buff=(char *) malloc(size);
fseek(fp,0,SEEK_SET);
if (fread(buff,1,size,fp)!=size) {
printf("read error!!");
}
free(buff);
}