|
-
Aug 20th, 2001, 07:54 PM
#1
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
-
Aug 21st, 2001, 08:27 AM
#2
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);
-
Aug 21st, 2001, 08:31 AM
#3
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 main( void )
{
FILE *fptr;
struct _stat buf;
int i;
char fnam[] = "myfile.txt";
i = _stat( fnam, &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_t) buf.st_size, (size_t) buf.st_size, fptr) == buf.st_size) // read it all in
{
printf("%s\n","success");
}
free(filebuf);
fclose(fptr);
}
-
Aug 21st, 2001, 02:44 PM
#4
Thanks guys
Laugh, and the world laughs with you. Cry, and you just water down your vodka.
Take credit, not responsibility
-
Aug 21st, 2001, 03:06 PM
#5
Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|