Advertisement

01.06.2008 at 07:48AM PST, ID: 23062017
[x]
Attachment Details
[x]
The Solution Rating System

With so many solutions, how can you tell which solutions are most likely to help you and which ones are not? To provide you with a tool to use, we rate our solutions based on various elements that most accurately determine if a solution is a quality solution. To explain what factors affect the solution rating, here are the elements we take into consideration when formulating our solution rating.

  • The Grade of the Solution
  • The Zone Rank of the Expert Providing the Solution
  • The Number of Author and Expert Comments
  • The Number of Experts Contributing
  • The Feedback of the Community

Your Input Matters
Because of the way the system is set up, the most important variable in this equation is you. As a member of Experts Exchange, you are able to cast your vote on the quality of the solutions in regard to how complete, accurate, helpful and easy to understand each solution is. When you provide your feedback, each rating is adjusted accordingly. So, if you see a solution that has a poor rating that you think is a good solution, let us know by rating it. As you do, the rating will be adjusted and will become more accurate for other members of our site.

If you have any suggestions that you would like to make for our rating system, please ask a question in the Suggestions Zone of Community Support.

Thank you!

Parsing a comma delimited string
Tags: C
hi,
i know this question has been answer a million times before but for some reason i can't find a proper answer to this problem

i have a char * that consists in 3 values separated by commas (,). for example:

hello,1234567,0s0d

now, i need to parse that string into 3 different variables (char *)
i don't know each value's lengh, it could be "hello" like in this example, or it could be "uuuuuuuuuuuuuu". the only thing constant is that each value is separated by a comma.

i tried playing with all the str* functions (strtok, strstr, str....) but i couldn't come with a good solution. any chance for some sample code?

thanks

Start your free trial to view this solution
Question Stats
Zone: Programming
Question Asked By: urif
Solution Provided By: Infinity08
Participating Experts: 2
Solution Grade: A
Views: 54
Translate:
Loading Advertisement...
01.06.2008 at 07:55AM PST, ID: 20594266

Rank: Sage

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
01.06.2008 at 07:56AM PST, ID: 20594268

Rank: Sage

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
01.06.2008 at 08:50AM PST, ID: 20594457

Rank: Guru

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
01.06.2008 at 08:30PM PST, ID: 20597092

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
01.06.2008 at 11:17PM PST, ID: 20597455

Rank: Sage

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
01.06.2008 at 11:17PM PST, ID: 20597457

Rank: Sage

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
01.06.2008 at 11:42PM PST, ID: 20597531

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
01.07.2008 at 01:11AM PST, ID: 20597774

Rank: Sage

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
01.07.2008 at 02:06AM PST, ID: 20597982

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
 
Loading Advertisement...
Microsoft
  • Internet Protocols
  • Applications
  • Development
  • OS
  • Hardware
  • Windows Security
Apple
  • Operating Systems
  • Hardware
  • Programming
  • Networking
  • Software
Internet
  • Search Engines
  • File Sharing
  • WebTrends / Stats
  • Spy / Ad Blockers
  • Web Browsers
  • New Net Users
  • Web Development
  • Chat / IM
  • Anti Spam
  • Web Servers
  • Anti-Virus
  • Email Clients
Gamers
  • Tips
  • Online / MMORPG
  • Puzzle
  • Emulators
  • Action / Adventure
  • Role Playing
  • Consoles
  • Game Programming
  • Strategy
  • Sports
  • Misc
  • Computer Games
Digital Living
  • Hardware
  • New Net Users
  • New Users
  • Software
  • Digital Music
  • Gaming World
  • Home Security
  • Apple
  • Networking Hardware
Virus & Spyware
  • Vulnerabilities
  • IDS
  • Encryption
  • Anti-Virus
  • Operating Systems Security
  • Software Firewalls
  • WebApplications
  • Cell Phones
  • Operating Systems
  • Internet
  • Hardware Firewalls
