Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. hmm... Now why did this work?

hmm... Now why did this work?

Scheduled Pinned Locked Moved C / C++ / MFC
helplinuxquestion
8 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • U Offline
    U Offline
    UserNameless
    wrote on last edited by
    #1

    I wonder if someone can help me understand why, when I made changes to the following code worked (more like, gave me the result that I wanted) and if I did something wrong which can come back to haunt me later on (don't bash me :~ ) Original Code:

    CFileDialog fOpenDlg(true,NULL,NULL,NULL,_T("All Files (*.*)|*.*||"),this);
    CString fName = NULL;
    string name;
    fName = fOpenDlg.GetFileName();
    name = fName;

    Error: error C2679: binary '=' : no operator found which takes a right-hand operand of type 'CString' (or there is no acceptable conversion) now the Modified Code (not the final one):

    CFileDialog fOpenDlg(true,NULL,NULL,NULL,_T("All Files (*.*)|*.*||"),this);
    CString fName = NULL;
    string name;
    fName = fOpenDlg.GetFileName();
    name = *fName;

    Observation: if I open a file named test.txt fName gets set to test, and name gets set to t Final code (giving me the result I wanted, discovered accidentally while applying it elsewhere):

    CFileDialog fOpenDlg(true,NULL,NULL,NULL,_T("All Files (*.*)|*.*||"),this);
    CString fName = NULL;
    string name;
    fName = fOpenDlg.GetFileName();
    name = (fName+"");

    Observation: now if I open a file named test.txt, fName gets set to test, and name ALSO gets set to test. I've been wondering how that +"" changed the behavior Thanks

    R D 2 Replies Last reply
    0
    • U UserNameless

      I wonder if someone can help me understand why, when I made changes to the following code worked (more like, gave me the result that I wanted) and if I did something wrong which can come back to haunt me later on (don't bash me :~ ) Original Code:

      CFileDialog fOpenDlg(true,NULL,NULL,NULL,_T("All Files (*.*)|*.*||"),this);
      CString fName = NULL;
      string name;
      fName = fOpenDlg.GetFileName();
      name = fName;

      Error: error C2679: binary '=' : no operator found which takes a right-hand operand of type 'CString' (or there is no acceptable conversion) now the Modified Code (not the final one):

      CFileDialog fOpenDlg(true,NULL,NULL,NULL,_T("All Files (*.*)|*.*||"),this);
      CString fName = NULL;
      string name;
      fName = fOpenDlg.GetFileName();
      name = *fName;

      Observation: if I open a file named test.txt fName gets set to test, and name gets set to t Final code (giving me the result I wanted, discovered accidentally while applying it elsewhere):

      CFileDialog fOpenDlg(true,NULL,NULL,NULL,_T("All Files (*.*)|*.*||"),this);
      CString fName = NULL;
      string name;
      fName = fOpenDlg.GetFileName();
      name = (fName+"");

      Observation: now if I open a file named test.txt, fName gets set to test, and name ALSO gets set to test. I've been wondering how that +"" changed the behavior Thanks

      R Offline
      R Offline
      Rajesh R Subramanian
      wrote on last edited by
      #2

      Are you doing a Unicode or an ANSI build?! This works for me, and I am doing a Unicode build.

      std::wstring cppStr;
      CFileDialog cfd(true);
      if(cfd.DoModal()==IDOK) //I never see a cfd.DoModal() in your code! But you've called cfd.GetFileName(), which is pointless.
      {
      cppStr = cfd.GetFileName();
      wcout<
      I hope you can figure out your mistakes.

      Also, I don't understand why do you need to use std::string if you're already using MFC. Why not use CString?! :confused:

      It is a crappy thing, but it's life -^ Carlo Pallini

      U 1 Reply Last reply
      0
      • R Rajesh R Subramanian

        Are you doing a Unicode or an ANSI build?! This works for me, and I am doing a Unicode build.

        std::wstring cppStr;
        CFileDialog cfd(true);
        if(cfd.DoModal()==IDOK) //I never see a cfd.DoModal() in your code! But you've called cfd.GetFileName(), which is pointless.
        {
        cppStr = cfd.GetFileName();
        wcout<
        I hope you can figure out your mistakes.

        Also, I don't understand why do you need to use std::string if you're already using MFC. Why not use CString?! :confused:

        It is a crappy thing, but it's life -^ Carlo Pallini

        U Offline
        U Offline
        UserNameless
        wrote on last edited by
        #3

        oh, i do do a "DoModal()==OK", I thought it didn't concern with the reason the strings behave (which I guess is what I wanted to know). and you are saying I don't need to use std::string with MFC eh? hmm... interesting point, I was basing that part of my code on basic- opening files, loading the data into buffer, copying the data into another file sort of operations. btw, on a new note, how does working with MFC change the need to use different types of variables (in this case, work with CStrings and not with CStrings and std::strings)? thanks for the patience mate p.s. oh there's another part of my code which isn't working (based on the examples I read on CFileDialog examples. I wanted to set the default directory to my C: drive when I click the Open button. my code for it is:

        fOpenDlg.m_pOFN->lpstrInitialDir = (LPCWSTR)("C:");

        which doesn't seem to be working... again im guessing on the way strings behave? (boy my string concepts are weak :(( )

        R 1 Reply Last reply
        0
        • U UserNameless

          oh, i do do a "DoModal()==OK", I thought it didn't concern with the reason the strings behave (which I guess is what I wanted to know). and you are saying I don't need to use std::string with MFC eh? hmm... interesting point, I was basing that part of my code on basic- opening files, loading the data into buffer, copying the data into another file sort of operations. btw, on a new note, how does working with MFC change the need to use different types of variables (in this case, work with CStrings and not with CStrings and std::strings)? thanks for the patience mate p.s. oh there's another part of my code which isn't working (based on the examples I read on CFileDialog examples. I wanted to set the default directory to my C: drive when I click the Open button. my code for it is:

          fOpenDlg.m_pOFN->lpstrInitialDir = (LPCWSTR)("C:");

          which doesn't seem to be working... again im guessing on the way strings behave? (boy my string concepts are weak :(( )

          R Offline
          R Offline
          Rajesh R Subramanian
          wrote on last edited by
          #4

          You can even write inline assembly to manipulate everything. But the question is: Do you really need to do it, given that you're already using a framework which makes things much more simpler? I am saying that you should not be using std::string in an MFC program. But given that CString does everything that you need to, I simply fail to see any justification for using std::string. (OK, you're manipulating files? Then you could use CFile or CStdioFile or ...). And why do I use MFC when I can do everything with vanilla C or C++? Because I'd like to leave for home at 5. I may use something like an std::string within an MFC program very rarely (may be I'm using some third party code and a function expects a reference to an std::string, or whatever)...

          sadman89 wrote:

          drive when I click the Open button. my code for it is:

          You are again providing code in bits and pieces. That works for me and I can't tell you why it didn't work in your case unless I can see the whole of your code (relevant code, please). See if this helps:

          CFileDialog cfd(true);
          CString szStr;
          cfd.m_pOFN->lpstrInitialDir = _T("C:\\");
          if(cfd.DoModal == IDOK)
          {
          szStr = cfd.GetFileName();
          AfxMessageBox(szStr);
          }

          It is a crappy thing, but it's life -^ Carlo Pallini

          U 1 Reply Last reply
          0
          • R Rajesh R Subramanian

            You can even write inline assembly to manipulate everything. But the question is: Do you really need to do it, given that you're already using a framework which makes things much more simpler? I am saying that you should not be using std::string in an MFC program. But given that CString does everything that you need to, I simply fail to see any justification for using std::string. (OK, you're manipulating files? Then you could use CFile or CStdioFile or ...). And why do I use MFC when I can do everything with vanilla C or C++? Because I'd like to leave for home at 5. I may use something like an std::string within an MFC program very rarely (may be I'm using some third party code and a function expects a reference to an std::string, or whatever)...

            sadman89 wrote:

            drive when I click the Open button. my code for it is:

            You are again providing code in bits and pieces. That works for me and I can't tell you why it didn't work in your case unless I can see the whole of your code (relevant code, please). See if this helps:

            CFileDialog cfd(true);
            CString szStr;
            cfd.m_pOFN->lpstrInitialDir = _T("C:\\");
            if(cfd.DoModal == IDOK)
            {
            szStr = cfd.GetFileName();
            AfxMessageBox(szStr);
            }

            It is a crappy thing, but it's life -^ Carlo Pallini

            U Offline
            U Offline
            UserNameless
            wrote on last edited by
            #5

            ok so i get your point with using CString over std::string in MFCs, however I had to do some operations like name.append(".txt" which don't seem to be working if I declare name as a CString variable. and that m_pOFN->lpstrInitialDir = _T("C:\\"); also doesn't seem to be working. ok so here's ALL (I hope) relavent code related to that function:

            ::OnBnClickedBrowse() //When you click the open button
            {
            CFileDialog fOpenDlg(true,NULL,NULL,NULL,_T("All Files (*.*)|*.*||"),this);
            fOpenDlg.m_pOFN->lpstrTitle= _T("Open File");
            fOpenDlg.m_pOFN->lpstrInitialDir = _T("C:\\");
            CString fPath = NULL;
            CString fName = NULL;
            if(fOpenDlg.DoModal() ==IDOK)
            {
            fPath = fOpenDlg.GetPathName();//path of current file
            fName = fOpenDlg.GetFileName();
            }

            thats basically all the code concerning lpstrInitialDir.

            R 1 Reply Last reply
            0
            • U UserNameless

              ok so i get your point with using CString over std::string in MFCs, however I had to do some operations like name.append(".txt" which don't seem to be working if I declare name as a CString variable. and that m_pOFN->lpstrInitialDir = _T("C:\\"); also doesn't seem to be working. ok so here's ALL (I hope) relavent code related to that function:

              ::OnBnClickedBrowse() //When you click the open button
              {
              CFileDialog fOpenDlg(true,NULL,NULL,NULL,_T("All Files (*.*)|*.*||"),this);
              fOpenDlg.m_pOFN->lpstrTitle= _T("Open File");
              fOpenDlg.m_pOFN->lpstrInitialDir = _T("C:\\");
              CString fPath = NULL;
              CString fName = NULL;
              if(fOpenDlg.DoModal() ==IDOK)
              {
              fPath = fOpenDlg.GetPathName();//path of current file
              fName = fOpenDlg.GetFileName();
              }

              thats basically all the code concerning lpstrInitialDir.

              R Offline
              R Offline
              Rajesh R Subramanian
              wrote on last edited by
              #6

              sadman89 wrote:

              however I had to do some operations like name.append(".txt" which don't seem to be working if I declare name as a CString variable.

              Append works perfectly. You are most probably doing something wrong. (again, unless I see your code...) You might as well want to look at the CString::operator+, just in case you didn't know, you could do this: CString szStr(_T("Hello, ")); szStr += _T("world!");

              sadman89 wrote:

              CString fPath = NULL; CString fName = NULL;

              This kind of initialization is not really needed. Your code works perfectly on my machine. Setting of the initial directory and the title of the open dialog seem to have no problem at all. I don't see what could have gone wrong. I'm assuming you have *do have* a C drive. :-D

              It is a crappy thing, but it's life -^ Carlo Pallini

              U 1 Reply Last reply
              0
              • R Rajesh R Subramanian

                sadman89 wrote:

                however I had to do some operations like name.append(".txt" which don't seem to be working if I declare name as a CString variable.

                Append works perfectly. You are most probably doing something wrong. (again, unless I see your code...) You might as well want to look at the CString::operator+, just in case you didn't know, you could do this: CString szStr(_T("Hello, ")); szStr += _T("world!");

                sadman89 wrote:

                CString fPath = NULL; CString fName = NULL;

                This kind of initialization is not really needed. Your code works perfectly on my machine. Setting of the initial directory and the title of the open dialog seem to have no problem at all. I don't see what could have gone wrong. I'm assuming you have *do have* a C drive. :-D

                It is a crappy thing, but it's life -^ Carlo Pallini

                U Offline
                U Offline
                UserNameless
                wrote on last edited by
                #7

                weird :confused: yeah i googled the .append and also the C: drive thingy, works for other ppl. bah, so i won't be able to open the C drive default... noone's gonna die lol. thanks for the help... now on to figure out bigger bugs X|

                1 Reply Last reply
                0
                • U UserNameless

                  I wonder if someone can help me understand why, when I made changes to the following code worked (more like, gave me the result that I wanted) and if I did something wrong which can come back to haunt me later on (don't bash me :~ ) Original Code:

                  CFileDialog fOpenDlg(true,NULL,NULL,NULL,_T("All Files (*.*)|*.*||"),this);
                  CString fName = NULL;
                  string name;
                  fName = fOpenDlg.GetFileName();
                  name = fName;

                  Error: error C2679: binary '=' : no operator found which takes a right-hand operand of type 'CString' (or there is no acceptable conversion) now the Modified Code (not the final one):

                  CFileDialog fOpenDlg(true,NULL,NULL,NULL,_T("All Files (*.*)|*.*||"),this);
                  CString fName = NULL;
                  string name;
                  fName = fOpenDlg.GetFileName();
                  name = *fName;

                  Observation: if I open a file named test.txt fName gets set to test, and name gets set to t Final code (giving me the result I wanted, discovered accidentally while applying it elsewhere):

                  CFileDialog fOpenDlg(true,NULL,NULL,NULL,_T("All Files (*.*)|*.*||"),this);
                  CString fName = NULL;
                  string name;
                  fName = fOpenDlg.GetFileName();
                  name = (fName+"");

                  Observation: now if I open a file named test.txt, fName gets set to test, and name ALSO gets set to test. I've been wondering how that +"" changed the behavior Thanks

                  D Offline
                  D Offline
                  David Crow
                  wrote on last edited by
                  #8

                  Until you call fOpenDlg.DoModal(), your code is pointless. That aside...

                  sadman89 wrote:

                  CString fName = NULL;

                  Why are you assigning NULL to a non-pointer? Not sure how this even compiles.

                  sadman89 wrote:

                  name = *fName;

                  This makes no sense.

                  sadman89 wrote:

                  name = (fName+"");

                  Nor does this.

                  "Old age is like a bank account. You withdraw later in life what you have deposited along the way." - Unknown

                  "Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons

                  1 Reply Last reply
                  0
                  Reply
                  • Reply as topic
                  Log in to reply
                  • Oldest to Newest
                  • Newest to Oldest
                  • Most Votes


                  • Login

                  • Don't have an account? Register

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • World
                  • Users
                  • Groups