ReadFile?

This is a discussion on ReadFile? within the Windows Programming forums, part of the Platform Specific Boards category; I was wondering how to use ReadFile. I want to read the data in a .txt file and output it ...

  1. #1
    Programming is fun, mkay?
    Join Date
    Oct 2001
    Posts
    490

    Question ReadFile?

    I was wondering how to use ReadFile. I want to read the data in a .txt file and output it in my EDIT box. I know how to use the LB_ADDSTRING part, but I need to know how to use ReadFile to get the file's data. Any help is appreciated!
    Website(s): http://www16.brinkster.com/trifaze/

    E-mail: trifaze_mattu@lycos.com

    ---------------------------------
    C++ Environment: MSVC++ 6.0; Dev-C++ 4.0/4.1
    DirectX Version: 9.0b
    DX SDK: DirectX 8.1 SDK

  2. #2
    Banned maes's Avatar
    Join Date
    Aug 2001
    Posts
    744
    here is a function that will read the file for you:
    the text is being put in the global variable text, and the function returns the filelength


    Code:
    char *text;
    
    int ReadTextFile(char FileName[MAX_PATH])
    {
    	char *pBuffer;
    	int iFileLength;
    	HANDLE hFile;
    	DWORD dwBytesRead;
    
    
    	if (INVALID_HANDLE_VALUE == 
                   (hFile = CreateFile (FileName, GENERIC_READ, FILE_SHARE_READ,
                                        NULL, OPEN_EXISTING, 0, NULL)))
    									return -1;
    	 iFileLength = GetFileSize (hFile, NULL) ;
    	 pBuffer = (char *)malloc (iFileLength + 2) ;
         ReadFile (hFile, pBuffer, iFileLength, &dwBytesRead, NULL) ;
    	 pBuffer[iFileLength] = '\0' ;
         pBuffer[iFileLength + 1] = '\0' ;
    	 text = (char *)malloc (iFileLength + 2) ;
    	 strcpy(text,pBuffer);
    	 free(pBuffer);
    	 CloseHandle (hFile) ;
    	 return iFileLength;
    }
    bye

  3. #3
    train spotter
    Join Date
    Aug 2001
    Location
    near a computer
    Posts
    3,854
    Instead of malloc use GlobalAlloc()

    ie
    Code:
    HGLOBAL  hMem=NULL;
    int      *pInt;
    
    hMem=GlobalAlloc(GHND,sizeof(int)*1000);
    if(hMem==NULL)
    {
        iError=GetLastError();
        sprintf(sBuffer,"Mem Alloc Error#%d in 'MyFunction'.",iError);
        MessageBox(hDlg,sBuffer,"My App Error",MB_OK|MB_ICONERROR);
    }
    else pInt=(int *)GlobalLock(hMem);
    Always check mem allocs for errors. Remember to free the mem later. ( With GlobalFree() and GlobalHandle() if needed )

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. ReadFile();
    By Queatrix in forum Windows Programming
    Replies: 8
    Last Post: 05-02-2005, 02:42 PM
  2. using ReadFile, WriteFile, strSafe
    By talz13 in forum C++ Programming
    Replies: 0
    Last Post: 02-08-2005, 11:37 AM
  3. ReadFile function fails.
    By dpkeller in forum C++ Programming
    Replies: 2
    Last Post: 12-03-2004, 09:20 PM
  4. fread readfile
    By samsam1 in forum Windows Programming
    Replies: 3
    Last Post: 02-03-2003, 01:14 PM
  5. Using API function ReadFile()
    By Echidna in forum Windows Programming
    Replies: 4
    Last Post: 10-23-2001, 10:33 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21