Hardware
  • Handhelds / PDAs
  • Displays / Monitors
  • Components
  • Networking Hardware
  • Peripherals
  • Laptops/Notebooks
  • Storage
  • Servers
  • Desktops
  • New Users
  • Misc
  • Apple
Software
  • System Utilities
  • Industry Specific
  • Network Management
  • Photos / Graphics
  • Page Layout
  • VMWare
  • Misc
  • Web Development
  • OS
  • CYGWIN
  • Voice Recognition
  • Message Queue
  • Quality Assurance
  • Security
  • Firewalls
  • MultiMedia Applications
  • Development
  • Database
  • Office / Productivity
  • Business Management
  • OS/2 Apps
  • Server Software
  • Internet / Email
ITPro
  • OS
  • Storage
  • Encryption
  • Operating Systems Security
  • Apple Hardware
  • Laptops & Notebooks
  • Servers
  • Networking Hardware
  • Peripherals
  • Devices
  • Displays / Monitors
  • WebTrends / Stats
  • Search Engines
  • Firewalls
  • WebApplications
  • IDS
  • Vulnerabilities
  • Email Clients
  • File Sharing
  • Spy / Ad Blockers
  • Web Browsers
  • Web Servers
  • Networking
  • Anti-Virus
  • Chat / IM
  • Anti Spam
Developer
  • Web Servers
  • Web Browsers
  • Game Programming
  • Dev Tools
  • Industry Specific
  • Office / Productivity
  • Database
  • CYGWIN
  • Web Development
  • Search Engines
  • File Sharing
  • WebTrends / Stats
  • Programming
  • Content Management
  • Application Servers
  • Protocols
Storage
  • Removable Backup Media
  • Storage Technology
  • Servers
  • Grid
  • Remote Access
  • Backup / Restore
  • Misc
  • Hard Drives
OS
  • Miscellaneous
  • Security
  • Development
  • Linux
  • VMWare
  • MainFrame OS
  • Unix
  • Apple
  • OS / 2
  • AS / 400
  • BeOS
  • Microsoft
  • VMS / OpenVMS
Database
  • Oracle
  • Miscellaneous
  • MySQL
  • Software
  • Sybase
  • Contact Management
  • PostgreSQL
  • Data Manipulation
  • Clarion
  • InterSystems Cache
  • Siebel
  • MUMPS
  • OLAP
  • SQLBase
  • SAS
  • GIS & GPS
  • 4GL
  • Berkeley DB
  • DB2
  • Informix
  • Interbase / Firebird
  • FoxPro
  • Reporting
  • LDAP
  • Filemaker Pro
  • MS SQL Server
  • dBase
  • MS Access
Security
  • Misc
  • Web Browsers
  • Software Firewalls
  • Operating Systems Security
  • File Sharing
  • Spy / Ad Blockers
  • Vulnerabilities
  • WebApplications
  • IDS
  • Anti-Virus
  • Encryption
  • Anti Spam
  • Email Clients
  • VPN
  • Chat / IM
Programming
  • Editors IDEs
  • Installation
  • Handhelds / PDAs
  • Multimedia Programming
  • System / Kernel
  • Algorithms
  • Game
  • Signal Processing
  • Project Management
  • Open Source
  • Database
  • Misc
  • Languages
  • Processor Platforms
  • Theory
Web Development
  • Scripting
  • Blogs
  • Web Servers
  • Software
  • Search Engines
  • Web Graphics
  • Images
  • Internet Marketing
  • Images and Photos
  • Components
  • Document Imaging
  • Web Languages/Standards
  • Illustration
  • WebApplications
  • Fonts
  • WebTrends / Stats
  • Authoring
  • Digital Camera Software
  • Miscellaneous
