I'd like to avoid unnecessary copies. I'm aiming for something along the lines of:
std::ifstream testFile( "testfile", "rb" );
std::vector<char> fileContents;
int fileSize = getFileSize( testFile );
fileContents.reserve( fileSize );
testFile.read( &fileContents[0], fileSize );
(which doesn't work because reserve
doesn't actually insert anything into the vector, so I can't access [0]
).
Of course, std::vector<char> fileContents(fileSize)
works, but there is an overhead of initializing all elements (fileSize
can be rather big). Same for resize()
.
This question is not so much about how important that overhead would be. Rather, I'm just curious to know if there's another way.