Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/64.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
从文件读取后未获取所有字符 #包括 #包括 int main() { 整数计数=0; 字符c=0; printf(“读取此源文件\n文件名为:%s\n”,\uuuu文件\uuuu); FILE*myFile=fopen(_FILE__,“r”); 如果(我的文件) printf(“文件已成功打开\n”); 做{ c=fgetc(myFile); 计数++; }而(c!=EOF); printf(“%s包含%d个字符\n”,\u文件\u,计数); char*p=(char*)malloc(计数+1); if(p==NULL) printf(“Malloc Fail\n”); 其他的 { fseek(myFile,0,SEEK_SET); printf(“\n分配成功-您有%d字节的内存\n”,计数+1); fgets(p,count,myFile); printf(“整个源代码是\n------------------------------------\n”); int i=0; 而(i_C - Fatal编程技术网

从文件读取后未获取所有字符 #包括 #包括 int main() { 整数计数=0; 字符c=0; printf(“读取此源文件\n文件名为:%s\n”,\uuuu文件\uuuu); FILE*myFile=fopen(_FILE__,“r”); 如果(我的文件) printf(“文件已成功打开\n”); 做{ c=fgetc(myFile); 计数++; }而(c!=EOF); printf(“%s包含%d个字符\n”,\u文件\u,计数); char*p=(char*)malloc(计数+1); if(p==NULL) printf(“Malloc Fail\n”); 其他的 { fseek(myFile,0,SEEK_SET); printf(“\n分配成功-您有%d字节的内存\n”,计数+1); fgets(p,count,myFile); printf(“整个源代码是\n------------------------------------\n”); int i=0; 而(i

从文件读取后未获取所有字符 #包括 #包括 int main() { 整数计数=0; 字符c=0; printf(“读取此源文件\n文件名为:%s\n”,\uuuu文件\uuuu); FILE*myFile=fopen(_FILE__,“r”); 如果(我的文件) printf(“文件已成功打开\n”); 做{ c=fgetc(myFile); 计数++; }而(c!=EOF); printf(“%s包含%d个字符\n”,\u文件\u,计数); char*p=(char*)malloc(计数+1); if(p==NULL) printf(“Malloc Fail\n”); 其他的 { fseek(myFile,0,SEEK_SET); printf(“\n分配成功-您有%d字节的内存\n”,计数+1); fgets(p,count,myFile); printf(“整个源代码是\n------------------------------------\n”); int i=0; 而(i,c,C,是因为fgets停在换行符处 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<stdio.h> 编辑 使用fread代替,或者使用第一个循环(带有fgetc的循环

是因为
fgets
停在换行符处

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<stdio.h>
编辑
使用
fread
代替,或者使用第一个循环(带有
fgetc
的循环)来存储字符,并在运行时展开
p

因为
fgets
在换行时停止

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<stdio.h>
编辑
改用
fread
或使用第一个循环(带有
fgetc
)存储字符并在运行时展开
p

您从不检查
fgets
错误。您从不检查
fgets
错误。可能值得一提的是
fread
或干脆
fgetc
将是正确的方法。
fead
或干脆
fgetc>可能值得一提
将是正确的方法。。
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<stdio.h>
fgets(p,count,myFile); /* Stops when it reaches `count` OR at newline. */