C语言实现读取文件所有内容到字符串
#include "stdio.h" #include "string" #include "stdlib.h" using namespace std; int main( void ) { FILE *fp; fp = fopen("05.爱,九把刀系列之那些年,我们一起追的女孩.txt" , "r"); fseek( fp , 0 , SEEK_END ); int file_size; file_size = ftell( fp ); printf( "%d" , file_size ); char *tmp; fseek( fp , 0 , SEEK_SET); tmp = (char *)malloc( file_size * sizeof( char ) ); fread( tmp , file_size , sizeof(char) , fp);//个人觉得第三个参数不对 printf("%s" , tmp ); return 0; }
摘自http://www.oschina.net/code/snippet_166355_9332#14930