Quantcast
Channel: CodeGuru Forums - Visual C++ Programming
Viewing all articles
Browse latest Browse all 3027

MFC and STD::STRING not compatable ???

$
0
0
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);


       
               
}


Viewing all articles
Browse latest Browse all 3027

Latest Images

Trending Articles



Latest Images