I have the file test.txt and in that it says "testing" and when I click a button in my program I want the text in the file to be displayed in a editbox on my program.
How would I do this?
Printable View
I have the file test.txt and in that it says "testing" and when I click a button in my program I want the text in the file to be displayed in a editbox on my program.
How would I do this?
Try this:
Code:bool OpenFile(LPSTR sFileName, HWND hText) {
HANDLE hFile;
bool bSuccess;
LPSTR pszText;
DWORD dwLength;
DWORD dwRead;
hFile = CreateFile(sFileName, GENERIC_READ, NULL, NULL, OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL, NULL);
if ( !hFile )
return false;
dwLength = GetFileSize(hFile, NULL);
if(dwLength != 0xFFFF)
{
pszText = (char *)GlobalAlloc(GPTR, dwLength + 1);
if (ReadFile(hFile, pszText, dwLength, &dwRead, NULL))
{
pszText[dwLength] = 0;
if( SetWindowText(hText, (LPCTSTR) pszText)) {
bSuccess = true;
}
}
GlobalFree((HGLOBAL) pszText);
}
else
{
MessageBox(NULL, "File cannot exceed 65535 bytes", "Error", MB_OK | MB_ICONEXCLAMATION);
bSuccess = false;
}
CloseHandle(hFile);
return bSuccess;
}
here is some sample code...
regards,PHP Code:DWORD dwptr=0, dwSize=0, dwByteRead=0, dwSz=0;
char *Buffer=NULL;
HANDLE pFile=NULL;
pFile = CreateFile("E:\\Project X\\DataFile\\test.txt",
GENERIC_READ,
FILE_SHARE_READ,
NULL,
OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL | FILE_FLAG_RANDOM_ACCESS,
NULL);
if (pFile != INVALID_HANDLE_VALUE)
{
// Get file size
dwSz = GetFileSize(pFile, NULL);
// Create buffer for storing the read data
Buffer = new char[dwSz];
memset(Buffer,0,dwSz);
// Reset file pinter
dwptr = 0;
// Read the all the data in one short.
ReadFile(pFile, Buffer, dwSz, &dwByteRead, NULL);
// Display the read data into EDIT control
SetDlgItemText(hWnd, IDC_EDIT1, Buffer);
// Detele the buffer
delete [] Buffer;
// Close the opned definition file
CloseHandle(pFile);
return 1;
}
else
{
// Close the opened definition file
CloseHandle(pFile);
return 0;
}
Thanks guys that worked.
Only one thing though...
I'm using your code, Chris. And when I push the button it reads in the data in the editbox but it looks like this: testýýýý1
Hehe, how do I get rid of those wierd signs?
Juz correct this line will do.
:P 'coz my previous posted code is store all the read data into a structure, so it didn't hit this error.PHP Code:dwSz = GetFileSize(pFile, NULL) + 1;
regards,
I think Megatron do a good validation in his posted code, whereby he does check for the filesize is a valid or not before proceed to the next command line :)
Hence, i did amend my posted code :D
regards,PHP Code:DWORD dwptr=0, dwSize=0, dwByteRead=0, dwSz=0;
char *Buffer=NULL;
HANDLE pFile=NULL;
pFile = CreateFile("E:\\Project X\\DataFile\\test.txt",
GENERIC_READ,
FILE_SHARE_READ,
NULL,
OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL | FILE_FLAG_RANDOM_ACCESS,
NULL);
if (pFile != INVALID_HANDLE_VALUE)
{
// Get file size
dwSz = GetFileSize(pFile, NULL);
if (dwSz != INVALID_FILE_SIZE)
{
dwSz = dwSz+1;
// Create buffer for storing the read data
Buffer = new char[dwSz];
memset(Buffer,0,dwSz);
// Reset file pinter
dwptr = 0;
// Read the all the data in one short.
ReadFile(pFile, Buffer, dwSz, &dwByteRead, NULL);
// Display the read data into EDIT control
SetDlgItemText(hWnd, IDC_EDIT1, Buffer);
// Detele the buffer
delete [] Buffer;
// Close the opned definition file
CloseHandle(pFile);
return 1;
}
else
{
// Close the opened definition file
CloseHandle(pFile);
return 0;
}
}
else
{
// Close the opened definition file
CloseHandle(pFile);
return 0;
}