-
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?
-
Re: Problem on using VC++ 2008
Sorry I will explain it again.....
Code:
#include<ifstream>;
using namespace std;
CStringList parList;
fstream 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||"));
if (FileDlg.DoModal() == IDCANCEL)
return;
_TCHAR sFileName[MAX_PATH];
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<< (LPCTSTR)parList.GetNext(Pos1)+_T('\n');
sourcefile.close();
This is my code I've written for the function to save the data to a file.
The CStringList parList contains the datas(Which are enthered by users) from a ListBox(this has done on another function
already). I am trying to load it on the fstream sourcefile.
Code:
parList.GetNext(pos1)
returns the exact value entered.But it is not loading to the file.
When am opening the file,it has some Hex values there(hope, these r address).
How to fix it???
-
Re: Problem on using VC++ 2008
You have the following code ... does it even compiile ?
Code:
for (POSITION Pos1 = parList.GetHeadPosition(); Pos1 != NULL);
sourcefile<< (LPCTSTR)parList.GetNext(Pos1)+_T('\n');
-
Re: Problem on using VC++ 2008
Ya.. I've compiled it.....
This only returns the file with some unknown data..
-
Re: Problem on using VC++ 2008
Is this a UNICODE build? Does the CString member of CStringList evaluate to CStringA or CStringW? Check with the debugger if you have to.
-
Re: Problem on using VC++ 2008
1) I do not think that it should compile
2) Do you really want a semi-colon at the end of the for statement.
-
Re: Problem on using VC++ 2008
Quote:
Originally Posted by
Philip Nicoletti
3) Why do you have a semi-colon at the end of the for statement?
Missed that. Good catch!
It should compile, but the loop ends at the semicolon.
Pos is normally incremented as part of GetNext().
Edit: It's missing a semicolon in the for(;;) loop so it should NOT compile. I stand corrected.
-
Re: Problem on using VC++ 2008
Sorry... That was my mistake while copying not in compilation...
I was copying it from the already posted codes..so there some smileys came n I just cleared without much care..
The code I've copied is
Code:
for(Position Pos1=ParList.GetHeadPosition();Pos1!=NULL;)
And it has compiled without any error..
I still conusing that my error is in part of fstream..
Shd I take any else precautions b4 using fstream??
-
Re: Problem on using VC++ 2008
You need to determine what kind of string (wide or multi-byte) you are getting form you CStringList. Just casting the return value to LPCTSTR won't cut it if the fundamental type is wrong (wchar_t * v. char *).
-
Re: Problem on using VC++ 2008
It is CString I hope..
Sorry that, am not @ my work place nw...and I can't access internet from ther..
So can't confirm nw.. Bt the return value is a CString only. I've verified it by returning on CString..
done as
Code:
CString ss;
ss=parList.GetNext();
::AfxMessageBox(ss);
and it get's the exact data, which i needed..
??
-
Re: Problem on using VC++ 2008
"CString" is a macro that can evaluate to either CStringA or CStringW depending on whether you are building a UNICODE app (whether _UNICODE is defined) or not.
-
Re: Problem on using VC++ 2008
Quote:
Originally Posted by
arunkr6
It is CString I hope..
Sorry that, am not @ my work place nw...and I can't access internet from ther..
Not good... :sick:
You was many times asked whether your current build is UNICODE or ANSI. You haven't answer yet...
Note that if it is a UNICODE one then you must use wfstream instead of fstream
-
Re: Problem on using VC++ 2008
I havn't defined it as UNICODE.
-
Re: Problem on using VC++ 2008
Apps built by the AppWizard should default to UNICODE in VS2005 and later.
-
Re: Problem on using VC++ 2008
So what should I do further??
I need ASCI values.
So I have to change as wfstream right??
lemme compile it n get u the response...thank u...
-
Re: Problem on using VC++ 2008
thank u..
it got worked.. i jus changed 2 wfstream.... got the problems,,
thank u..all