How would I do this? I successfully load the file, and now I want to put it into the edit control. I did fgets and then SetWindowText, but that only allows for one line of text, not the whole file.
Printable View
How would I do this? I successfully load the file, and now I want to put it into the edit control. I did fgets and then SetWindowText, but that only allows for one line of text, not the whole file.
Look up SendDlgItemMessage( ) if the text doesn't fit why not enlarge your edit control?
CreateFile().....//Open your fileQuote:
Originally posted by Garfield
How would I do this? I successfully load the file, and now I want to put it into the edit control. I did fgets and then SetWindowText, but that only allows for one line of text, not the whole file.
GetFileSize().....//Find Size
GetProcessHeap()....//Find Heap
HeapAlloc()....//Set aside memory
ReadFile()......//Put file contents in memory
SetWindowText().....//Put data into edit
HeapFree().....//Free Memory
CloseHandle........//Close file
You could try loading the entire file into an array. I cannot remember the exact functions because I am at work (I will post them when I get home). This is how it works.
Open the file (using windows API)
GetFileSize() (something like this)
Creat Dynamic array with file size
Fread the entire file into the array using the file size
close file handle
SetWindowText() using the array
free the memory in the array
[Edit]
I see someone else posted the functions
[/Edit]
- Sean
Okay, I guess I'll stick with the API functions instead of the STDIO ones. Guess I'm gonna have to change around some code now. Thanks, fellas!
You can use the std libs if you want...there's no real restriction for something like this.......go with what you feel comfortable with
heres a function i made that i used in my text-editor that brings up the open file dialog box, and lets the use pick a file to open, then loads it to the edit box
Code:void FileOpen(HWND hwnd, HWND hEdit)
{
#define MAX_PATHSIZE 256
OPENFILENAME ofn;
char FileName[MAX_PATHSIZE] = "";
HANDLE hFile;
ZeroMemory(&ofn, sizeof(ofn));
ofn.lStructSize = sizeof(ofn);
ofn.hwndOwner = hwnd;
ofn.lpstrFilter = "Text Files (*.txt)\0*.txt\0All Files (*.*)\0*.*\0";
ofn.lpstrFile = FileName;
ofn.nMaxFile = MAX_PATHSIZE;
ofn.Flags = OFN_EXPLORER | OFN_FILEMUSTEXIST | OFN_HIDEREADONLY;
ofn.lpstrDefExt = "txt";
if(GetOpenFileName(&ofn))
{
}
hFile = CreateFile(FileName, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, NULL);
if(hFile != INVALID_HANDLE_VALUE)
{
DWORD dwFileSize;
dwFileSize = GetFileSize(hFile, NULL);
if(dwFileSize != 0xFFFFFFFF)
{
LPSTR FileText;
SendMessage(hStatus, SB_SETTEXT, 0, (LPARAM)"Opening...");
FileText = (LPSTR)GlobalAlloc(GPTR, dwFileSize + 1);
if(FileText != NULL)
{
DWORD dwRead;
if(ReadFile(hFile, FileText, dwFileSize, &dwRead, NULL))
{
FileText[dwFileSize] = 0; // Set the NULL terminator
SetWindowText(hEdit, FileText);
}
GlobalFree(FileText);
}
}
CloseHandle(hFile);
SendMessage(hStatus, SB_SETTEXT, 0, (LPARAM)"Ready...");
char WindowText[256];
strcpy(WindowText, "NotepadX -- ");
strcat(WindowText, FileName);
SetWindowText(hwnd, WindowText);
}
}
I don't know if windows programmers look down on me for this or not but I never use the api's file functions. Just do this:
This is OS independant (not too important i'm guessing) but more importantly it is simple and compact.Code:<include stdio.h>
char *GetFileArray(const char *filename) {
char *buffer;
int fsize;
FILE *file = fopen(filename, "rb");
if(file == NULL)
return NULL;
fseek(file, 0, 2); //set the pointer to the end of the file
fsize = ftell(file);//get the file size
fseek(file, 0, 0); //set the pointer back to the beginning
buffer = (char *)malloc(fsize);
if(buffer == NULL)
return NULL;
fread(buffer, fsize, 1, file);
fclose(file);
return buffer;
}
Why "rb"? Isn't hte "b" for binary? Why would you read like that?
open file for reading and binary
Yeah, I know. But why read binary?
>>Yeah, I know. But why read binary?
If written in binary each element (use those struct's to write the file) has the same size. This may not be true in text mode where the actual length of the string may affect the size.
I was thinking about using the standard C functions, but I think I'm just going to read up on those WinAPI functions and read it all into the heap and then put it in the edit with the WinAPI functions.
master5001 - Im 100% sure that you'll find that using those functions simply indirectly uses the windows API. Also you will find that acording to the "Old farts guide to C programming" (fictional) that your way is actually the correct way of file access as if you used the API directly you would be voiding one of C's main advantages that it can be ported across many different platforms with minimal changes and a different compiler.
VB, he's writing with winAPI. That basically removes code portability.