Networking
  • Protocols
  • Apple Networking
  • Network Management
  • Message Queue
  • Application Servers
  • Content Management
  • File Servers
  • Email Servers
  • Misc
  • Java Editors & IDEs
  • Wireless
  • Networking Hardware
  • Backup / Restore
  • System Utilities
  • ISPs & Hosting
  • Web Servers
  • Storage Technology
  • Removable Backup Media
  • Servers
  • Broadband
  • Grid
  • OS / 2
  • Novell Netware
  • Unix Networking
  • Windows Networking
  • Security
  • Telecommunications
  • Operating Systems
  • Linux Networking
Other
  • Community Advisor
  • Lounge
  • Community Support
  • New Net Users
  • Philosophy / Religion
  • Math / Science
  • Miscellaneous
  • URLs
  • Expert Lounge
  • Politics
  • Puzzles / Riddles
Community Support
  • Suggestions
  • New to EE
  • New Topics
  • Community Advisor
  • CleanUp
  • Announcements
  • General
  • Feedback
  • Input
  • EE Bugs
 
01.06.2008 at 07:55AM PST, ID: 20594266

Rank: Sage

Look at this code :
1:
2:
3:
4:
5:
6:
7:
char str[] = "hello,1234567,0s0d";         /* <--- the string that we want to split up */
char *tok = strtok(str, ",");              /* <--- get the first token */
 
while (tok) {                              /* <--- tok will be null if no more token can be found */
  printf("token : %s\n", tok);             /* <--- show the current token */
  strtok(0, ",");                          /* <--- get the next token */
}
Open in New Window
 
01.06.2008 at 07:56AM PST, ID: 20594268

Rank: Sage

Note that instead of the printf, you can do anything you want with the found token.

Also, a nice reference page for strtok :

        http://www.cplusplus.com/reference/clibrary/cstring/strtok.html
 
01.06.2008 at 08:50AM PST, ID: 20594457

Rank: Guru

Just a note that there is a difference between what you are asking for and Comma Separated Values (CSV). With CSV you are allowed to have a comma embedded within one of the values. This is handled by enclosing the value with a comma in quotes. There are some other caveats too.

http://en.wikipedia.org/wiki/Comma-separated_values

You just need to be aware that parsing CSV data is more complex than just using strtok since it will not respect the quoted value and tread the embedded comma as a separator.

-Rx.
 
01.06.2008 at 08:30PM PST, ID: 20597092
@infinity8 thanks for the help, but as i mentioned i already tried strtok, i need to separate the 3 values into 3 different variables.

@evilrix it's not CSV, i could've chosen + or | or % as a separator, i chose comma because it was convenient and also it's more readable when i am debugging.  
 
01.06.2008 at 11:17PM PST, ID: 20597455

Rank: Sage

>> @infinity8 thanks for the help, but as i mentioned i already tried strtok, i need to separate the 3 values into 3 different variables.

Well, the code I posted is the standard example to split up a comma-separated string into its tokens ... If you execute it, you'll see that the three tokens you want are correctly recognised.

Now, I also gave the hint that you can replace the printf with other code ... That's exactly what you need to do. Instead of showing the token on the screen using printf, you have to store the token in a variable :

1:
2:
3:
4:
5:
6:
7:
char str[] = "hello,1234567,0s0d";         /* <--- the string that we want to split up */
char *tok = strtok(str, ",");              /* <--- get the first token */
 
while (tok) {                              /* <--- tok will be null if no more token can be found */
  /* tok is the current token - you should store it in a variable here */
  strtok(0, ",");                          /* <--- get the next token */
}
Open in New Window
 
01.06.2008 at 11:17PM PST, ID: 20597457

Rank: Sage

Can you give it a try, and post what you have here ?
 
01.06.2008 at 11:42PM PST, ID: 20597531
thanks, maybe i am being a bit slow here, but how do you want me to update the variables inside the while? you want to check each variable each iteration to see if it already got the value and update it if not?

i don't understand. i have 3 values in one string, each value separated by a token (comma in this case) i need to grab each value and extract it into a different char * variable.

any chance you can show me an example of working code?

