views:

630

answers:

3

Hello everyone,

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,

A: 

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

msvcyc
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
+1  A: 

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
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
+1  A: 
  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