tmighty
tmighty

Reputation: 11389

C++ Best way to quickly read from larger file

During runtime I frequently need to read small portions of larger file (300 mb). Currently I always open up the file, read from it and then close it again like that:

FILE *file =fopen(szFileName,"rb");
if (file)
{
    fseek( file,iFirstByteToRead, SEEK_SET);
    fread(nEncodedBytes,sizeof(unsigned char), iLenCompressedBytes, file);
    fclose(file);
}

But that is too slow because I do that so frequently. Also I am not sure if fread could be sped up.

What is the best practice for such a situation, please?

Upvotes: 5

Views: 546

Answers (2)

hyun
hyun

Reputation: 2143

The work related to file control always causes expensive cost. If you control several times for manage the file, you don’t have to close file pointer.

Like below, once you get the file pointer,

FILE *stream = fopen(stream, “rb”);

if (stream != NULL)

You have to close stream file pointer after finishing in your program, and also if you have to manage a number of files, you would use a thread, which is dedicated as a file reading.

Upvotes: 0

StilesCrisis
StilesCrisis

Reputation: 16290

Keep the file open and you'll do much better.

Try mmap for improved performance still.

Upvotes: 1

Related Questions