Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

Error using ListBox item in C++Builder

Posted on 2000-04-13
12
Medium Priority
?
557 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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 400 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.
Suggested Courses

580 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