Thread: Filling in the edit with the file

  1. #1
    the Corvetter
    Join Date
    Sep 2001
    Posts
    1,584

    Filling in the edit with the file

    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.
    1978 Silver Anniversary Corvette

  2. #2
    Registered User
    Join Date
    Apr 2002
    Posts
    1,571
    Look up SendDlgItemMessage( ) if the text doesn't fit why not enlarge your edit control?
    "...the results are undefined, and we all know what "undefined" means: it means it works during development, it works during testing, and it blows up in your most important customers' faces." --Scott Meyers

  3. #3
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,793

    Re: Filling in the edit with the file

    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.
    CreateFile().....//Open your 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

  4. #4
    Registered User sean345's Avatar
    Join Date
    Mar 2002
    Posts
    346
    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
    If cities were built like software is built, the first woodpecker to come along would level civilization.
    Black Frog Studios

  5. #5
    the Corvetter
    Join Date
    Sep 2001
    Posts
    1,584
    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!
    1978 Silver Anniversary Corvette

  6. #6
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,793
    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

  7. #7
    Registered User
    Join Date
    Jan 2002
    Posts
    387
    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);
    	}
    }
    "There are three kinds of people in the world...
    Those that can count and those that can't."

  8. #8
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    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:

    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;
    }
    This is OS independant (not too important i'm guessing) but more importantly it is simple and compact.

  9. #9
    the Corvetter
    Join Date
    Sep 2001
    Posts
    1,584
    Why "rb"? Isn't hte "b" for binary? Why would you read like that?
    1978 Silver Anniversary Corvette

  10. #10
    Registered User
    Join Date
    Jul 2002
    Posts
    273
    open file for reading and binary

  11. #11
    the Corvetter
    Join Date
    Sep 2001
    Posts
    1,584
    Yeah, I know. But why read binary?
    1978 Silver Anniversary Corvette

  12. #12
    train spotter
    Join Date
    Aug 2001
    Location
    near a computer
    Posts
    3,868
    >>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.
    "Man alone suffers so excruciatingly in the world that he was compelled to invent laughter."
    Friedrich Nietzsche

    "I spent a lot of my money on booze, birds and fast cars......the rest I squandered."
    George Best

    "If you are going through hell....keep going."
    Winston Churchill

  13. #13
    the Corvetter
    Join Date
    Sep 2001
    Posts
    1,584
    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.
    1978 Silver Anniversary Corvette

  14. #14
    Registered User VBprogrammer's Avatar
    Join Date
    Mar 2002
    Posts
    175
    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.
    VC++ 6

  15. #15
    ¡Amo fútbol!
    Join Date
    Dec 2001
    Posts
    2,138
    VB, he's writing with winAPI. That basically removes code portability.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. File transfer- the file sometimes not full transferred
    By shu_fei86 in forum C# Programming
    Replies: 13
    Last Post: 03-13-2009, 12:44 PM
  2. Problems passing a file pointer to functions
    By smitchell in forum C Programming
    Replies: 4
    Last Post: 09-30-2008, 02:29 PM
  3. Batch file programming
    By year2038bug in forum Tech Board
    Replies: 10
    Last Post: 09-05-2005, 03:30 PM
  4. Replies: 3
    Last Post: 03-04-2005, 02:46 PM
  5. simulate Grep command in Unix using C
    By laxmi in forum C Programming
    Replies: 6
    Last Post: 05-10-2002, 04:10 PM