Im currently on a project where I try to store files into one (binary) file.
The files are stored block by block and a block looks like this:
struct Block_FS {
uint32_t ID;
char nutzdaten[_blockSize]; // blocksize is 4096, Nutzdaten means rawdata
};
So there is no issue with writing it into the file. That goes very cleanly and fast.
But when I try to read the original file out of the (binary) file it takes a long time (ca. 3-4 or more seconds for 10 blocks).
My readBlock
function looks like this:
Block_FS getBlock(long blockID, std::fstream & iso, long blockPosition, Superblock_FS superblock) {
iso.seekg(blockPosition);
for (long i=0; i<superblock.blocksCount; i++) { //blockscount is just all blocks that are currently stored
Block_FS block;
std::string line;
std::getline(iso, line); //read the id
lock.ID = stoi(line);
iso.read(&block.nutzdaten[0], superblock.blockSize); //read the raw data 4kb
getline(iso, line); //skipping. maybe I should use null terminating to avoid this getline()...?
if (block.ID == blockID) { //if the current block has the right id im returning the whole block.
return block;
}
}
std::cerr << "Unexpected behavior: Block -> " << blockID << " not found." << std::endl;
Block_FS block;
block.ID = -1;
return block;
}
I don't think that there's much more you need to see from my code.
Somewhere in this function must be something that takes a lot of time. Blocks are always stored on the disk and not in the cache. Only the one I'm currently writing or reading is in the cache.
Currently I'm kind of iterating through the file until I find the right id. I thought maybe that causes the time issue. But when I tried the other way by saving the position of the block with tellg()
and jump right to the point with seekg()
it still took the same amount of time to read blocks. Do you see anything I don't see?
question from:
https://stackoverflow.com/questions/65851198/c-takes-a-lot-of-time-when-reading-blocks-4kb-out-of-a-file-stdiosbina 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…