thanks again
 
01.07.2008 at 01:11AM PST, ID: 20597774

Rank: Sage

>> thanks, maybe i am being a bit slow here

No, sorry. It's my fault. I should have been more clear.


>> i have 3 values in one string, each value separated by a token (comma in this case)

First some terminology : the comma is the separator in this case ... what is between the separators are the tokens.


>> any chance you can show me an example of working code?

It would be very easy for me, but I prefer to help you find the code yourself by giving you hints and nudges in the good direction. I found that the best way to learn something ... if you don't mind ;)


So, I'll be a bit more verbose this time :

Let's first see what the piece of code I posted does :

        char str[] = "hello,1234567,0s0d";
        char *tok = strtok(str, ",");
       
        while (tok) {
         
          strtok(0, ",");
        }

strtok modifies the original string, so you always have to make sure that it is writable (and that it can be modified). The first strtok (the one outside of the while) will find the first separator (in this case we chose the comma) in the str string. It will replace that separator by a null character '\0'. So before str consisted of these characters :

        'h'  'e'  'l'  'l'  'o'  ','  '1'  '2'  '3'  '4'  '5'  '6'  '7'  ','  '0'  's'  '0'  'd'  '\0'

and after :

        'h'  'e'  'l'  'l'  'o'  '\0'  '1'  '2'  '3'  '4'  '5'  '6'  '7'  ','  '0'  's'  '0'  'd'  '\0'

strtok will return a pointer to the first character of the token that was found (in this case the 'h' of "hello"), and will keep a pointer to the rest of the string internally (in this case the '1' just after the '\0'). This internal pointer is necessary so that the next call of strtok knows where to start.

So, then the while loop is started, and we can do something with the found token "hello". After that, we look for the next token with a new strtok call. This time however, we pass NULL as the first parameter to signify that we want to continue with the previous string (and so that we want to use the internal stored pointer). Again strtok will find the next separator, starting from the '1' character, and will replace it with '\0', so we get :

        'h'  'e'  'l'  'l'  'o'  '\0'  '1'  '2'  '3'  '4'  '5'  '6'  '7'  '\0'  '0'  's'  '0'  'd'  '\0'

and find the token "1234567". And the final strtok call will not find any separator any more, and will thus return the final token "0s0d".


Now ... this was just for your information so you understand better what happens, and how strtok works. If something isn't clear - feel free to ask.


How can we make use of this now ? There are two main approaches :

1) you can keep the while loop as I showed it, and store the tokens in an array of strings. With each iteration of the while loop, you increment the index into that array, so the next token will be stored at the next position.

2) you can also store the tokens in separate variables (not an array). In that case, it's best to get rid of the while loop, and just call strtok 3 times in a row - ie. unroll the while loop.

Which approach you choose, depends on the requirements. From your last post, I understand that you want to take the second approach ? If so, then just take leave out the while loop, and call strtok 3 times. After each call to strtok, you can store the found token in one of the variables.

Does that get you going ? If so, give it a try, and post what you get here. If not, feel free to ask for clarification where needed.
Accepted Solution
 
01.07.2008 at 02:06AM PST, ID: 20597982
thanks once again, i did use strtok 3 times in a row, still it did not solve the problem. however i found a way to do by playing with strstr and other str functions.
so the points go to you, the explanation was excellent but i already know how strtok works, it just wasn't what i needed to solve the problem.

thanks once again, i was looking for a simple piece of code, but your solution is good enough, even tho it didn't solve the problem.
 
 
01.07.2008 at 02:10AM PST, ID: 20597998
>> i did use strtok 3 times in a row, still it did not solve the problem.

If you show your code, I'll point out the problem :)


>> the explanation was excellent but i already know how strtok works, it just wasn't what i needed to solve the problem.

Sorry ;) At least it might be useful to someone else reading it heh.
 
 
 
20080236-EE-VQP-29 / EE_QW_2_20070628