-
March 9th, 2010, 01:23 AM
#1
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...
-
March 9th, 2010, 01:29 AM
#2
Re: program corrupts in secondtime run
What is "sFileName"? What is "store_file"?
Originally Posted by arunkr6
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
-
March 9th, 2010, 01:35 AM
#3
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.
-
March 9th, 2010, 05:11 AM
#4
Re: program corrupts in secondtime run
Originally Posted by arunkr6
wchar_t store_file[255];
_TCHAR sFileName[260];
Code:
_tcscpy(sFileName, FileDialog.GetFileName());
Possible buffer overrun.
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
-
March 9th, 2010, 06:09 AM
#5
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:
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 07: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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|