Solved

Error using ListBox item in C++Builder

Posted on 2000-04-13
12
491 Views
Last Modified: 2012-05-04
Well, I need to create a ListBox item that lists names recieved out of a file.  So far, I think my coding is right, all runs fine until I try to use this certain area of my program.  The program will abort and give me this error:

Project Trodholm.exe raised exception class EStringListError with message 'List index out of bounds (0)'. Process stopped. Use Step or Run to continue.

here is my source code for it:

void charstuff::ListChars(void) {
   CodeIt(PlayFile);
   PlayerFile = fopen(PlayFile, "r+");
   CharCount = -1;
   while (!feof(PlayerFile)) {

      fscanf(PlayerFile,yourname.c_str());

   fscanf(PlayerFile,yourpassword.c_str());
   CharCount++;
   LoadCharForm->CharListBox->Items->Strings[CharCount]
      = yourname.c_str();
   }
   fclose(PlayerFile);
   CodeIt(PlayFile);
}
0
Comment
Question by:nfcdrummer
  • 5
  • 4
  • 3
12 Comments
 
LVL 6

Expert Comment

by:graham_k
ID: 2714188
well, you don't say which line is giving problems & withoout the full project, we can only code read. If any sugegstions here don't help, perhpas you can zip up the project & put it somewhere that we can d/l it (if its not too private).

here are some thoughts:

- I never used fscanf() before, but if it's like scanf() and scscanf() then the first parameter is a source e.g file handle, the next is a format string an then come some parameters. So, if you're trying to read in a string from the file, you might want to use

  char name[255], password[255];
  fscanf(PlayerFile, "%s %s",  name, password);

note that you can't read _into_ AnsiString::c_str() it _outputs_ the char * value of an AnsiString - it's read only.

Also, TListBox->Items->Strings[] is an array (a vector, actually) of type AnsiString, so
LoadCharForm->CharListBox->Items->Strings[CharCount] = yourname.c_str();  in your example above should be LoadCharForm->CharListBox->Items->Strings[CharCount] = yourname;   // note, no .c_str()    if yoruname were of type AnsiString.  If it is a char[] or char* (samne thing), use = AnsiString (yourname);


Let me know if that helps, or if any of it is not clear.
0
 
LVL 6

Expert Comment

by:graham_k
ID: 2714206
hmm, I've just re-read the question and, while I stand by all of what I wrote, it doesn't explain the problem with charCount.

Can you stop the debugger on that line & examine CharCount and the strings. You ought to find CharCount == 0. What about LoadCharForm->CharListBox->Items->Strings.Count ?  What size is the string array? Normally, you shouldn't have to allocate any memory for it, but if, for instance, you had dynamically created your listbox at run time, you might have too.

What I'm saying here, is that I suspect that you are trying to write element zer0 of an array (vector) that doesn't even have memory allocated for a zer0th element.  I dimly remember having the same, probelm, but can't remember the answer. Can you post the code anywhere?
0
 

Author Comment

by:nfcdrummer
ID: 2714407
What I have read from the error, the out of bounds index (0) is saying I can't write into Strings[0].  I tried it at [1] as well, and it doesn't access it.  Thats my problem, its when it gets to the

LoadCharForm->CharListBox->Items->Strings[CharCount] = yourname;

line.

by the way, I noticed the problem with the c_str();, and fixed that before I checked this.

So what I'm saying is, when it has read the name out of the file, it can't assign it to Strings[0].  Thats basically it and I cannot seem to fix it.
0
Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

 

Author Comment

by:nfcdrummer
ID: 2714408
What I have read from the error, the out of bounds index (0) is saying I can't write into Strings[0].  I tried it at [1] as well, and it doesn't access it.  Thats my problem, its when it gets to the

LoadCharForm->CharListBox->Items->Strings[CharCount] = yourname;

line.

by the way, I noticed the problem with the c_str();, and fixed that before I checked this.

So what I'm saying is, when it has read the name out of the file, it can't assign it to Strings[0].  Thats basically it and I cannot seem to fix it.

Unfortunately, I do not wish to post my source code as it is private, and I don't have the time to upload it I'm sorry.
0
 
LVL 6

Expert Comment

by:graham_k
ID: 2714529
don't worry about not posting private code. That's quite o.k.  With a C++ Builder project it would be all or nothing anyway, if we were to debug it.

Now, what the error is telling you is that LoadCharForm->CharListBox->Items->Strings has no members. No one index by [1], and not even one indexed by [0]. There is simply no memory allocated for these strings.

Now, we have to ask why. if you create your listbox at design time by selecting the standard listbox component from the toolbar & dropping it onto your form, then C++ Builder _should_ automatically create the memory for you.

If not, you mnust allocate it yourself with something like LoadCharForm->CharListBox->Items->Strings = new TStringList  In which case, don't forget to delete the stringlist at the appropriate time.

I am in the office now (Japan) but will play about with some TListBOx stuff this weekend & look at some of my own old code.

In the meantime, try as I said, examine LoadCharForm->CharListBox->Items->Strings, especially LoadCharForm->CharListBox->Items->Strings->Count just before it crashes.


STOP PRESS - am I not thinking stright?  Instead of assigning LoadCharForm->CharListBox->Items->Strings[0] = sometext;

why not use LoadCharForm->CharListBox->Items->Strings.Add(sometext);  ?


That ought to allocate the memory. I'm *sure* that that is it. Sorry, but I'm trying to concentrate on something else @ work. That should have obvious to me from the start.

Note that you may want to Clear() the strings before starting the loop, so that when you call the fn() again, you don;t end up adding a second copy of each string.
0
 
LVL 1

Expert Comment

by:jbodom
ID: 2714923
Here's your problem--graham_k is right:
You've not allocated memory for items in the listbox.  Instead of

LoadCharForm->CharListBox->Items-Strings[CharCount]= yourname.c_str();

Try this:
LoadCharForm->CharListBox->Items->Add(yourname.c_str());

This should work fine.
   
0
 
LVL 6

Accepted Solution

by:
graham_k earned 100 total points
ID: 2714955
well, thanks for backing me up (and proposing my comment as your answer)


btw not

LoadCharForm->CharListBox->Items->Add(yourname.c_str());

but rather

LoadCharForm->CharListBox->Items->Add(yourname);
0
 

Author Comment

by:nfcdrummer
ID: 2715008
because you stole graham_k's answer

thanks for your help graham_k
0
 

Author Comment

by:nfcdrummer
ID: 2715017
you are very welcome
0
 
LVL 6

Expert Comment

by:graham_k
ID: 2715053
well, thanks for the points. More important is getting the problem solved. Sorry that it took me a few hours for the answer to occur. But at least you got to see how my thinking (if you can call that) works & we tidied up a few other things with fscanf() and c_str().
0
 
LVL 1

Expert Comment

by:jbodom
ID: 2715723
Well, maybe I should've made it a comment but as far as I saw, graham k didn't post the code to make it work.  I don't see how you think that I stole his answer.  As a matter of fact he even stated "I dimly remember having the same, probelm, but can't remember the answer".  Not a big deal, I was just trying to help and I'm pretty sure, you're using the code that I showed you.
0
 
LVL 1

Expert Comment

by:jbodom
ID: 2716093
Either of these work fine:

LoadCharForm->CharListBox->Items->Add(yourname.c_str());

LoadCharForm->CharListBox->Items->Add(yourname);

0

Featured Post

Problems using Powershell and Active Directory?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
C Language combined operators 28 109
Least Squares Curve Fitting 4 77
Problem with SqlConnection 4 169
draw a Christmas tree by using a nested loop? 26 62
Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question