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!
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 ...
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
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
byeCode: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; }
Instead of malloc use GlobalAlloc()
ie
Always check mem allocs for errors. Remember to free the mem later. ( With GlobalFree() and GlobalHandle() if needed )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);