Results 1 to 5 of 5

Thread: easiest way to input an entire text file...

  1. #1

    Thread Starter
    The Devil crptcblade's Avatar
    Join Date
    Aug 2000
    Location
    Quetzalshacatenango
    Posts
    9,091

    Talking easiest way to input an entire text file...

    Can somebody give me a handle on it?

    Thanks

    Last edited by crptcblade; Aug 20th, 2001 at 08:21 PM.
    Laugh, and the world laughs with you. Cry, and you just water down your vodka.


    Take credit, not responsibility

  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 *)LocalAlloc(LPTR, dwLength + 1);
    		if (ReadFile(hFile, pszText, dwLength, &dwRead, NULL)) 
    		{
    			pszText[dwLength] = 0; 
    			if( SetWindowText(hText, (LPCTSTR) pszText)) {
    				bSuccess = true;
    			}
    		}
    		LocalFree((HLOCAL) pszText);
    	}
    	else
    	{
    		MessageBox(NULL, "File cannot exceed 65535 bytes", "Error", MB_OK | MB_ICONEXCLAMATION);
    		bSuccess = false;
    	}
    
    	CloseHandle(hFile);
    	return bSuccess;
    }
    Usage:

    OpenFile("FileName", hwndEdit);

  3. #3
    jim mcnamara
    Guest
    file handle, eh? how about a file pointer...
    PHP Code:
    #include <time.h>
    #include <sys/types.h>
    #include <sys/stat.h>
    #include <stdio.h>

    void mainvoid )
    {
       
    FILE *fptr;
       
    struct _stat buf;
       
    int i;
       
    char fnam[] = "myfile.txt";
       
    =  _statfnam, &buf );  // get file information
       // buf.st_size is now file len
       
    char *filebuf malloc(buf.st_size);  // get a place to store file in memory
       // check for malloc fail in good code
       
    fptr fopen(buffer);
       if ( 
    fread(filebuf, (size_tbuf.st_size, (size_tbuf.st_sizefptr) == buf.st_size)   // read it all in
       
    {
          
    printf("%s\n","success");
        }
        
    free(filebuf);
        
    fclose(fptr);   
     } 

  4. #4

    Thread Starter
    The Devil crptcblade's Avatar
    Join Date
    Aug 2000
    Location
    Quetzalshacatenango
    Posts
    9,091
    Thanks guys

    Laugh, and the world laughs with you. Cry, and you just water down your vodka.


    Take credit, not responsibility

  5. #5

    Re: easiest way to input an entire text file...

    Originally posted by crptcblade
    Can somebody give me a handle on it?

    Thanks

    *cough* fstream.h *cough*

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