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.
golfinguy4 - my point exactly! (Read one of the threads about ASM if you dont understand) What he should have done was use as many C++ libary functions as possible, this maintains code (code not executible) portability.
I don't write functions like that so avoid using the win api. I have no gripes about overhead or speed when it comes to IO on any operating system. However, why write something you'll just need to re-write later. I think one advantage to making portable code that no-one really talks about is that I can get programming help from a Unix message board. I really don't care if fopen, fclose, fread, fwrite, read, write, creat, open, and close all use the api (which would make sense since they interface with the actual operating system) they get the job done without me needing to look in the Windows SDK manual every time I need to use them.
Yes but the fundemental goal was to display data in an editbox......therefore his code cannot be portable.....Quote:
Originally posted by VBprogrammer
golfinguy4 - my point exactly! (Read one of the threads about ASM if you dont understand) What he should have done was use as many C++ libary functions as possible, this maintains code (code not executible) portability.
Fordy is right to some extent. But once again, portable doesn't necessarily mean you take windows source code and compile it to linux. It means I that one doesn't need to spend 5 hours changing window's specific calls to linux specific calls. I think people have a personal preference when it comes to making portable code. The way I look at is that one day microsoft may crumble and we are all using some new operating system. Some of us will be able to take our code and with little changes be able to re-use it. Others will have to start over from the top. I make portable code so I can make open source projects that other platforms can use.
Another interersting angle.......if I develop a commercial app for windows, and I lose the source......would I want it to be easily ported to *nix?....dont think so....Quote:
Originally posted by master5001
Fordy is right to some extent. But once again, portable doesn't necessarily mean you take windows source code and compile it to linux. It means I that one doesn't need to spend 5 hours changing window's specific calls to linux specific calls. I think people have a personal preference when it comes to making portable code. The way I look at is that one day microsoft may crumble and we are all using some new operating system. Some of us will be able to take our code and with little changes be able to re-use it. Others will have to start over from the top. I make portable code so I can make open source projects that other platforms can use.
Okay, I got it working to open a file and put the text in the edit, but now how would I take the text and save it to a file? Thanks, fellas! :D
Send Messages EM_GETTEXTLENGTHEX & EM_GETTEXTEX To get the text...then WriteFile();Quote:
Originally posted by Garfield
Okay, I got it working to open a file and put the text in the edit, but now how would I take the text and save it to a file? Thanks, fellas! :D
Fordy, what are the parameter specifics of those messages? This bothers me. Is there any place that I can get the SDK CD for free? I need these documentations...
I have a question regarding this code
This works fine when I am using it to open smaller files. In fact it has worked on ASCII files up to 1.8 Mb large. When I tried a 4 Mb ASCII file it failed. I debugged and found it was failing at if(dwFileSize != 0xFFFFFFFF). Is this way limited by the size of the file? I am not using an edit control by the way I am using a richedit control.Code: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);
}
}
- Sean
Info hereQuote:
Originally posted by Garfield
Fordy, what are the parameter specifics of those messages? This bothers me. Is there any place that I can get the SDK CD for free? I need these documentations...
Yeah, I remember that thread, but I can't always go online to look up a function or some other stuff, so what's why I would need the CD. Anywhere?
:pQuote:
Originally posted by Garfield
Yeah, I remember that thread, but I can't always go online to look up a function or some other stuff, so what's why I would need the CD. Anywhere?
No Garf...the link given by Robert602 allows you to go and download the docs of the SDK.....this is very much like the MSDN you get with VC++....except its more up to date and doesnt have any std lib functions (but all documented APIs are in there).....
Fordy, do I want to install the Core SDK or the Windows installer SDK? Thanks...
I would get the core SDK...it will have the new libs and includes......add them to your options->directories in VC++ and you can use the newly defined functions....Quote:
Originally posted by Garfield
Fordy, do I want to install the Core SDK or the Windows installer SDK? Thanks...
Yeah, I pretty much just need them for reference on params and the such...
>>dwFileSize = GetFileSize(hFile, NULL);
try using the lpFileSizeHigh param (rather than leaving it NULL)
Code:SetLastError(ERROR_SUCCESS);
dwFileSizelow=GetFileSize(hFile,&dwFileSizeHigh);
if(dwFileSizeLow == INVALID_FILE_SIZE)
if(GetLastError()!= ERROR_SUCCESS)//or NO_ERROR