问题的关键是如何得到文件的长度,进而利用它来定义一个字符串数组,同时还应在字符串数组后加上‘/0‘,否则会出现乱码。
FILE *pFile=fopen("1.txt","r");
char *pBuf;
fseek(pFile,0,SEEK_END); //找到文件末尾指针
int len =ftell(pFile); //获取文件长度
pBuf=new char[len+1];
fread(pBuf,1,len,pFile);
pBuf[len]='/0';
fclose(pFile);