-
October 15th, 2012, 03:34 AM
#1
problem in file writting with onsavedocument
Code:
void CFileMgm::write(string fname, solutions databox)
{
file1.open (fname.c_str(), ios::out);
if (file1.is_open())//if(file)+
{
file1<<"Population Size is : ";
file1<<databox.pop_size;
file1<<"Crossover Rate is : ";
file1<<databox.crossover_rate;
file1<<"Mutation Rate is : ";
file1<<databox.mutation_rate;
file1<<"Maximum fitness is: ";
file1<<databox.fitnesses[0];
file1.close();
}
else
{
AfxMessageBox("Unable to open file");
//cout<<"File does not exist";
exit(0);
}
i don't know why this code is unable to write any thing in file.
following is onsavedocument function
Code:
BOOL CTspGeneticAlgoDoc::OnSaveDocument(LPCTSTR lpszPathName)
{
string filename = (string) lpszPathName;
CFileMgm mydatafile;
mydatafile.write(filename,sol);
return CDocument::OnSaveDocument(lpszPathName);
}
-
October 15th, 2012, 03:49 AM
#2
Re: problem in file writting with onsavedocument
Are you sure it doesn't write or the file content is overwritten by CDocument::OnSaveDocument(lpszPathName)?
But a breakpoint before the call to CDocument::OnSaveDocument(lpszPathName) and check the content of your file.
-
October 15th, 2012, 03:51 AM
#3
Re: problem in file writting with onsavedocument
Originally Posted by tspga
Code:
BOOL CTspGeneticAlgoDoc::OnSaveDocument(LPCTSTR lpszPathName)
{
string filename = (string) lpszPathName;
CFileMgm mydatafile;
mydatafile.write(filename,sol);
return CDocument::OnSaveDocument(lpszPathName);
}
Why do you use a C-style cast there? You should not use C-style casts at all in a C++ program. See http://www.cplusplus.com/doc/tutorial/typecasting/
In this case, no cast is needed at all. If your project is compiled as ANSI, you can just assign the argument to a std::string. If it is compiled as unicode, then you need to convert the string using something like WideCharToMultiByte.
Cheers, D Drmmr
Please put [code][/code] tags around your code to preserve indentation and make it more readable.
As long as man ascribes to himself what is merely a posibility, he will not work for the attainment of it. - P. D. Ouspensky
-
October 15th, 2012, 04:16 AM
#4
Re: problem in file writting with onsavedocument
Originally Posted by tspga
i don't know why this code is unable to write any thing in file.
Did you debug your code?
Code:
BOOL CTspGeneticAlgoDoc::OnSaveDocument(LPCTSTR lpszPathName)
{
string filename = (string) lpszPathName;
So what happens if your application is compiled as UNICODE? You should have seen that right away that "filename" is incorrect if you have a UNICODE app.
The C++ std::string is a char based type. It knows nothing about two-byte character strings "automatically" without you having to write code for it to do so. If lpszPathName is indeed a pointer to a two-byte character string (UNICODE string), then you would see that filename would be equal to a partial string (usually just the first character of lpszPathName), and that's it.
The easiest thing for you to do is to use CString, since that will be consistent with the build type (ANSI/UNICODE). If you must insist on using the C++ string class, then you must write code to either use std::string for ANSI builds, or std::wstring for UNICODE builds, or use the conversion functions that D_Drmmr pointed out.
Regards,
Paul McKenzie
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
|