I'm trying to load a .wav file but it takes forever. The code is use:
std::ifstream file (filePath, std::ios::binary);
// check if the file exists
if (! file.good())
{
reportError ("ERROR: File doesn't exist or otherwise can't load file\n" + filePath);
return false;
}
file.unsetf (std::ios::skipws);
std::istream_iterator<uint8_t> begin (file), end;
std::vector<uint8_t> fileData (begin, end);
As I said, it takes forever to load a 27MB file. But I have seen a few posts online that claim to have loaded a 106MB file in around 1400ms by using getline()
.
I know that ifstream::read()
can load data faster, but then I wouldn't get the data as uint8_t back.
I haven't really worked much with files so it would be nice if somebody could explain how i could read the data fast and get it as uint8_t. Any help regarding this would be appreciated.
EDIT:
I now reserve space for the bytes, but it is still slow:
std::ifstream file(filePath, std::ios::binary);
auto size = std::filesystem::file_size(filePath);
// check if the file exists
if (!file.good())
{
reportError("ERROR: File doesn't exist or otherwise can't load file\n" + filePath);
return false;
}
file.unsetf(std::ios::skipws);
std::istream_iterator<uint8_t> begin(file), end;
std::vector<uint8_t> fileData;
fileData.reserve(size);
fileData = std::vector<uint8_t>(begin, end);
Any other ideas on how I might speed this up?
EDIT 2: I searched online and found this:
char* cp = new char[100000000];
std::ofstream ofs("bff.txt"); //make a huge file to test with.
ofs.write(cp, 100000000);
ofs.close();
std::ifstream ifs("bff.txt");
ifs.read(cp, 100000000);
ifs.close();
This is extremely fast and also copies all the data into into the char array extremely quickly. Can anyone tell me how I might do this with uint8_ts?