Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

writing to files 1

Status
Not open for further replies.

computerwhiz

Programmer
May 1, 2002
28
US
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;
}
 
> pch = (LPCH) LocalAlloc(LPTR, sizeof(char)*81);
Is there any reason why you allocate just 81 bytes, when you have local arrays of 512 bytes?
It would save having to free the memory at the end of the function (which you don't do at present).
Nor do you check to see if that memory allocation failed.

> LPSTR Temp = strrchr(lpszFile, 0x05C)+1;
Why not write this, for readability
[tt]LPSTR Temp = strrchr(lpszFile, '\\' )+1;[/tt]
Additionally, you should check for the return result being NULL.

> WriteFile(hWFile, pchTmp, lstrlen(pchTmp), &dwWritten, NULL);
ReadFile doesn't append a \0 onto the end of the data it reads (which is what lstrlen() is going to go looking for). My guess is that the allocated memory has a \0 in nearly the right place, which makes the -1 fix you have mostly OK.
Since you already have the length in cb, you may as well write
[tt]WriteFile(hWFile, pchTmp, cb, &dwWritten, NULL);[/tt]

Comparing cb with dwWritten is an additional check, just in case your file system fills up for example.

> ZeroMemory(pchTmp, sizeof(pchTmp));
This only clears 4 bytes (the size of the pointer), not the 81 bytes it points to.
All your ZeroMemory calls do not seem to add any value to the code you have.
The single case in lpszSaveFile where it might matter is easily fixed with
[tt]CopyMemory(lpszSaveFile, lpszFile, lstrlen(lpszFile)-4);
lpszSaveFile[lstrlen(lpszFile)-4)] = '\0';[/tt]
Optimising to call lstrlen() just once is a bonus.

> if (hWFile == (HANDLE) INVALID_HANDLE_VALUE)
This can leave hRFile open.

> CopyMemory(lpszSaveFile, lpszFile, lstrlen(lpszFile)-4);
This assumes that the extension to the file is exactly 3 characters.

--
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top