Why isn't this working?

This is a discussion on Why isn't this working? within the Windows Programming forums, part of the Platform Specific Boards category; Code: int SaveClientInfo(HWND DialogHandle) { Client Cliente; HWND NameEditBoxHwnd; HWND NameEditBox; char * NameOfFile; char *FileToSave; char *NameOfClient; int iCount; ...

  1. #1
    left crog... back when? incognito's Avatar
    Join Date
    Oct 2001
    Posts
    1,427

    Why isn't this working?

    Code:
    
    int SaveClientInfo(HWND DialogHandle)
    
    {
    
    	Client Cliente;
    	HWND NameEditBoxHwnd;
    	HWND NameEditBox;
    	char * NameOfFile;
    	char *FileToSave;
    	char *NameOfClient;
    	int iCount;
    
    	NameEditBoxHwnd=GetDlgItem(DialogHandle, IDC_NOMBRE);
    	iCount=GetWindowTextLength(NameEditBoxHwnd);
    	
    	//we incremente iCount to accomodate for the NULL character
    	NameOfFile=new char [sizeof(iCount)];
    	GetWindowText(NameEditBoxHwnd,NameOfFile,iCount+1);
    	FileToSave= new char [sizeof(NameOfFile)];
    
    	
        OpenFileName.hwndOwner         = NULL;
        OpenFileName.lpstrFile         = NameOfFile;
    	OpenFileName.lpstrFileTitle	   =FileToSave;
        OpenFileName.Flags			   = OFN_EXPLORER | OFN_PATHMUSTEXIST | OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT ;
    	GetSaveFileName (&OpenFileName);
    
    	
    
    	NameEditBox=GetDlgItem(DialogHandle, IDC_NOMBRE);
    	iCount=GetWindowTextLength(NameEditBox);
    	
    	//we incremente iCount to accomodate for the NULL character
    	NameOfClient=new char [sizeof(iCount)];
    	GetWindowText(NameEditBox,NameOfClient,iCount+1);
    
    	Cliente.SetName(NameOfClient);
    
    
    	ofstream fout(OpenFileName.lpstrFileTitle);
    	fout.write((char*)&Cliente,sizeof Cliente);
    	fout.close();
    
    	delete NameOfFile;
    	delete FileToSave;
    	delete NameOfClient;
    
    	NameOfFile=NULL;
    	FileToSave=NULL;
    	NameOfClient=NULL;
    
    	/* I am having some serious problems with this stuff
    	upon closing the window the program crashes....
    	how would I free the pointer memory? 
    
    	delete Pointer;
    	Pointer=NULL;
    
    	Should  I do this here? 
    	Remember taht this is called more than once thruout the program
    	I think that it's because I am not freeing memory properly
    
       */
    
    
    
    
    
    
    	return Success;
    
    	
    	
    }
    There are some real morons in this world please do not become one of them, do not become a victim of moronitis. PROGRAMMING IS THE FUTURE...THE FUTURE IS NOW!!!!!!!!!

    "...The only real game I thank in the world is baseball..." --Babe Ruth

    "Life is beautiful"-Don Corleone right before he died.

    "The expert on anything was once a beginner" -Baseball poster I own.


    Left cprog on 1-3-2005. Don't know when I am coming back. Thanks to those who helped me over the years.

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,499
    > NameOfFile=new char [sizeof(iCount)];
    Which is pretty much the same as
    NameOfFile=new char [sizeof(int)];

    You probably meant
    NameOfFile=new char [iCount];

    Or maybe
    NameOfFile=new char [iCount+1];

    The other sizeof() is probably wrong as well for much the same reason
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Function not working
    By sloopy in forum C Programming
    Replies: 31
    Last Post: 11-12-2005, 07:08 PM
  2. Program Not working Right
    By raven420smoke in forum C++ Programming
    Replies: 2
    Last Post: 09-16-2005, 03:21 AM
  3. Trying to eject D drive using code, but not working... :(
    By snowfrog in forum C++ Programming
    Replies: 3
    Last Post: 05-07-2005, 07:47 PM
  4. x on upper right corner not working
    By caduardo21 in forum Windows Programming
    Replies: 1
    Last Post: 02-20-2005, 07:35 PM
  5. cygwin -> unix , my code not working properly ;(
    By CyC|OpS in forum C Programming
    Replies: 4
    Last Post: 05-18-2002, 04:08 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21