Extract Elements From a Comma Delimited String

This is a discussion on Extract Elements From a Comma Delimited String within the C++ Programming forums, part of the General Programming Boards category; I am developing a program that receives TCP packets from an OP7200 SBC. I am receiving the packets just fine, ...

  1. #1
    Registered User
    Join Date
    Nov 2006
    Posts
    2

    Question Extract Elements From a Comma Delimited String

    I am developing a program that receives TCP packets from an OP7200 SBC. I am receiving the packets just fine, but now I am trying to extract the individual elements from the string so I can work with them. Here is the code I have so far:

    Code:
    #include "ServerSocket.h"
    #include "SocketException.h"
    #include <string>
    #include <iostream>
    #include <stdio.h>
    
    int main ( int argc, int argv[] )
    {
    
      try
        {
          // Create the socket
          ServerSocket server ( 14035 );
    
          while ( true )
    	{
    
    	  ServerSocket new_sock;
    	  server.accept ( new_sock );
    
    	  try
    	    {
    	      while ( true )
    		{
    		  std::string data;
    		  new_sock >> data;
    		  new_sock << data;
    		  using namespace std;
    		  string trainid(data);
    		  cout << "Data received: " << trainid << endl;
    		}
    	    }
    	  catch ( SocketException& ) {}
    
    	}
        }
      catch ( SocketException& e )
        {
          std::cout << "Exception was caught:" << e.description() << "\nExiting.\n";
        }
    
      return 0;
    }
    Here is an example of the data I need to work with:
    Code:
    Unit Name,1,2
    Unit Name: Name of the location the next two numbers pertain to.
    1: Unit #
    2: Number of Cycles on Unit


    I know the solution is probably simple, but I am fairly new to C and C++. I'm having a hard time understanding some of the stuff I've gound on Google. The program I have so far is nothing more than code I used from an excellent socket programming tutorial. I've done PHP and VB programming in the past. This is my first time writing a program for a Linux box.

    Thanks!
    Chris

  2. #2
    Registered User
    Join Date
    Sep 2001
    Posts
    752
    Callou collei we'll code the way
    Of prime numbers and pings!

  3. #3
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    I wouldn't use strtok with strings. You can use a stringstream, or you can parse it yourself with string member functions like find and substr (you would still need a stringstream or strtol or atoi to get the integer from the string).

    Do you know how you would read that data from cin? If you do, do the same thing with a istringstream instead.

  4. #4
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    You can use the find function of string. And there's also find_first_of, find_first_not_of, etc.
    Code:
       std::string unit_name, unit_num, num_cycles;
    
       std::string::size_type pos = trainid.find(',');
       if (pos != std::string::npos)
       {
          unit_name = trainid.substr(0,pos);
          cout << unit_name << endl;
       }
    
       trainid = trainid.substr(pos+1);
       pos = trainid.find(',');
       if (pos != std::string::npos)
       {
          unit_num = trainid.substr(0,pos);
          cout << unit_num << endl;
       }
    
       num_cycles = trainid.substr(pos+1);
       cout << num_cycles << endl;

  5. #5
    Registered User
    Join Date
    Nov 2006
    Posts
    2
    Thank you all for your help.

    I added the code you wrote, swoopy, and it works great!!! Thank you very much!!!

    Chris

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 04:45 PM
  2. how to extract words from a string
    By panfilero in forum C Programming
    Replies: 7
    Last Post: 11-04-2005, 07:06 AM
  3. Program using classes - keeps crashing
    By webren in forum C++ Programming
    Replies: 4
    Last Post: 09-16-2005, 03:58 PM
  4. problems with overloaded '+' again
    By Brain Cell in forum C++ Programming
    Replies: 9
    Last Post: 04-14-2005, 05:13 PM
  5. Linked List Help
    By CJ7Mudrover in forum C Programming
    Replies: 9
    Last Post: 03-10-2004, 09:33 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21