Results 1 to 6 of 6

Thread: Display text from file in a editbox

  1. #1

    Thread Starter
    Member
    Join Date
    Feb 2001
    Posts
    40

    Display text from file in a editbox

    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?

  2. #2
    Megatron
    Guest
    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;
    }

  3. #3
    PowerPoster Chris's Avatar
    Join Date
    Jan 1999
    Location
    K-PAX
    Posts
    3,238
    here is some sample code...

    PHP Code:
        DWORD    dwptr=0dwSize=0dwByteRead=0dwSz=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(pFileNULL);
            
    // 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(pFileBufferdwSz, &dwByteReadNULL);
            
    // Display the read data into EDIT control
            
    SetDlgItemText(hWndIDC_EDIT1Buffer);
            
    // Detele the buffer
            
    delete [] Buffer;
            
    // Close the opned definition file
            
    CloseHandle(pFile);
            return 
    1;
        }
        else
        {
            
    // Close the opened definition file
            
    CloseHandle(pFile);
            return 
    0;
        } 
    regards,

  4. #4

    Thread Starter
    Member
    Join Date
    Feb 2001
    Posts
    40
    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?

  5. #5
    PowerPoster Chris's Avatar
    Join Date
    Jan 1999
    Location
    K-PAX
    Posts
    3,238
    Juz correct this line will do.

    PHP Code:
    dwSz GetFileSize(pFileNULL) + 1
    :P 'coz my previous posted code is store all the read data into a structure, so it didn't hit this error.

    regards,

  6. #6
    PowerPoster Chris's Avatar
    Join Date
    Jan 1999
    Location
    K-PAX
    Posts
    3,238
    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
    PHP Code:
        DWORD    dwptr=0dwSize=0dwByteRead=0dwSz=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(pFileNULL);

            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(pFileBufferdwSz, &dwByteReadNULL);
                
    // Display the read data into EDIT control
                
    SetDlgItemText(hWndIDC_EDIT1Buffer);
                
    // 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;
        } 
    regards,

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width