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