program corrupts in secondtime run
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5

Thread: program corrupts in secondtime run

  1. #1
    Join Date
    Oct 2008
    Posts
    59

    Question program corrupts in secondtime run

    I am doing my Project in VC++.net 2008. I want to load the datas to a List box from a text(.txt) file. So I have written the code as
    Code:
    void CNewSerialPrintDlg::OnBnClickedBtnLdfile()
    {
    	/*TODO: Add your control notification handler code here*/
    	CFileDialog FileDialog(TRUE, NULL, NULL, OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT, _T("Text File (*.txt)|*.txt||"),NULL,0,TRUE);
    	if (FileDialog.DoModal() == IDCANCEL)
    		return;
    	m_parameterlist.ResetContent();   //CListBox m_parameterlist
    	ASSERT(m_parameterlist.GetCount() == 0);
    
    	_tcscpy(sFileName, FileDialog.GetFileName());
    	LoadFile.open(sFileName, ios::in | ios::beg);    //initialised LoadFile as fstream in headerfile
    	if(!LoadFile.is_open())
    		::AfxMessageBox( _T("The file was not opened" ));
    	parList.RemoveAll();    //the datas to ListBox is in CStringList parList
    
    	while(!LoadFile.eof())
    	{
    		LoadFile.getline(store_file,255,_T('\n')); 
    		parList.AddTail(store_file); 
    		m_parameterlist.AddString((LPCTSTR)store_file);
    	}
    	LoadFile.close();
    }
    the problem am facing is: it will work in the first time. If I tried it after clearing all the datas in Listbox..data doesnot loaded..
    I've used the same code in VC++6.0 and worked... bt here not... Wat change needed...

  2. #2
    Join Date
    Apr 1999
    Posts
    27,434

    Re: program corrupts in secondtime run

    What is "sFileName"? What is "store_file"?
    Quote Originally Posted by arunkr6 View Post
    the problem am facing is: it will work in the first time. If I tried it after clearing all the datas in Listbox..data doesnot loaded..
    I've used the same code in VC++6.0 and worked... bt here not... Wat change needed...
    Just because the code "worked" in VC 6.0 doesn't mean the code was correct. You more than likely always had a bug, and now the bug has been exposed using a different compiler.

    The "fix" is to debug your application. The code you posted is not enough for someone to tell you what's the problem, since it is part of a much larger program. You more than likely corrupted memory somewhere else, and it's this function where your program finally breaks down.

    Regards,

    Paul McKenzie

  3. #3
    Join Date
    Oct 2008
    Posts
    59

    Re: program corrupts in secondtime run

    wchar_t store_file[255];
    _TCHAR sFileName[260];

    may be it have bugs... But the function is working as per my requirment in first run... later if I clear all those datas and using the above function again, data get corrupted(not seems any data loaded).

    or else can u suggest a code to save and reload the data from a listbox to a File n viceversa.

  4. #4
    Join Date
    Apr 1999
    Posts
    27,434

    Re: program corrupts in secondtime run

    Quote Originally Posted by arunkr6 View Post
    wchar_t store_file[255];
    _TCHAR sFileName[260];
    Code:
    _tcscpy(sFileName, FileDialog.GetFileName());
    Possible buffer overrun.
    may be it have bugs...
    Then there is no need for further comment. Either you fix the bugs, or you have to live with a buggy program.
    But the function is working as per my requirment in first run... later if I clear all those datas and using the above function again, data get corrupted(not seems any data loaded).
    Like I stated previously, debug your program.

    Regards,

    Paul McKenzie

  5. #5
    Join Date
    Aug 2000
    Location
    West Virginia
    Posts
    7,623

    Re: program corrupts in secondtime run

    I do not see where LoadFile is declared, so it must be global or a
    member variable. That being the case, the second time you execute
    this function, the function should not be able to even open the file
    unless the internal state of the stream is cleared.

    The best solution would be to make the fstream object local to this
    function. Anoher method would be to clear the state of the
    stream at the start of the function by adding:

    Code:
    LoadFile.clear();
    Also, looping as follows:
    Code:
    while(!LoadFile.eof())
    {
       LoadFile.getline(store_file,255,_T('\n')); 
       parList.AddTail(store_file); 
       m_parameterlist.AddString((LPCTSTR)store_file);
    }
    Usually results in an extra line being added. Better to loop as follows:
    Code:
    while(  LoadFile.getline(store_file,255,_T('\n')) )
    {
       parList.AddTail(store_file); 
       m_parameterlist.AddString((LPCTSTR)store_file);
    }

    Also, since you seem to want CString objects, it would make more
    sense to use CStdioFile and ReadString()
    Last edited by Philip Nicoletti; March 9th, 2010 at 06:04 AM.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center