Re: My own fuction: Access Violation error...




"Blue Streak" <rdlebreton@xxxxxxxxxxx> ha scritto nel messaggio
news:839f1b64-d4e8-469e-a86e-d005133cf094@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

Cannot be helped, old application uses char* all over the place and
I'm not re-writing the *whole* program.

OK, you do want to use char*.

However, I would really suggest to use a robust string class, and avoid
strtok (which has several problems, like exposing code to buffer overruns
attacks, etc.).

You may consider this code for splitting a string into substrings (and I
used std::string, so it is compatible with your need of char *):

<code>

typedef std::vector< std::string > StringList;

//
// Split a string into substrings using delimiters.
//
void Split(
StringList & substrings, // [out]
const std::string & s, // [in]
const std::string & delimiters )// [in]
{
// Clear output parameter
substrings.clear();

//
// Each token in input string is delimited by two "pointers":
// 'begin' and 'end'.
// These "pointers" (indexes in string) are updated during
// string splitting, they are moved scanning input string.
//

// Skip delimiters at beginning (if any)
string::size_type begin = s.find_first_not_of( delimiters, 0 );

// Find first non-delimiter
string::size_type end = s.find_first_of( delimiters, begin );

// Splitting loop
while (end != string::npos || begin != string::npos)
{
// Found a token: add it to the substring vector
substrings.push_back( s.substr( begin, end - begin ) );

// Skip delimiters
begin = s.find_first_not_of( delimiters, end );

// Find next non-delimiter
end = s.find_first_of( delimiters, begin );
}
}

</code>

You can use the Split function like this:

<code>

void TestSplit()
{
std::string input = "Jim Joe Jeff John";
StringList names;
Split( names, input, " ");

// Print substrings
int count = (int)names.size();
for ( int i = 0; i < count; i++ )
{
std::cout << names[i].c_str() << std::endl;
}
}

</code>

HTH,
Giovanni



.



Relevant Pages

  • Re: Less is not More
    ... not produce an increase in the string's KC toward its maximum upper ... You are implying that larger substrings have constant or ... digits of computable constant X" with n as an argument, ... 24 ways I can concatenate them once each into a single string, ...
    (talk.origins)
  • Re: (?{..}) and lexical scoping issues.
    ... > @_ is most definitely a package variable. ... > (You are counting how many substrings of each string are also substrings ... > all substrings of a string. ... it will be hard to beat with a Perl program. ...
    (comp.lang.perl.misc)
  • Re: Einlesen einer GROSSEN Textdatei
    ... In Wirklichkeit gibt es knapp 50 Substrings, ... muss ich den String vorher leeren. ... zeilenBean = new MeineZeilenBean; ... anzahlVorkommen = errorCount1; ...
    (de.comp.lang.java)
  • Re: Parse String
    ... it picking up substrings beginning with 'f' followed by hyphens, ... Function gnaf(s As String) As Variant ... Exit Do 'inner Do ... Loop ...
    (microsoft.public.excel.programming)
  • Re: re sub help
    ... >inside the string, there are "\n". ... I don't want to substitute the '\n' ... Sometimes splitting and processing the pieces selectively can be a solution, e.g., ... Maybe I'll try two pairs of delimiters: ...
    (comp.lang.python)