Убедитесь, что указатель приращения введен в l oop. Также необходимо избегать утечек памяти:
#include <windows.h>
#include <iostream>
BYTE* ReadFileToMem(WCHAR* szFileName, DWORD& dwSize,DWORD& ByteSize)
{
HANDLE hFile = CreateFileW(szFileName, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, NULL, NULL);
if (hFile == INVALID_HANDLE_VALUE)
return 0;
dwSize = GetFileSize(hFile, NULL);
if (!dwSize)
return 0;
BYTE* pFileBuffer = (BYTE*)VirtualAlloc(NULL, dwSize, MEM_COMMIT | MEM_RESERVE, PAGE_READWRITE);
if (!pFileBuffer)
return 0;
DWORD dwRead = 0;
ReadFile(hFile, pFileBuffer, dwSize, &dwRead, NULL);
CloseHandle(hFile);
int allocSize{};
for (int i = 1; i <= dwSize; i++)
{
if (i % 4) continue;
allocSize++;
}
ByteSize = allocSize + dwSize;
BYTE* pFileBufferB = (BYTE*)VirtualAlloc(NULL, ByteSize, MEM_COMMIT | MEM_RESERVE, PAGE_READWRITE);
BYTE* pp = pFileBufferB;
LPBYTE p = pFileBuffer;
for (int i = 1; i <= dwSize; i++, p++, pp++)
{
*pp = *p;
if (i % 4 == 0)
{
pp++;
*pp = '*';
}
}
std::cout << allocSize;
VirtualFree(pFileBuffer, dwSize, MEM_RELEASE);
return pFileBufferB;
}
int main()
{
DWORD dwSize = 0;
DWORD ByteSize = 0;
WCHAR file[] = L"test.txt";
BYTE* pFileBufferB = ReadFileToMem(file, dwSize, ByteSize);
//To Do;
VirtualFree(pFileBufferB, ByteSize, MEM_RELEASE);
return 0;
}
Или использовать std::string::insert(index,std::string)
:
#include <windows.h>
#include <iostream>
#include <vector>
#include <string>
std::string ReadFileToMem(WCHAR* szFileName, DWORD& dwSize)
{
HANDLE hFile = CreateFileW(szFileName, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, NULL, NULL);
if (hFile == INVALID_HANDLE_VALUE)
return 0;
dwSize = GetFileSize(hFile, NULL);
if (!dwSize)
return 0;
CHAR* pFileBuffer = (CHAR*)VirtualAlloc(NULL, dwSize, MEM_COMMIT | MEM_RESERVE, PAGE_READWRITE);
if (!pFileBuffer)
return 0;
DWORD dwRead = 0;
ReadFile(hFile, pFileBuffer, dwSize, &dwRead, NULL);
CloseHandle(hFile);
std::string tmp = pFileBuffer;
VirtualFree(pFileBuffer, dwSize, MEM_RELEASE);
for (int i = dwSize; i > 0; i--)
{
if (i % 4) continue;
tmp.insert(i,"*");
}
return tmp;
}
int main()
{
DWORD dwSize = 0;
DWORD ByteSize = 0;
WCHAR file[] = L"test.txt";
std::string pFileBufferB = ReadFileToMem(file, dwSize, ByteSize);
std::cout << pFileBufferB;
return 0;
}