Weird problem using ifstream / rdbuf
I'm trying to read in large amounts of data using the rdbuf member of ifstream, it works fine as long as the bufferSize I pass into rdbuf as a parametre is an even number, but if its an odd number I get a completely wrong set of data from the file. Ill show the code and the results i'm getting.
Data in file is: LDHGSJDHFKJASTEVEDHSFDLHSFDHSFAHDSVICTORKFHDSAKJFH (file goes on but this is the start)
unsigned int bufferSize = 20;
size_t searching = bufferSize;
char *bufferPrimary = new char[bufferSize + 1];
std::ifstream fin(filename->c_str(), std::ifstream::in);
searching = fin.rdbuf()->_Sgetn_s(bufferPrimary, bufferSize, bufferSize);
bufferPrimary[bufferSize] = fin.widen('\0');
std::cout << "Using set: " << bufferPrimary << std::endl;
If bufferSize is 20 I get: LDHGSJDHFKJASTEVEDHS (this seems correct)
If bufferSize is 21 I get: FDLHSFDHSFAHDSVICTORF (this is not the start of the file... and I dont know where the F on the end comes from)
Is this because I may be using pubsetbuf wrong? I used it because I wanted to limit the buffer size read in.
If anyone could tell me what i'm doing wrong I would be very greatful, thanks.