CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4

Thread: problem in file writting with onsavedocument

  1. #1
    Join Date
    Jul 2012
    Posts
    42

    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);
    }

  2. #2
    Join Date
    Oct 2002
    Location
    Timisoara, Romania
    Posts
    14,360

    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.
    Marius Bancila
    Home Page
    My CodeGuru articles

    I do not offer technical support via PM or e-mail. Please use vbBulletin codes.

  3. #3
    Join Date
    Jul 2005
    Location
    Netherlands
    Posts
    2,042

    Re: problem in file writting with onsavedocument

    Quote Originally Posted by tspga View Post
    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

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

    Re: problem in file writting with onsavedocument

    Quote Originally Posted by tspga View Post
    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
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width




On-Demand Webinars (sponsored)