如何使以下代碼能夠讀取正確的文本。在我的文本文件中,歡迎使用C++,但是在文本末尾,它有一個新行。用下面的代碼,我的readBuffer總是包含額外的字符。使用ReadFile()從文本文件中讀取字符串時遇到問題
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!"));
}
}
}
感謝,
感謝robbotic編輯我的文章。 – Bopha 2009-06-09 20:44:47
不是你的主要問題,但你是混合和匹配wchar_t和TCHAR。 mbstowcs()從char *轉換爲wchar_t *。 TCHAR可能是char或wchar_t。如果你想使用wchar_t,那就使用它。如果你想使用TCHAR,那麼你需要測試你需要mbstowcs()與否。 – 2009-06-10 06:07:38
s/whetehr /是/ – 2009-06-10 06:08:40