I'm writing a function in my Visual C++ project that reads contents of a file via WinAPI in 2000 byte increments and returns it as a std::string.
A problem occurs when the file is much larger than the buffer (for example 100 KB), I get garbage added at several locations in the file in the middle of valid data. This is a long 0xcccccccc...
sequence terminated by 3-4 other bytes, usually appearing in the middle of a word. The function doesn't fail otherwise and none of the valid data is missing.
I haven't checked the exact positions but it seems that this happens at buffer size increments (or a multiplier of buffer size increments). If I increase the size of the buffer to more than the size of the test files, the problem goes away. What causes this to happen? What am I doing wrong?
std::string read_file(std::string filename) {
HANDLE hFile = CreateFile(filename.c_str(), GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, NULL, NULL);
if (hFile == INVALID_HANDLE_VALUE)
{
std::string errortext("Error opening " + filename + ", bad handle value: " + to_string((int)hFile));
MessageBox(hwnd, errortext.c_str(), "Error", 0);
return "";
}
char buffer[2000] = "";
std::string entire_file = "";
DWORD dwBytesRead = 0;
while (ReadFile(hFile, buffer, sizeof(buffer), &dwBytesRead, NULL))
{
if (!dwBytesRead)
break;
entire_file += buffer;
}
CloseHandle(hFile);
return entire_file;
}