computerwhiz
Programmer
I am reading a file by 81 char chunks, after I process this piece of info and write it to the new file, there is a "defective" char at every 81 char split. I have been having to move the file pointer back one space to correct this but is there a something that I am doing wrong?
Code:
BOOL ProcessFiles(HWND hwnd)
{
HANDLE hRFile; // Handle of file to read
HANDLE hWFile; // Handle of file to write
DWORD cb; // count of bytes read
DWORD dwFileSize; // Size of file to read
DWORD dwWritten; // Number of bytes wrote to file
int iTotBytes = 0; // Total bytes read
LPCH pch; // Address of data read from file
LPCH pchTmp; // Temporary pointer
char lpszFile[512]; // File name from list box
char lpszSaveFile[512];// File name to save as
pch = (LPCH) LocalAlloc(LPTR, sizeof(char)*81); //reading file buffer
pchTmp = pch;
for(int a = 0; a <= uintLBCount-1; a++)
{
ZeroMemory(lpszFile, sizeof(lpszFile));
ZeroMemory(lpszSaveFile, sizeof(lpszSaveFile));
SendMessage(hwndLB, LB_GETTEXT, a, (LPARAM) lpszFile);
CopyMemory(lpszSaveFile, lpszFile, lstrlen(lpszFile)-4);
lstrcat(lpszSaveFile, ".csv");
LPSTR Temp = strrchr(lpszFile, 0x05C)+1;
SetWindowText(hwndSTS, Temp);
hRFile = CreateFile(
lpszFile, GENERIC_READ, FILE_SHARE_READ, (LPSECURITY_ATTRIBUTES)
NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, (HANDLE) NULL);
hWFile = CreateFile(lpszSaveFile, GENERIC_WRITE, 0, 0,
CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, 0);
if (hRFile == (HANDLE) INVALID_HANDLE_VALUE)
return FALSE;
if (hWFile == (HANDLE) INVALID_HANDLE_VALUE)
return FALSE;
dwFileSize = GetFileSize(hRFile, (LPDWORD) NULL);
// Parse the file.
while(ReadFile(hRFile, pchTmp, sizeof(char)*81, &cb, (LPOVERLAPPED) NULL))
{
if(cb)
{
iTotBytes += cb;
// Include here code that parses the file.
//.
SetFilePointer(hWFile, -1, NULL, FILE_CURRENT);
WriteFile(hWFile, pchTmp, lstrlen(pchTmp), &dwWritten, NULL);
// Advance the current position of the
// progress bar by the % of file read.
ZeroMemory(pchTmp, sizeof(pchTmp));
SendMessage(hwndPB, PBM_SETPOS, (WPARAM)
(((float)iTotBytes/(float)dwFileSize)*100), 0);
}
else break;
}
//SetFilePointer(hWFile, -1, NULL, FILE_CURRENT);
//SetEndOfFile(hWFile);
CloseHandle((HANDLE) hWFile);
CloseHandle((HANDLE) hRFile);
SetWindowText(hwndSTS, "...");
SendMessage(hwndPB, PBM_SETPOS, (WPARAM)0, 0);
}
return TRUE;
}