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. Token and parsing

Token and parsing

Scheduled Pinned Locked Moved C / C++ / MFC
tutorialquestioncomsysadmindata-structures
4 Posts 2 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.
  • H Offline
    H Offline
    HomeNuke
    wrote on last edited by
    #1

    I have a current project going at work and since I'm the only developer here would like to use you guys as a sounding board, hope no one minds. Here's my current project I have a text file that extracts total counts by different codes into a main text file. Here is an example of a couple lines from the text file:

    3AAA, 789
    4EFJ,R321 90
    5BBB,3AAA 6
    etc...

    Now what I'm doing is reading in the file with CStdioFile and tokenizing each line and inserting relevant information into a structure like:

    struct Data{
    char* Code;
    long Amount;
    };

    by using the strtok() function I have to keep a counter during the tokenizing portion of the program in order to back track from an array of struct Data I created in order to fill in the Amount field with the right amount. Because let's say for example I tokenize the 3rd string I get these tokens:

    5BBB
    3AAA
    6

    I do not get the amount value until the very end so I have to go back on 5BBB and 3AAA to add in amount 6. Now this file gets pretty big, and with just going through the array and looking up each string literal to see if it is there or not and then adding the amount I might be using more resources than I need to. Question to anyone is there a more efficent way to do this? I'm not asking for code examples just methodology. If I got code examples then I couldn't figure out how to write it and then programming becomes no fun :) But if there is a more efficient method to do what I'm proposing please let me know, and for further clarification just from the 3 lines at top the final output file looks like:

    3AAA 765
    4EFG 90
    R321 90
    5BBB 6

    It works now, but I'm thinking there is a more efficent way of doing this... TIA HomeNuke ---- "Nuke'd Your Home, Yet?" Run your own PostNuke based web server from home http://www.homenuke.com

    O 1 Reply Last reply
    0
    • H HomeNuke

      I have a current project going at work and since I'm the only developer here would like to use you guys as a sounding board, hope no one minds. Here's my current project I have a text file that extracts total counts by different codes into a main text file. Here is an example of a couple lines from the text file:

      3AAA, 789
      4EFJ,R321 90
      5BBB,3AAA 6
      etc...

      Now what I'm doing is reading in the file with CStdioFile and tokenizing each line and inserting relevant information into a structure like:

      struct Data{
      char* Code;
      long Amount;
      };

      by using the strtok() function I have to keep a counter during the tokenizing portion of the program in order to back track from an array of struct Data I created in order to fill in the Amount field with the right amount. Because let's say for example I tokenize the 3rd string I get these tokens:

      5BBB
      3AAA
      6

      I do not get the amount value until the very end so I have to go back on 5BBB and 3AAA to add in amount 6. Now this file gets pretty big, and with just going through the array and looking up each string literal to see if it is there or not and then adding the amount I might be using more resources than I need to. Question to anyone is there a more efficent way to do this? I'm not asking for code examples just methodology. If I got code examples then I couldn't figure out how to write it and then programming becomes no fun :) But if there is a more efficient method to do what I'm proposing please let me know, and for further clarification just from the 3 lines at top the final output file looks like:

      3AAA 765
      4EFG 90
      R321 90
      5BBB 6

      It works now, but I'm thinking there is a more efficent way of doing this... TIA HomeNuke ---- "Nuke'd Your Home, Yet?" Run your own PostNuke based web server from home http://www.homenuke.com

      O Offline
      O Offline
      Orbital
      wrote on last edited by
      #2

      Hy! You can applay 2 strtok. First you make strtok( line, " " ) and get 2 substring( one with the codes and one with the value ). 5BBB,3AAA 6 Then applay again strtok on the codes string and now you can complete your structure because you already have the value ... and as you can here is no backtracking :) 5BBB 6 3AAA 6 Bye, Orbital^ ...the night is long ... but not long enought to do some real coding ...

      H 2 Replies Last reply
      0
      • O Orbital

        Hy! You can applay 2 strtok. First you make strtok( line, " " ) and get 2 substring( one with the codes and one with the value ). 5BBB,3AAA 6 Then applay again strtok on the codes string and now you can complete your structure because you already have the value ... and as you can here is no backtracking :) 5BBB 6 3AAA 6 Bye, Orbital^ ...the night is long ... but not long enought to do some real coding ...

        H Offline
        H Offline
        HomeNuke
        wrote on last edited by
        #3

        Man, when you finally see the answer you hit yourself on the head! Thanks alot now let me see if I can implement this method ;) Thanks again, I'll let you know if it works, or should I say, if I can get it to work :) HomeNuke ---- "Nuke'd Your Home, Yet?" Run your own PostNuke based web server from home http://www.homenuke.com

        1 Reply Last reply
        0
        • O Orbital

          Hy! You can applay 2 strtok. First you make strtok( line, " " ) and get 2 substring( one with the codes and one with the value ). 5BBB,3AAA 6 Then applay again strtok on the codes string and now you can complete your structure because you already have the value ... and as you can here is no backtracking :) 5BBB 6 3AAA 6 Bye, Orbital^ ...the night is long ... but not long enought to do some real coding ...

          H Offline
          H Offline
          HomeNuke
          wrote on last edited by
          #4

          Just wanted to let you know your method works! :) Thanks it is much cleaner than my previous code attempt. Now I just need to convert my fixed size array to a vector and it would be that much more perfect :-D Thanks again! HomeNuke ---- "Nuke'd Your Home, Yet?" Run your own PostNuke based web server from home http://www.homenuke.com

          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