Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

strncmp resulting in OutOfRange error

Status
Not open for further replies.

XNOR

Programmer
Nov 15, 2001
17
US
hi fellow programmers, a bit of help if you will. :)

I am writing a program to search and replace certain lines/strings in a memo/richedit. Here is the code:
void __fastcall TForm1::Button2Click(TObject *Sender)
{

int i;
int n=0;
int token = 0;

//this sets up some strings so i can do a quick search for the word
char keyword[2][17]={"ZSOLDADDR1Z", "ZSOLDADDR2Z"};
//this allows me to use key codes to use with the caseswitch
enum{ZSOLDADDR1Z, ZSOLDADDR2Z};

//this increments each line
for(n=0; n<RichEdit1->Lines->Count; n++)
{
//this sets up my string for searching
String string=RichEdit1->Lines->Strings[n]+&quot; ~&quot;;
//obviously, the keyword has to strat with 'Z' to make things easy
if(string[1]=='Z')
{
//this is where i search each enum and compare it to the strings. It is also where i get my error.(at the if)
for(i=0; i<=2; i++)
if(strncmp(keyword, &string[n], strlen(keyword))==0)
token=i;

//does the good stuff once keyword is found
switch(token)
{
//this area incomplete, cant get here yet to worry about it
case ZSOLDADDR1Z: RichEdit1->Lines->Strings[n]=Edit2->Text ; break;
case ZSOLDADDR2Z: break;
default: break;
}
}
}
}

In the strncmp where its in the if statement, i have three items. the first two are the items to be compared and the third the lenght to compare. At that point keyword == the enum strings and &string[n] == the line that the pointer is at... considering it gets to ZSOLDADDR1Z, borland kicks me out and points to ThrowIfOutOfRange(idx); in dstring.h.

If you could give me a hint of a reason why this small function is not working, i would be much obliged.

if i need to clarify, please be specific. if i need to shove off, be nice ;)
 
Uhm, ok. Well, i fixed the problem by just doing a whole new project. Dont ask me why those problems arise... but if you know, please do tell.

Now if i may, ill move away silently to avoid any ridicule due to my buggy code ;)
 
the errors in your code :

first you define a AnsiString named string to read the strings out of your RichEdit :

String string=RichEdit1->Lines->Strings[n]+&quot; ~&quot;;

and later on you use :
if(strncmp(keyword, &string[n], strlen(keyword))==0)

first error : if you use string[n], there is supposed to be an array of string while you only initialised one,

second : strncmp asks for a c_str string (char *), if you want to convert a AnsiString to a c_str you have to use .c_str() for the AnsiString.

Correct would be :
if(strncmp(keyword, string.c_str(), strlen(keyword))==0)

also (a bit earlier) :if(string[1]=='Z')
must be : if (string.c_str()[0]=='Z')

string is the AnsiString you get out of the RichEdit.
string.c_str() is the conversion of that AnsiString to a c_str
string.c_str() gives the character of position i in that string (the first character has index 0)

I know there is a lot of confusion about char* strings (the classical strings of c++) and AnsiStrings, but it is essential.

regards

Wim Vanherp


Wim Vanherp
Wim.Vanherp@myself.com
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top