c - I am trying to read a file and to show the reading progress in %. I have tried following code but I am not able to divide in chunks & show progress -
i trying read file , show reading progress in percentage. have tried out following code not able divide in chunks , show progress. how should show progress?
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);
here's rough draft, illustrate replacing 1 fread()
use:
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 }
disclaimer: have not compiled snippet, please forgive typos.
Comments
Post a Comment