User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the C++ section within the Software Development category of DaniWeb, a massive community of 391,601 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 2,608 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our C++ advertiser:
Views: 293 | Replies: 6 | Solved
Reply
Join Date: Apr 2008
Posts: 27
Reputation: savinki is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 0
savinki savinki is offline Offline
Light Poster

split data

  #1  
May 29th, 2008
I want to split data in a string. The string will consist of several data. Before every element the length ofthe element will come. I want to exract data one by one.

e.g. char * abc = "[length ]acsfdnhsjg[len]gvsdgdvheg[len]efgwetrge";

can someone help me!!!!!!!
AddThis Social Bookmark Button
Reply With Quote  
Join Date: May 2007
Location: Ireland
Posts: 1,698
Reputation: twomers will become famous soon enough twomers will become famous soon enough 
Rep Power: 6
Solved Threads: 24
twomers's Avatar
twomers twomers is offline Offline
Posting Virtuoso

Re: split data

  #2  
May 29th, 2008
So it'll say like 13 and you want to grab the next 13 characters? Will the length actually be wrapped in []? If ya have boost installed use boost::regex for the simplest way. You might be able to use strtok to split them all with respect to [, skip till ] and grab the rest of the string. If [ and ] mark the size of the string you don't actually need the length.
"Mr Kitty, you have to live in the attic now. Here, write a diary."
I am the Walrus!
Reply With Quote  
Join Date: Apr 2008
Posts: 27
Reputation: savinki is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 0
savinki savinki is offline Offline
Light Poster

Re: split data

  #3  
May 29th, 2008
no its not warapped in {}. just for understanding I used it. so i couldnt use strtok either.
Reply With Quote  
Join Date: Apr 2008
Posts: 27
Reputation: savinki is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 0
savinki savinki is offline Offline
Light Poster

Re: split data

  #4  
May 29th, 2008
example for the format of the data is:
5gterfs2rt5tyuri4oiuy10rtyuioiuyt
Reply With Quote  
Join Date: May 2008
Posts: 345
Reputation: Radical Edward has a spectacular aura about Radical Edward has a spectacular aura about Radical Edward has a spectacular aura about 
Rep Power: 3
Solved Threads: 56
Radical Edward's Avatar
Radical Edward Radical Edward is offline Offline
Posting Whiz

Re: split data

  #5  
May 29th, 2008
Probably easiest would be to read the length and then extract the characters after it one by one. The code is longer because Ed put in complete error checking:
#include <cctype>
#include <cstdlib>
#include <iostream>

int main()
{
  char *p = "6gterfs2rt5tyuri4oiuy10rtyuioiuyt";
  char *data = p;
  long length;

  while (*data != '\0') {
    // Save the current position for error checking
    char *temp = data;
    long length = std::strtol(data, &data, 0);

    // Make sure a leading length is present
    if (data == temp) {
      std::cerr << "Invalid length detected "
        "at [" << data << "]\n";
      break;
    }

    // Make sure trailing data is present
    if (*data == '\0') {
      std::cerr << "Length detected ["
        << length << "] without data\n";
      break;
    }

    for (long i = 0; *data != '\0' && i < length; ++i)
      std::cout << *data++;
    std::cout << '\n';

    // Make sure the data matches the length
    if (*data != '\0' && !std::isdigit(*data)) {
      std::cerr << "Bad length [" << length
        << "] before [" << data << "]\n";
      break;
    }
  }
}
Error checking is good. It's because of the error checking that Edward found an error in your sample string. The first data set doesn't match the length you gave "5gterfs". "gterfs" has 6 characters but the length only specifies 5.
Last edited by Radical Edward : May 29th, 2008 at 8:33 am.
If at first you don't succeed, keep on sucking until you do succeed.
Reply With Quote  
Join Date: Apr 2008
Posts: 27
Reputation: savinki is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 0
savinki savinki is offline Offline
Light Poster

Re: split data

  #6  
May 29th, 2008
thanks edward.
The solution given by the Edward is woring fine. but it fails if data starts with a digit.
e.g. char *p = "006gterfs002rt005tyuri0028ab004oiuy010rtyuioiuyt";
here length of the data = 002 and data =8ab

and the other thing is here data will be display character by character.
I want to assigned the extracted data into a char pointer.

e.g. char * ptr;
and ptr = ab
Reply With Quote  
Join Date: May 2008
Posts: 345
Reputation: Radical Edward has a spectacular aura about Radical Edward has a spectacular aura about Radical Edward has a spectacular aura about 
Rep Power: 3
Solved Threads: 56
Radical Edward's Avatar
Radical Edward Radical Edward is offline Offline
Posting Whiz

Re: split data

  #7  
May 29th, 2008
> but it fails if data starts with a digit.
With the example you gave before: "5gterfs2rt5tyuri4oiuy10rtyuioiuyt", that's an impossible case because the length could be multiple digits. Your new example suggests that the length is a fixed width number of 3 digits with leading zeros. That's different, and it's a good way to see how minor differences in the format can change the suitability of a solution. When you need help parsing a string, it's best to provide a formal specification and at least some of the actual data rather than a summary.

> here length of the data = 002 and data =8ab
That's still a logical error though. The length is 2, but the number of data characters is 3. Here's another example that extracts the data--without error checking!--using a 3 digit length:
#include <cstdio>
#include <cstdlib>
#include <iostream>

int main()
{
  char *p = "006gterfs002rt005tyuri0038ab004oiuy010rtyuioiuyt";
  char *data = p;

  while (*data != '\0') {
    char temp[4];
    int length;

    sscanf(data, "%3[0123456789]", temp);
    length = std::strtol(temp, 0, 10);
    data += 3;

    for (int i = 0; *data != '\0' && i < length; ++i)
      std::cout << *data++;
    std::cout << '\n';
  }
}
>and the other thing is here data will be display character by character.
How you process the characters after they're extracted is your business, not Ed's. The code was meant to be an example of how to extract the characters, not a complete solution to your problem.
If at first you don't succeed, keep on sucking until you do succeed.
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

DaniWeb C++ Marketplace
Thread Tools Display Modes

Similar Threads
Other Threads in the C++ Forum

All times are GMT -4. The time now is 11:51 pm.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC