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. Problems with std::map

Problems with std::map

Scheduled Pinned Locked Moved C / C++ / MFC
questionioscomhelp
5 Posts 2 Posters 1 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.
  • C Offline
    C Offline
    ChemmieBro
    wrote on last edited by
    #1

    This is my first experience with maps and I am having some troubles. I have developed a Logger that can do private logs depending on the client who calls the COM object. Based on the client's information, it needs to find the specific file to log to. Here is my code: //(header declarations) std::map m_FileList; std::map::iterator mFileListIterator; //(code) bool ClientHasCalledBefore; _bstr_t FileName; //name of the file coming in. //Look for FileName in map to see if it was called before. ClientHasCalledBefore = (!(m_FileList.find(FileName) == m_FileList.end())); // If it has called before, it logs to a clear file. // If not, it creates the file, then appends the logs. if (!ClientHasCalledBefore) { ofstream *ofstr = new ofstream(FileName, ios::out); m_FileList[FileName] = ofstr; *m_FileList[FileName] << "EntryToBeLogged"; m_FileList[FileName]->close(); } else { m_FileList[FileName]->open(FileName, ios::app); *m_FileList[FileName] << "EntryToBeLogged"; m_FileList[FileName]->close(); } Okay. This code leads me to my first question. When I enter a new file into the map, sometimes it does not enter properly and the next time the conditional is checked, the FileName is not found. It does this sometimes the first one to three times it runs through. After the first couple of times, everything works fine. Because I am opening the file clean, this overwrites the first couple of entries. Any suggestions so that my conditional finds the entry in the map the first time? //************** In my destructor, I have this code to add a footer to every file: for (m_FileListIterator = m_FileList.begin(); m_FileListIterator != m_FileList.end(); ++m_FileListIterator) { (m_FileListIterator->second)->open(FileListIterator->first, ios::app); *(m_FileListIterator->second) << "FooterToBeAdded"; (m_FileListIterator->second)->close(); } I think something is totally flawed with this. Sometimes it will append to a file I would have never created in my code, making a new file. Other times it will log the footer into the same file multiple times. It is almost as if it is not iterating. Again, this is my first experience with any of this. Any help is much appreciated. Thank you.

    J 1 Reply Last reply
    0
    • C ChemmieBro

      This is my first experience with maps and I am having some troubles. I have developed a Logger that can do private logs depending on the client who calls the COM object. Based on the client's information, it needs to find the specific file to log to. Here is my code: //(header declarations) std::map m_FileList; std::map::iterator mFileListIterator; //(code) bool ClientHasCalledBefore; _bstr_t FileName; //name of the file coming in. //Look for FileName in map to see if it was called before. ClientHasCalledBefore = (!(m_FileList.find(FileName) == m_FileList.end())); // If it has called before, it logs to a clear file. // If not, it creates the file, then appends the logs. if (!ClientHasCalledBefore) { ofstream *ofstr = new ofstream(FileName, ios::out); m_FileList[FileName] = ofstr; *m_FileList[FileName] << "EntryToBeLogged"; m_FileList[FileName]->close(); } else { m_FileList[FileName]->open(FileName, ios::app); *m_FileList[FileName] << "EntryToBeLogged"; m_FileList[FileName]->close(); } Okay. This code leads me to my first question. When I enter a new file into the map, sometimes it does not enter properly and the next time the conditional is checked, the FileName is not found. It does this sometimes the first one to three times it runs through. After the first couple of times, everything works fine. Because I am opening the file clean, this overwrites the first couple of entries. Any suggestions so that my conditional finds the entry in the map the first time? //************** In my destructor, I have this code to add a footer to every file: for (m_FileListIterator = m_FileList.begin(); m_FileListIterator != m_FileList.end(); ++m_FileListIterator) { (m_FileListIterator->second)->open(FileListIterator->first, ios::app); *(m_FileListIterator->second) << "FooterToBeAdded"; (m_FileListIterator->second)->close(); } I think something is totally flawed with this. Sometimes it will append to a file I would have never created in my code, making a new file. Other times it will log the footer into the same file multiple times. It is almost as if it is not iterating. Again, this is my first experience with any of this. Any help is much appreciated. Thank you.

      J Offline
      J Offline
      Joaquin M Lopez Munoz
      wrote on last edited by
      #2

      Hello ChemmieBro, next time you post here please make sure you check on the "Do not treat <'s as HTML tags" checkbox below the input window: otherwise your <'s and >'s won't show up properly. For the benefit of other readers, I'm reproducing here the definition of m_FileList with the bracket thing right:

      std::map<BSTR, ofstream*> m_FileList;

      The problem you're having is most likely the following: when you look up an item in std::map, the key you pass is compared against those of the elements stored in the map. So far so good, but in your case it is BSTRs that are used as keys, and these are pointers: so, instead of comparing the contents pointed to by the BSTR, it is the pointer addresses that std::map is taking into account, certainly not what you want. The most straightforward solution is to use as keys another type of objects with the right comparison semantics, like for instance MFC CString or some other wrapper around raw BSTRs. Joaquín M López Muñoz Telefónica, Investigación y Desarrollo Want a Boost forum in Code Project? Vote here[^]!

      C 1 Reply Last reply
      0
      • J Joaquin M Lopez Munoz

        Hello ChemmieBro, next time you post here please make sure you check on the "Do not treat <'s as HTML tags" checkbox below the input window: otherwise your <'s and >'s won't show up properly. For the benefit of other readers, I'm reproducing here the definition of m_FileList with the bracket thing right:

        std::map<BSTR, ofstream*> m_FileList;

        The problem you're having is most likely the following: when you look up an item in std::map, the key you pass is compared against those of the elements stored in the map. So far so good, but in your case it is BSTRs that are used as keys, and these are pointers: so, instead of comparing the contents pointed to by the BSTR, it is the pointer addresses that std::map is taking into account, certainly not what you want. The most straightforward solution is to use as keys another type of objects with the right comparison semantics, like for instance MFC CString or some other wrapper around raw BSTRs. Joaquín M López Muñoz Telefónica, Investigación y Desarrollo Want a Boost forum in Code Project? Vote here[^]!

        C Offline
        C Offline
        ChemmieBro
        wrote on last edited by
        #3

        Thanks for your help. I'm brand new to using BSTRs also. I do not have access to MFC here, so the CString is out. I will look into using some kind of wrapper around the BSTRs.

        J 1 Reply Last reply
        0
        • C ChemmieBro

          Thanks for your help. I'm brand new to using BSTRs also. I do not have access to MFC here, so the CString is out. I will look into using some kind of wrapper around the BSTRs.

          J Offline
          J Offline
          Joaquin M Lopez Munoz
          wrote on last edited by
          #4

          Check Michael Dunn and Nishant Sivakumar's Complete Guide to C++ Strings, Part II [^], you might find what you need there. Joaquín M López Muñoz Telefónica, Investigación y Desarrollo Want a Boost forum in Code Project? Vote here[^]!

          C 1 Reply Last reply
          0
          • J Joaquin M Lopez Munoz

            Check Michael Dunn and Nishant Sivakumar's Complete Guide to C++ Strings, Part II [^], you might find what you need there. Joaquín M López Muñoz Telefónica, Investigación y Desarrollo Want a Boost forum in Code Project? Vote here[^]!

            C Offline
            C Offline
            ChemmieBro
            wrote on last edited by
            #5

            Thank you so much. It was a pretty simple fix using the _bstr_t class instead of the BSTR. My inexperience with BSTR and _bstr_t got the best of me. I don't know why I chose BSTR in my map instead of _bstr_t. But everything is working fine now!

            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