代码如下:
pFile = fopen(buf,"r"); if (pFile==NULL)
{
fputs ("File error",stderr);
exit (1);
} // obtain file size:
fseek (pFile , 0 , SEEK_END);
lSize = ftell (pFile);
rewind (pFile); // allocate memory to contain the whole file:
buffer = (char*) malloc (sizeof(char)*lSize);
memset(buffer,0,(sizeof(char)*lSize));
if (buffer == NULL)
{
fputs ("Memory error",stderr);
exit (2);
} // copy the file into the buffer:
result = fread (buffer,sizeof(char),lSize,pFile); if (result != lSize)
{
fputs ("Reading error",stderr);
exit (3);
} /* the whole file is now loaded in the memory buffer. */ // terminate
fclose (pFile);
free (buffer);
}
我打开了一个.C文件,长度获得为lSize,然后把文件的数据读到内存,但是使用fread返回的长度比lSize小,只是放入buffer的数据并不是完整的.C文件的数据,少了最后一段。不知道是为什么,请指教。
pFile = fopen(buf,"r"); if (pFile==NULL)
{
fputs ("File error",stderr);
exit (1);
} // obtain file size:
fseek (pFile , 0 , SEEK_END);
lSize = ftell (pFile);
rewind (pFile); // allocate memory to contain the whole file:
buffer = (char*) malloc (sizeof(char)*lSize);
memset(buffer,0,(sizeof(char)*lSize));
if (buffer == NULL)
{
fputs ("Memory error",stderr);
exit (2);
} // copy the file into the buffer:
result = fread (buffer,sizeof(char),lSize,pFile); if (result != lSize)
{
fputs ("Reading error",stderr);
exit (3);
} /* the whole file is now loaded in the memory buffer. */ // terminate
fclose (pFile);
free (buffer);
}
我打开了一个.C文件,长度获得为lSize,然后把文件的数据读到内存,但是使用fread返回的长度比lSize小,只是放入buffer的数据并不是完整的.C文件的数据,少了最后一段。不知道是为什么,请指教。
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货