0

How can I make the code below to read correct text. In my text file has Hello welcome to C++, however at the end of the text, it has a new line. With the code below, my readBuffer always contains extra characters.

DWORD byteWritten;
int fileSize = 0;

//Use CreateFile to check if the file exists or not.
HANDLE hFile = CreateFile(myFile, GENERIC_READ, FILE_SHARE_READ, NULL, 
                            OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);

if(hFile != INVALID_HANDLE_VALUE)
{
    BOOL readSuccess;
    DWORD byteReading;
    char readBuffer[256];
    readSuccess = ReadFile(hFile, readBuffer, byteReading, &byteReading, NULL);

    if(readSuccess == TRUE)
    {
        TCHAR myBuffer[256];
        mbstowcs(myBuffer, readBuffer, 256);

        if(_tcscmp(myBuffer, TEXT("Hello welcome to C++")) == 0)
        {
            FindClose(hFile);
            CloseHandle(hFile);

            WriteResultFile(TRUE, TEXT("success!"));
        }
    }
}

Thanks,

Robert Greiner
  • 29,049
  • 9
  • 65
  • 85
Bopha
  • 3,296
  • 6
  • 25
  • 23
  • thanks robbotic for editing my post. – Bopha Jun 09 '09 at 20:44
  • Not your main problem, but you are mix and matching wchar_t and TCHAR. mbstowcs() converts from char * to wchar_t *. TCHAR might be either char or wchar_t. If you want to use wchar_t, then use it. If you want to use TCHAR, then you need to test whetehr you need mbstowcs() or not. – Michael J Jun 10 '09 at 06:07
  • Thanks and I have changed mbstowcs to MultiByteToWideChar(). As of the new line at the each of text line, there is always there when I do ReadFile(). Since I don't know other way, I'm just using an old style to eliminate the new line: int index = 0; char buffer[256] = {'\0'}; while(readBuffer[index] != '\r') { buffer[index] = readBuffer[index]; index++; } So this just ignore the new line. – Bopha Jun 10 '09 at 19:49

3 Answers3

2

There are a few problems:

  • You're passing uninitialized data (byteReading) as the "# of bytes to read" parameter to ReadFile().
  • Depending on how you created the file, the file's contents may not have a terminating 0 byte. The code assumes that the terminator is present.
  • FindClose(hFile) doesn't make sense. CloseHandle(hFile) is all you need.
  • You need to call CloseHandle if CreateFile() succeeds. Currently, you call it only if you find the string you're looking for.

This isn't a bug, but it's helpful to zero-initialize your buffers. That makes it easier to see in the debugger exactly how much data is being read.

Michael Dunn
  • 429
  • 2
  • 2
  • You are right. I will not use ByteReading, but I will use 256 instead in the ReadFile(). FindClose(), it was used in my other program and it was required, but this program doesn't need it so I've commented out all. And the closeHandle(), I should not just put inside the string comparison, it should be outside the if-statement too. Actually This is a small portion code which I cute and paste from my program so it's missing some statements. – Bopha Jun 09 '09 at 19:52
2
  HANDLE hFile = CreateFile(myfile, GENERIC_READ, FILE_SHARE_READ, NULL, 
    OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);

  if(hFile != INVALID_HANDLE_VALUE)
  {
    BOOL readSuccess;
    DWORD byteReading = 255;
    char readBuffer[256];
    readSuccess = ReadFile(hFile, readBuffer, byteReading, &byteReading, NULL);
    readBuffer[byteReading] = 0;
    if(readSuccess == TRUE)
    {
      TCHAR myBuffer[256];
      mbstowcs(myBuffer, readBuffer, 256);

      if(_tcscmp(myBuffer, TEXT("Hello welcome to C++")) == 0)
      {
        rv = 0;
      }
    }
    CloseHandle(hFile);
  }

I see two things:

  • byteReading isn't initialized
  • you are reading bytes so you have to terminate the string by 0.
  • CloseHandle is sufficient
Totonga
  • 4,236
  • 2
  • 25
  • 31
0

Either remove the new line character from the file or use _tcsstr for checking the existence of the string "Hello Welcome to C++".

msvcyc
  • 2,569
  • 4
  • 24
  • 30
  • after following your suggestiong changing _tcscmp to _tcsstr, it works for the commparison, thanks. What in my text file has numeric value and I have to use the value so my readBuffer will contain extra characters beside the number? Unfortunately I can not change the text file content because it's written by other program by other people so I just have to read as it is. – Bopha Jun 09 '09 at 18:59