从文件中读取后没有获得所有字符

#include #include int main() { int count=0; char c=0; printf("Reading this Source file\nFile name is: %s\n",__FILE__); FILE *myFile=fopen(__FILE__,"r"); if(myFile) printf("File Successfully Opened\n"); do{ c=fgetc(myFile); count++; } while(c!=EOF); printf("%s contains %d characters\n",__FILE__,count); char *p=(char*)malloc(count+1); if(p==NULL) printf("Malloc Fail\n"); else { fseek(myFile,0,SEEK_SET); printf("\nMalloc succeeded - You have %d Bytes of Memory\n",count+1); fgets(p,count,myFile); printf("The Entire Source Code is\n---------------------------\n"); int i=0; while(i<count) printf("%c",*(p+i++)); } free(p); fclose(myFile); return 0; } 

在上面的程序中,我一直只能得到以下字符:

 #include 

那是我的输出是:

 Reading this Source file File name is: main.c File Successfully Opened main.c contains 704 characters Malloc succeeded - You have 705 Bytes of Memory The Entire Source Code is #include 

为什么输出中没有显示整个文件内容?

因为fgets在换fgets停止。

 fgets(p,count,myFile); /* Stops when it reaches `count` OR at newline. */ 

编辑

使用fread或使用第一个循环(带有fgetc循环)来存储字符并随时展开p