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. memstr

memstr

Scheduled Pinned Locked Moved C / C++ / MFC
c++question
6 Posts 6 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.
  • K Offline
    K Offline
    kevincwong
    wrote on last edited by
    #1

    Hi, I have a binary string. I need to search certain characters. I used to use "memstr" (obtain fist substring from string) on Unix. Is there anything similar to that available from MFC? Thanks, Kevin

    R K V J P 5 Replies Last reply
    0
    • K kevincwong

      Hi, I have a binary string. I need to search certain characters. I used to use "memstr" (obtain fist substring from string) on Unix. Is there anything similar to that available from MFC? Thanks, Kevin

      R Offline
      R Offline
      Rage
      wrote on last edited by
      #2

      If your are using CStrings, yes, CString::Find ~RaGE();

      1 Reply Last reply
      0
      • K kevincwong

        Hi, I have a binary string. I need to search certain characters. I used to use "memstr" (obtain fist substring from string) on Unix. Is there anything similar to that available from MFC? Thanks, Kevin

        K Offline
        K Offline
        kakan
        wrote on last edited by
        #3

        strstr or _strstr

        1 Reply Last reply
        0
        • K kevincwong

          Hi, I have a binary string. I need to search certain characters. I used to use "memstr" (obtain fist substring from string) on Unix. Is there anything similar to that available from MFC? Thanks, Kevin

          V Offline
          V Offline
          vikas amin
          wrote on last edited by
          #4

          Use the CString to store the binary data & use Find (..) member fuction to find the charaters. :cool: Vikas Amin Embin Technology Bombay vikas.amin@embin.com

          1 Reply Last reply
          0
          • K kevincwong

            Hi, I have a binary string. I need to search certain characters. I used to use "memstr" (obtain fist substring from string) on Unix. Is there anything similar to that available from MFC? Thanks, Kevin

            J Offline
            J Offline
            James R Twine
            wrote on last edited by
            #5

            There is nothing exactly like memstr(...) in MFC or in the MS VC++ RTL.  The closest things are strstr(...) and and functions like CString::Find(...).    However, these might not work as you expect, because you said "binary string", and that usually means that NUL characters can be expected in other locations than the end of the string.    Other than taking an existing implementation and copying the source or writing your own implementation, a Q&D way would be to store the locations of all of the non-terminating NUL characters and convert them to some other character and then use a function like strstr(...) (with the strings modified as specified above).    After locating your substrings, you can convert them back by replacing the NULs you replaced before.    Peace! -=- James


            If you think it costs a lot to do it right, just wait until you find out how much it costs to do it wrong!
            Tip for new SUV drivers: Professional Driver on Closed Course does not mean your Dumb Ass on a Public Road!
            DeleteFXPFiles & CheckFavorites (Please rate this post!)

            1 Reply Last reply
            0
            • K kevincwong

              Hi, I have a binary string. I need to search certain characters. I used to use "memstr" (obtain fist substring from string) on Unix. Is there anything similar to that available from MFC? Thanks, Kevin

              P Offline
              P Offline
              PJ Arends
              wrote on last edited by
              #6

              As was mentioned before, strstr is the closest thing, but it is limited in that NULL characters terminate the buffers. So I have used this code:

              #include <string.h>

              char * _memmem(const char *mem, size_t mem_size, const char *sub, size_t sub_size)
              {
              // returns a pointer of the first occurance of a sub-buffer in a memory buffer
              // returns NULL if the sub-buffer was not found
              // is not limited by embedded NULLs in either the memory buffer or the sub-buffer
              //
              // Parameters:
              // mem - The memory buffer to scan
              // mem_size - The size, in bytes, of the memory buffer
              // sub - The sub buffer to find
              // sub_size - The size, in bytes, of the sub buffer

              char \*ret = NULL;
              char \*ptr = const\_cast<char \*>(mem);
              while (ptr && !ret)
              {
                  ptr = reinterpret\_cast<char \*>(memchr(ptr, \*sub, mem\_size - (sub\_size - 1) - (int)(ptr - mem)));
                  if (ptr)
                  {
                      if (!memcmp(ptr, sub, sub\_size))
                          ret = ptr;
                      ++ptr;
                  }
              }
              
              return ret;
              

              }

              [edit] cleaned up the code a little [/edit]


              "You're obviously a superstar." - Christian Graus about me - 12 Feb '03 "Obviously ???  You're definitely a superstar!!!" - mYkel - 21 Jun '04 "There's not enough blatant self-congratulatory backslapping in the world today..." - HumblePie - 21 Jun '05 Within you lies the power for good - Use it! -- modified at 18:12 Monday 21st November, 2005

              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