•
•
•
•
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
![]() |
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!
I am the Walrus!
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:
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.
#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;
}
}
}
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.
•
•
Join Date: Apr 2008
Posts: 27
Reputation:
Rep Power: 1
Solved Threads: 0
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
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
> 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:
>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.
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';
}
}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.
![]() |
•
•
•
•
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
•
•
•
•
•
•
•
•
DaniWeb C++ Marketplace
•
•
•
•
access advice broadband business classification code combo crime cult of the dead cow daniweb data data protection data transfer database drive dropdownlist encryption europe forensic forensics gadget google government hacking hard hardware help hitachi hp industrial espionage information internet linux mobile module net news payment services privacy protection reuse search security spot storage terabyte tutorials and more tv web wikipedia
- How to extract data from web databases? (Database Design)
- Reading external data into array of struct (Visual Basic 4 / 5 / 6)
- csv file split (Python)
- Reverse Data File (Python)
- Data consolidation across an array a.k.a. Hashed to death... heeelp... (Perl)
- passing data using multiple form styles (HTML and CSS)
- Suggestions for perl resources? Dealing with csv data. (Perl)
- cleaning up the boot partition in xp. (Windows NT / 2000 / XP / 2003)
- Help with programming assignment (Python)
Other Threads in the C++ Forum
- Previous Thread: Problem with Windows Command Line with C++
- Next Thread: graphs



Linear Mode