CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 21

Thread: MFC and STD::STRING not compatable ???

  1. #1
    Join Date
    Jun 2004
    Posts
    1,352

    MFC and STD::STRING not compatable ???

    Im maintaining a legacy app in VC6 aka VS6 (release date 1998, wikipedia), don't have time to upgrade it now. I have found a previous bug in this version I think I found another.


    I'm trying to write objects to disk and read them back. This code works in a counsel app WITHOUT MFC. I can't get it to work in an MFC SDI FORVIEW app.

    I created an SDI FORMVIEW APP and place a Listbox and Button on the screen. I placed the following code in the button message handler. This is just a test App to test the code.


    Note the list function takes a pointer to the listbox so as you all know you have to create a control variable in classwizard to the listbox

    In a counsel app I can add and read objects fine, but in the MFC version all I get is junk in the list box and message box.

    Can you guys try it in a newer Visual Studio? Or am I overlooking something? I don't use std::string much as I'm more of a CString guy, but I'm very interested in writing/reading std::cstring obects to file since I haven't found a way to write CString objects to file and accesses the files in random access mode.


    Thanks in advance.

    I have version 2003 profesional but haven't installed it. If some one has it please try it on that verision. It's a big project and porting it might cause issues.


    Code:
    //top of file
    #include "stdafx.h"
    
    //......
    
    
    #include <string>
    #include <iostream>
    #include <fstream>
    
    using namespace std;
    
    void MYFORMVIEW::OnButtonDbCpp() 
    {
    
    
    
    
    
    	class Person{
    
    		public:
    	
    		std::string m_Name;
    		int m_Age;
    		int m_Ratings;
    
    
    		void InitPerson( std::string Name, int Age, int Ratings){
    				m_Name = Name;
    				m_Age  = Age;
    				m_Ratings = Ratings;
    		};
    	};
    	class Person_IO{
    
    	public:
    
    		void WritePerson( Person P, std::string Name, int Age, int Ratings){
    				P.m_Name = Name;
    				P.m_Age  = Age;
    				P.m_Ratings = Ratings;
    
    				ofstream outfile;
    
    				outfile.open("persfile.dat", ios::app | ios::binary);
    
    				
                                    //file.read((char*)&obj, sizeof(obj)); //doesn't work either	
    				outfile.write(reinterpret_cast<char*>(&P),sizeof(P));
    
    				outfile.flush(); 
    
    				outfile.close();
    		}
    
    		void ReadAllPersons( Person P, CListBox* pList){
    				//P.m_Name = Name;
    				//P.m_Age  = Age;
    				//P.m_Ratings = Ratings;
    
    				ifstream infile;
    
    				infile.open("persfile.dat",  ios::binary);
    
    				
    				while(1){
    					 
                                            //file.read((char*)&obj, sizeof(obj)); //doesn't work either	
    					infile.read(reinterpret_cast<char*>(&P),sizeof(P));
    					if(infile.eof()) break;
    					pList->AddString(P.m_Name.c_str());
    
    					::AfxMessageBox(P.m_Name.c_str());
    				};
    
    				infile.close();
    
    				
    		}
    
    
    
    
    	};
    
    	Person   Per;
    	Person_IO P_IO;
    
    	P_IO.WritePerson( Per, "Joe Baker",42, 125);
    	P_IO.WritePerson( Per, "Sam Valet",52, 12);
    	P_IO.WritePerson( Per, "Don Tracy",55, 912);
    
    
    	P_IO.ReadAllPersons( Per, &m_CList1);
    
    
    	
    		
    }
    Rate this post if it helped you.

  2. #2
    VictorN's Avatar
    VictorN is online now Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    19,759

    Re: MFC and STD::STRING not compatable ???

    Quote Originally Posted by ADSOFT View Post
    ... but in the MFC version all I get is junk in the list box and message box.

    Can you guys try it in a newer Visual Studio? Or am I overlooking something? I don't use std::string much as I'm more of a CString guy, but I'm very interested in writing/reading std::cstring obects to file since I haven't found a way to write CString objects to file and accesses the files in random access mode.
    You didn't show your MFC "version" that doesn't work.

    As for writing/reading with MFC CString you can use CStdioFile class.
    Victor Nijegorodov

  3. #3
    Join Date
    Jun 2004
    Posts
    1,352

    Re: MFC and STD::STRING not compatable ???

    That is the MFC version, I'm writing to a ListBox and using AfxMessageBox ???

    ... when I ment writing CStrings , I'm ment writing a class of with many CStrings in them and and writing the whole object.
    Rate this post if it helped you.

  4. #4
    VictorN's Avatar
    VictorN is online now Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    19,759

    Re: MFC and STD::STRING not compatable ???

    Quote Originally Posted by ADSOFT View Post
    in the MFC version all I get is junk in the list box and message box.
    The code you have shown is an ANSI one. Is your project also ANSI/MBCS or it is UNICODE?
    Victor Nijegorodov

  5. #5
    Join Date
    Jun 2004
    Posts
    1,352

    Re: MFC and STD::STRING not compatable ???

    The code that I'm showing is the code in an OnButton message handler.
    Code:
    void MYFORMVIEW::OnButtonDbCpp() 
    {

    these header files where necessary to compile std::string

    Code:
    #include "stdafx.h"
    
    #include <string>
    #include <iostream>
    #include <fstream>
    
    using namespace std;
    note that I'm including "stdafx.h" on the first line of my *.CPP file?
    Rate this post if it helped you.

  6. #6
    Join Date
    Jun 2004
    Posts
    1,352

    Re: MFC and STD::STRING not compatable ???

    I created an SDI FORMVIEW APP and place a Listbox and Button on the screen. I placed the following code in the button message handler. This is just a test App to test the code.


    ...the code that I'm showing is part of my SDI FORMVIEW APP.
    Rate this post if it helped you.

  7. #7
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,193

    Re: MFC and STD::STRING not compatable ???

    Code:
    outfile.write(reinterpret_cast<char*>(&P),sizeof(P));
    Sorry, but that's a C++ no-no where the struct/class P uses dynamic memory (eg std::string). All this does is to copy the contents of the memory used for that instance of the class/struct including the memory location(s) used at the time for dynamic classes such as std::string. It does not copy the data pointed to by the memory location used within these dynamic classes (ie doesn't copy the actual data used). With std::string and if the small buffer is being used (SSO), you might get away with this (but very fragile code!), but if the SB is not used, then you won't actually save the data - just the memory location used. There is no C++ standard way of writing a class/struct to a file that uses dynamic memory. This is one time when using a c_style array is 'better' than using a std::string in a class/struct as the contents of arrays are written to the file.
    Last edited by 2kaud; May 6th, 2020 at 02:13 PM.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++17 Compiler: Microsoft VS2019 (16.7.0)

  8. #8
    Join Date
    Jun 2004
    Posts
    1,352

    Re: MFC and STD::STRING not compatable ???

    So why does it work in a consul app.

    I got the example from a C++ book.
    Rate this post if it helped you.

  9. #9
    Join Date
    Jun 2004
    Posts
    1,352

    Re: MFC and STD::STRING not compatable ???

    Check this link out. If it's against your policy you can delete my post.

    But read what it says:

    https://www.geeksforgeeks.org/readwr...fromto-file-c/
    Rate this post if it helped you.

  10. #10
    VictorN's Avatar
    VictorN is online now Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    19,759

    Re: MFC and STD::STRING not compatable ???

    What is a "consul app"?
    What is your "C++ book"?
    Victor Nijegorodov

  11. #11
    Join Date
    Jun 2004
    Posts
    1,352

    Re: MFC and STD::STRING not compatable ???

    I ment Console app.

    Book: Object Oriented Programing in C++ : Third Edition, By Robert Lafore.

    He has several examples of writing an object to file, as the above link does as well.
    Rate this post if it helped you.

  12. #12
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,318

    Re: MFC and STD::STRING not compatable ???

    Quote Originally Posted by ADSOFT View Post
    So why does it work in a consul app.

    I got the example from a C++ book.
    Because maybe the console app is compiled for MBCS but the MFC app is compiled for UNICODE?

  13. #13
    Join Date
    Jun 2004
    Posts
    1,352

    Re: MFC and STD::STRING not compatable ???

    That's what I was hoping you guys could answer?

    I think it could be that my Compiler is just too old? VS6 ??

    If one of you guys with a newer compiler could create an SDI FORVIEW APP as I outlined and pop the code in there, that would be an easy way to find out. Thats how we confirmed the previous limitation in found in VS6.

    I forgot the name of the poster who did that.
    Rate this post if it helped you.

  14. #14
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,193

    Re: MFC and STD::STRING not compatable ???

    Quote Originally Posted by ADSOFT View Post
    Check this link out. If it's against your policy you can delete my post.

    But read what it says:

    https://www.geeksforgeeks.org/readwr...fromto-file-c/
    That example will only perhaps work if the std::string Small Buffer (Small/Short String Optimization [SSO]) is being used. It is a typical example of 'bad practice' that may perhaps if the planets are in alignment with the moon give the impression of working. That method should not be used for any struct/class that contains dynamic memory pointers (as std::string does) - or virtual methods where a v-table is used. Just because it is in a book/post doesn't make it right! This is a so-called shallow copy, whereas when dynamic memory is used a deep copy is required.

    Book: Object Oriented Programing in C++ : Third Edition, By Robert Lafore.
    The later 4th edition was published in 2001. This book is so old and out of date that IMO its best use is as a doorstop. Please, please, get an up-to-date book that covers at least C++17.

    PS. In C++, just because with one set of inputs a program appears to work, that does not mean that the program is correct!

    PPS. AFAIK, MFC CString doesn't use Small String Optimisation (SSO), so this method will never work with CString.
    Last edited by 2kaud; May 6th, 2020 at 02:14 PM. Reason: PPS
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++17 Compiler: Microsoft VS2019 (16.7.0)

  15. #15
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,193

    Re: MFC and STD::STRING not compatable ???

    For MS VS2019, the size of the std::string small buffer is 16. So if less than 16 ANSI chars are stored, the small buffer will be used and so that code will give the impression of working - but it is fragile and it won't work with a std::string holding more than 16 chars (or what this number is for the particular compiler you are using).
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++17 Compiler: Microsoft VS2019 (16.7.0)

Page 1 of 2 12 LastLast

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)