我正在尝试读取文件,并以百分比显示读取进度。我已经尝试了以下代码,但无法分成多个部分显示进度。我应该如何显示进度?
printf("File contains %ld bytes!\n", fsize);
buf = (char*) malloc (sizeof(char)*fsize);
if (buf == NULL) {
printf("Memory error!\n");
return 2;
}
else {
printf("Allocating memory!\n");
bytes_read = fread(buf, 1, fsize, pf);
printf("number of bytes read %1d",bytes_read);
这是一个粗略的草稿,以说明替换fread()
当前使用的草稿:
size_t numToRead = fsize;
char* bufPtr = &buf[0];
while ( numToRead > 0 )
{
int percentDone = 0;
size_t numSuccessful = fread( bufPtr, 1, 10000, pf ); // choose size
bufPtr += numSuccessful;
numToRead -= numSuccessful;
percentDone = ( 100 * ( fsize - numToRead ) / fsize );
// display percentage as you like
}
免责声明:我尚未编写此代码段,因此请原谅错别字。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句