Thread: Extracting Text from Multiline Edit Box

  1. #1
    VA National Guard The Brain's Avatar
    Join Date
    May 2004
    Location
    Manassas, VA USA
    Posts
    903

    Question Extracting Text from Multiline Edit Box

    Having troubles handling text from a multiline edit box.. the first part of this algorithm runs fine by itself.. the second part (highlighted in blue) runs fine as a DOS project.. but when I add it to this windows project.. it distorts the GUI display and causes the program to terminate...



    Code:
    void EditBoxFileParser(HDC hdc, PAINTSTRUCT* ps, HFONT hFont, HWND hwnd, HWND hEdit)
    {
         
         int iCount; 
         WORD iLength, iOffset;        
         TCHAR **Lines;
         
         //Get Number of Lines in Edit Field
         iCount = SendMessage(hEdit, EM_GETLINECOUNT, 0, 0);
         
         //If Editbox is empty, exit function
         if(!iCount)
         
              return;
         
         Lines = new TCHAR*[iCount];
         
         //Populate 2D array, Lines[LineIndex][LineText]
         for(int i=0; i<iCount; i++)
         {    iOffset = SendMessage(hEdit, EM_LINEINDEX, i, 0);
              iLength = (WORD)SendMessage(hEdit, EM_LINELENGTH, iOffset, 0);
              Lines[i] = new TCHAR[iLength+sizeof(WORD)+1];
              CopyMemory(Lines[i], &iLength, sizeof(WORD));          
              SendMessage(hEdit, EM_GETLINE, i, (LPARAM)Lines[i]);
              Lines[i][iLength] = '\0';
         }   
         
         //Visually Verify Lines[][]
         for(int i=0, x=300, y=275; i<iCount; i++)
              
                 TextOut(hdc, x, y+=20, Lines[i], lstrlen(Lines[i]));   
    
         
    /* Code works fine up until this point */
    
    //The code below works fine by itself in as a DOS application
    //But locks up this windows project
     
        //Parse User Entered Text
        TCHAR **Name = new TCHAR*[iCount];
        TCHAR **FiberCount1 = new TCHAR*[iCount];
        TCHAR **FiberCount2 = new TCHAR*[iCount];;
        int word_index;
        bool cont = true;
        
        for(int i=0, j=0; i<iCount; i++, j=0)
        {   
            word_index = 0;
    	cont = true;
            
            do{        
             
                  //Extract Fiber Name
                  while(Lines[i][j]!=',')
                  {
                       j++;
                  }
             
                  Lines[i][j]='\0';
                  Name[i] = new TCHAR[j-word_index+1];
                  lstrcpy(Name[i], Lines[i]+word_index);
                  Name[i][j-word_index] = '\0';
             
                  word_index = j;
    	      word_index++;
             
                  //Extract Beginning Fiber Count
                  while(Lines[i][j]!='-')
                  {
                       j++;
                  }
             
                  Lines[i][j] = '\0';
                  FiberCount1[i] = new TCHAR[j-word_index+1];
                  lstrcpy(FiberCount1[i], Lines[i]+word_index); 
                  FiberCount1[i][j-word_index] = '\0';
             
                  word_index = j;
    	      word_index++;
             
                  //Extract Ending Fiber Count
                  while(Lines[i][j]!=',' && Lines[i][j]!='\0')  
                  {
                       j++; 
                  }
    
    	      if(Lines[i][j]=='\0')
    
    	           cont = false;
             
                  Lines[i][j] = '\0';
                  FiberCount2[i] = new TCHAR[j-word_index+1];
                  lstrcpy(FiberCount2[i], Lines[i]+word_index);
                  FiberCount2[i][j-word_index] = '\0';
    
    	      word_index = j;
    	      word_index++;
             
              }while(cont);  
                     
        }   
        
        //Visual Verification
         for(int i=0, x=400, y=275; i<iCount; i++)
              
                 TextOut(hdc, x, y+=20, Name[i], lstrlen(Lines[i]));  
            
    }
    Last edited by The Brain; 11-08-2005 at 03:29 AM.
    • "Problem Solving C++, The Object of Programming" -Walter Savitch
    • "Data Structures and Other Objects using C++" -Walter Savitch
    • "Assembly Language for Intel-Based Computers" -Kip Irvine
    • "Programming Windows, 5th edition" -Charles Petzold
    • "Visual C++ MFC Programming by Example" -John E. Swanke
    • "Network Programming Windows" -Jones/Ohlund
    • "Sams Teach Yourself Game Programming in 24 Hours" -Michael Morrison
    • "Mathmatics for 3D Game Programming & Computer Graphics" -Eric Lengyel

  2. #2
    Yes, my avatar is stolen anonytmouse's Avatar
    Join Date
    Dec 2002
    Posts
    2,544
    Your parsing code is a little convoluted and has many possible ways that it can crash. How about replacing it with good use of the sscanf function. Something like this:
    Code:
    #include <windows.h>
    #include <new>
    #include <stdio.h>
    #include <tchar.h>
    
    struct Fiber
    {
    	TCHAR Name[100];
    	int   Count1;
    	int   Count2;
    
    	Fiber()
    	{
    		Name[0] = Count1 = Count2 = 0;
    	}
    };
    
    int main()
    {
    	char* Lines[] = {{"H1001, 1-24"}, {"A, 25-36"}, {"H1007, 36-45"}};
    	int   iCount  = 3;
    
    	Fiber* Fibers = new Fiber[iCount];
    
    	for (int i = 0; i < iCount; i++)
    	{
    		if (_stscanf(Lines[i], TEXT("%99[^,], %d-%d"),
    		              Fibers[i].Name, &Fibers[i].Count1, &Fibers[i].Count2) != 3)
    		{
    			printf("Invalid line: %s\n", Lines[i]);
    		}
    
    		// Check for correct parsing...
    		printf("%s--%d--%d--\n", Fibers[i].Name, Fibers[i].Count1, Fibers[i].Count2);
    	}
    
    	getchar();
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Capture Enter key press in EDIT Box
    By richiev in forum Windows Programming
    Replies: 4
    Last Post: 07-14-2005, 12:03 AM
  2. Appending text to an edit control
    By dit6a9 in forum Windows Programming
    Replies: 3
    Last Post: 08-13-2004, 09:52 PM
  3. New Theme
    By XSquared in forum A Brief History of Cprogramming.com
    Replies: 160
    Last Post: 04-01-2004, 08:00 PM
  4. Edit Box Questions PT. II
    By Quantrizi in forum Windows Programming
    Replies: 16
    Last Post: 08-12-2003, 10:42 PM
  5. Ok, Structs, I need help I am not familiar with them
    By incognito in forum C++ Programming
    Replies: 7
    Last Post: 06-29-2002, 09:45 PM