Problem on using VC++ 2008
I have worked on VC++6.0,
Now first time, am on VC++.Net 2008 platform....
Here I want to Save some datas of a Listbox to a file... So I have tried the following code
CFileDialog FileDialog(FALSE, "txt","downloader", OFN_CREATEPROMPT|OFN_HIDEREADONLY |
OFN_SHOWHELP|OFN_OVERWRITEPROMPT|OFN_LONGNAMES, "Save the file as (*.txt)|*.txt||");
if (FileDialog.DoModal() == IDCANCEL)
return;
char sFileName[255];
BeginWaitCursor();
UpdateData(true);
strcpy(sFileName, FileDialog.GetFileName());
sourceFile.open(sFileName,ios::out);
if(!sourceFile.is_open())
AfxMessageBox("file for saving not opened");
else
for (POSITION Pos1 = pFrame->Batch_boardList.GetHeadPosition(); Pos1 != NULL;)
sourceFile <<pFrame->Batch_boardList.GetNext(Pos1)+'\n';
sourceFile.close();
This was the code I have used in VC++ 6.0,
On the same routine, I've tried it on 2008... But the CFileDialog class itself creates some errors.. n solved.(without error)
Here I can't copy "strcpy(sFileName, FileDialog.GetFileName());"
The error noted as The Parameter 2 cannot convert from CString to char*..
Plz explain how it is so?? and how can I solve it??
Re: Problem on using VC++ 2008
Probably a UNICODE - MCBS issue. Try:
Code:
CFileDialog FileDialog(FALSE, "txt","downloader", OFN_CREATEPROMPT|OFN_HIDEREADONLY |
OFN_SHOWHELP|OFN_OVERWRITEPROMPT|OFN_LONGNAMES, "Save the file as (*.txt)|*.txt||");
if (FileDialog.DoModal() == IDCANCEL)
return;
_TCHAR sFileName[255];
BeginWaitCursor();
UpdateData(true);
_tcscpy(sFileName, FileDialog.GetFileName());
sourceFile.open(sFileName,ios:ut);
if(!sourceFile.is_open())
AfxMessageBox("file for saving not opened");
else
for (POSITION Pos1 = pFrame->Batch_boardList.GetHeadPosition(); Pos1 != NULL
sourceFile <<pFrame->Batch_boardList.GetNext(Pos1)+'\n';
sourceFile.close();
Re: Problem on using VC++ 2008
I'd do some more changes:
Code:
CFileDialog FileDialog(FALSE, _T("txt"), _T("downloader"), OFN_CREATEPROMPT|OFN_HIDEREADONLY |
OFN_SHOWHELP|OFN_OVERWRITEPROMPT|OFN_LONGNAMES, _T("Save the file as (*.txt)|*.txt||)");
if (FileDialog.DoModal() == IDCANCEL)
return;
_TCHAR sFileName[MAX_PATH]; // 260, not 255
BeginWaitCursor();
UpdateData(true);
_tcscpy(sFileName, FileDialog.GetFileName());
sourceFile.open(sFileName, ios:ut);
if(!sourceFile.is_open())
AfxMessageBox( _T("file for saving not opened"));
else
for (POSITION Pos1 = pFrame->Batch_boardList.GetHeadPosition(); Pos1 != NULL
sourceFile << pFrame->Batch_boardList.GetNext(Pos1) + _T('\n');
sourceFile.close();
Re: Problem on using VC++ 2008
Thank u victor & Hox...
I've cleared the problem.... n modified it like this...
#include<ifstream>;
using namespace std;
ifstream sourcefile;
CFileDialog FileDlg(FALSE, _T("txt"),_T("ParameterFile"), OFN_CREATEPROMPT|OFN_HIDEREADONLY |
OFN_SHOWHELP|OFN_OVERWRITEPROMPT|OFN_LONGNAMES, _T("Save the file as(*.txt)|*.txt||"),NULL,0,TRUE);
if (FileDlg.DoModal() == IDCANCEL)
return;
_TCHAR sFileName[255];
BeginWaitCursor();
UpdateData(true);
_tcscpy(sFileName, FileDlg.GetFileName());
sourcefile.open(sFileName,ios::out);
if(!sourcefile.is_open())
AfxMessageBox(_T("file for saving not opened"));
else
for (POSITION Pos1 = parList.GetHeadPosition(); Pos1 != NULL;)
sourcefile<< parList.GetNext(Pos1)+'\n';
sourcefile.close();
Now the file is created.... but when it is opening, the data in the listbox is not there... Some unknown Hexadecimal values are seeing there...
How to resolve it??
Re: Problem on using VC++ 2008
1) Does GetNext() return a CString ? Maybe the streams do not understand how
to output a CString and it is using the void* conversion (basically, printing
out the address of the object). Try casting to LPCTSTR
Code:
sourcefile<< (LPCTSTR)parList.GetNext(Pos1)<< '\n';
2) Depending an compiler options, you might need to change to
wifstream (also why istream types if outputing ?).
3) If GetNext() does return a CString, it would make more sense to use the
CStdioFile class which can directly read/write Cstring objects.
Re: Problem on using VC++ 2008
Quote:
Originally Posted by
arunkr6
... Now the file is created.... but when it is opening, the data in the listbox is not there... Some unknown Hexadecimal values are seeing there...
How to resolve it??
What listbox do you mean? I don't see any... :confused:
Re: Problem on using VC++ 2008
Sorry Victor, for not providing sufficient data...
I've entered the datas of Listbox to a CStringList parList.
GetNext() returns CString itself... I've verified and it gives the exact data in particular CStringList. I hope the prblm is in the line 'sourcefile<<Parlist.Get....... Here the data is not loading to fstream sourcefile.
I've tried wifstream and ifstream.It gives an error as binary '<<' no operator found which takes a left hand operand of type std::wifstream.. So I've used fstream and it's working...
The problem still exists.....As the data written is something hexvalue(hope the address)....
Re: Problem on using VC++ 2008
Did you try the cast as I posted earlier ?
Re: Problem on using VC++ 2008
Is parList an CAtlList? How is it defined (what is the type parameter)?
Re: Problem on using VC++ 2008
Ya... I've casted it... Bt it doesn't resemble any change..same effect..
parList is assigned as CStringList only..
Can you plz give me a code to save and load the datas(user entering in fields and it combines to a ListBox) to a file....This is my requirement.. By using the above code I've done it in VC++ 6.0... Bt here it failed,,.!!
Re: Problem on using VC++ 2008
Quote:
Originally Posted by
arunkr6
Ya... I've casted it... Bt it doesn't resemble any change..same effect..
parList is assigned as CStringList only..
Can you plz give me a code to save and load the datas(user entering in fields and it combines to a ListBox) to a file....This is my requirement.. By using the above code I've done it in VC++ 6.0... Bt here it failed,,.!!
Why don't you just post your complete code and we'll suggest how to fix your code?
Re: Problem on using VC++ 2008
the exact code has pasted already..plz check above...
That's the code I've written for this function(Save to a file).
Re: Problem on using VC++ 2008
Quote:
Originally Posted by
arunkr6
the exact code has pasted already..plz check above...
That's the code I've written for this function(Save to a file).
Use code tags & more people will read it.
Re: Problem on using VC++ 2008
Quote:
Originally Posted by
arunkr6
the exact code has pasted already..plz check above...
The code has smiley faces and is unformatted. Why not fix it the formatting first by using code tags?
Regards,
Paul McKenzie
Re: Problem on using VC++ 2008
Quote:
Originally Posted by
arunkr6
the exact code has pasted already..plz check above...
That's the code I've written for this function(Save to a file).
You talk about the data not getting into a listbox, but there isn't any listbox code. How about posting the complete code?