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. String Parsing

String Parsing

Scheduled Pinned Locked Moved C / C++ / MFC
c++sysadminjsonhelp
5 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.
  • A Offline
    A Offline
    aj
    wrote on last edited by
    #1

    I have an MFC application that querries an ODBC responder. There is a particular set of messages which is unique to the system which are send to the server through the winsock to register and then to querry the ODBC. Once the application receives the data from the winsock control it is in a string which has to be parsed, right now I got the string separated by the "|" that's the symbol that separates these messages. Then, the information from the ODBC comes in this format, { 1 1 administrator Default {} {} 0 {}} I am having a hard time parsing the information out of the curly braces. I was thinking of finding the position of the first "{" brace and then finding the position of the last "}" brace, and then extracting the content in between. If any of you guys have any suggestions, methods or any sample code wchich could help me out it would be great, :-) Thanks

    M R 2 Replies Last reply
    0
    • A aj

      I have an MFC application that querries an ODBC responder. There is a particular set of messages which is unique to the system which are send to the server through the winsock to register and then to querry the ODBC. Once the application receives the data from the winsock control it is in a string which has to be parsed, right now I got the string separated by the "|" that's the symbol that separates these messages. Then, the information from the ODBC comes in this format, { 1 1 administrator Default {} {} 0 {}} I am having a hard time parsing the information out of the curly braces. I was thinking of finding the position of the first "{" brace and then finding the position of the last "}" brace, and then extracting the content in between. If any of you guys have any suggestions, methods or any sample code wchich could help me out it would be great, :-) Thanks

      M Offline
      M Offline
      Masaaki Onishi
      wrote on last edited by
      #2

      Hello, the codegurus around the world.;) I'm not sure what is going on. 1) Did you create your own client/server program? 2) If so, server program reads some info from ODBC setting on Window NT server, server program sends this info to client. 3) Did you use CSocket class or WinSock API? If you use CSocket class, we have a chance to have our class derived from CObject to set the data structure to make our life easy.:rolleyes: Have a nice day! -Masaaki Onishi-

      A 1 Reply Last reply
      0
      • A aj

        I have an MFC application that querries an ODBC responder. There is a particular set of messages which is unique to the system which are send to the server through the winsock to register and then to querry the ODBC. Once the application receives the data from the winsock control it is in a string which has to be parsed, right now I got the string separated by the "|" that's the symbol that separates these messages. Then, the information from the ODBC comes in this format, { 1 1 administrator Default {} {} 0 {}} I am having a hard time parsing the information out of the curly braces. I was thinking of finding the position of the first "{" brace and then finding the position of the last "}" brace, and then extracting the content in between. If any of you guys have any suggestions, methods or any sample code wchich could help me out it would be great, :-) Thanks

        R Offline
        R Offline
        realJSOP
        wrote on last edited by
        #3

        Check out my CStringParser class (posted here on CodeProject). It supports "quoted" strings, but you're using matching braces, so you'd have to replace the '{' and '}' with a suitable character, like a double quote or maybe even a caret. Here's how it would work: CString sMyString = "{ 1 1 administrator Default {} {} 0 {}}"; // remove the curly brace and space characters from the beginning of the string sMyString.Mid(2); // remove the last curly brace from the end of the string sMyString.Left(0, sMyString.GetLength() - 1); // replace the braces sMyString.Replace('{', '\"'); sMyString.Replace('}', '\"'); // now parse the string CStringParser sParser(sMyString,' ', '\"'); You should get the following string values as a result: sParser.GetField(0) - "1 1 administrator Default "" "" 0 """ sParser.GetField(1) - "1" sParser.GetField(2) - "1" sParser.GetField(3) - "administrator" sParser.GetField(4) - "Default" sParser.GetField(5) - "" sParser.GetField(6) - "" sParser.GetField(7) - "0" sParser.GetField(5) - ""

        A 1 Reply Last reply
        0
        • M Masaaki Onishi

          Hello, the codegurus around the world.;) I'm not sure what is going on. 1) Did you create your own client/server program? 2) If so, server program reads some info from ODBC setting on Window NT server, server program sends this info to client. 3) Did you use CSocket class or WinSock API? If you use CSocket class, we have a chance to have our class derived from CObject to set the data structure to make our life easy.:rolleyes: Have a nice day! -Masaaki Onishi-

          A Offline
          A Offline
          aj
          wrote on last edited by
          #4

          1)Yes 2)Yes 3)CAsyncSocket ;)

          1 Reply Last reply
          0
          • R realJSOP

            Check out my CStringParser class (posted here on CodeProject). It supports "quoted" strings, but you're using matching braces, so you'd have to replace the '{' and '}' with a suitable character, like a double quote or maybe even a caret. Here's how it would work: CString sMyString = "{ 1 1 administrator Default {} {} 0 {}}"; // remove the curly brace and space characters from the beginning of the string sMyString.Mid(2); // remove the last curly brace from the end of the string sMyString.Left(0, sMyString.GetLength() - 1); // replace the braces sMyString.Replace('{', '\"'); sMyString.Replace('}', '\"'); // now parse the string CStringParser sParser(sMyString,' ', '\"'); You should get the following string values as a result: sParser.GetField(0) - "1 1 administrator Default "" "" 0 """ sParser.GetField(1) - "1" sParser.GetField(2) - "1" sParser.GetField(3) - "administrator" sParser.GetField(4) - "Default" sParser.GetField(5) - "" sParser.GetField(6) - "" sParser.GetField(7) - "0" sParser.GetField(5) - ""

            A Offline
            A Offline
            aj
            wrote on last edited by
            #5

            Thanks John! The CStringParser clarifies a lot and it's not verbose, on the contrary very helpfull(It's probably one of the more helpfull string parsers on the web. If you get a chance the bracketed sub-strings would be usefull to see. ;)

            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