2012年3月29日 星期四

[Win API] Sample of reading txt file


It is just a simple example for reading text from .txt file using win32 api.


HANDLE hFile = CreateFile(dataFilePath, GENERIC_READ, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0);
if (hFile == INVALID_HANDLE_VALUE)
{
    return;
}

// read all characters from file.
DWORD bufferSize = GetFileSize(hFile, 0);
char* buffer = new char[bufferSize];
memset(buffer, '\0', bufferSize);

bool result = false;
DWORD bytesRead = 0;
result = !!ReadFile(hFile, &buffer, bufferSize, &bytesRead, 0);

delete[]  buffer;
CloseHandle(hFile);



Then, you can follow the example below to get file path.
http://twnin.blogspot.com/2012/03/get-current-directory-and-convert-to.html

沒有留言:

張貼留言