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!

Populating a listbox from a text file

Status
Not open for further replies.

holdingprice

Programmer
Apr 8, 2003
5
GB
Can anyone help me by suggesting some code to populate a listbox from an already created .txt file.

I have written a Microsoft console application and am now converting it into a Borland visual application.

This is a new environment for me!
 
I haven't tried this but it might work. I'm doing this from memory so look carefully at the code for bugs.
Code:
// Don't forget to #include <fstream> in the header file
std::ifstream CommFile; // stream set up
InputFile.open(&quot;SomeTextFile.txt&quot;); // Use your text file here

// Check File
if (!InputFile)
{
    // Something went wrong
    Application->MessageBox(&quot;Could not open text file for processing!&quot;, &quot;File Error&quot;, MB_OK);
    return; // Exit method (optional depending on how you impliment this)
}
else
{
    // File opened OK. Now get data
    std::string ALine;
    int j = 0; 

    // Proceed as long as there is data;
    while (std::getline(CommFile, ALine, '\n'))
    {
         // Stuff the data into the box
         MyListBox->Items->Strings[j] = ALine.c_str();
         // The above line converts the standard string into 
         // a character array and puts in into the ListBox
         // named MyListBox.

         // If ListBox insists you use AnsiString you could do
         // String NewStrLine = ALine.c_str();
         // MyListBox->Items->Strings[j] = NewStrLine;

         ++j; // Increments line counter
    }
}

Like I say, I've never done it but this should work. You would need to put this code in someplace where the program starts up maybe on the form's OnCreate method. I like to use standard streams but there are other ways to get text from a file, use your preference. James P. Cottingham

When a man sits with a pretty girl for an hour, it seems like a minute. But let him sit on a hot stove for a minute and it's longer than any hour. That's relativity.
[tab][tab]Albert Einstein explaining his Theory of Relativity to a group of journalists.
 
I personally like fgets and fopen.

#include &quot;stdio.h&quot;

FILE *file;

if ((file = fopen(&quot;SOMEfile.TXT&quot;, &quot;r&quot;)) != NULL)
{
char *buff = new char[256];
int j = 0;

while (!feof (file))
{
fgets (buff, 256, file);
MyListBox->Items->Strings[j] = buff;
j++;
}

fclose (file);
}

tomcruz.net
 
Looking at butthead's code I notice that I forget to close my file
Code:
InputFile.close()
. Also I noticed another error.
Code:
while (std::getline(CommFile, ALine, '\n'))
should be
Code:
while (std::getline(InputFile, ALine, '\n'))
. There are probably other errors, too, knowing me.

You could also use VCL's FileOpen, FileRead, and FileClose. That's one of the nice things about C++, there's almost always more than one way to get something done. James P. Cottingham

When a man sits with a pretty girl for an hour, it seems like a minute. But let him sit on a hot stove for a minute and it's longer than any hour. That's relativity.
[tab][tab]Albert Einstein explaining his Theory of Relativity to a group of journalists.
 
Thanx,

Got it working fine now [smile] only problem is sort one problem and the next isnt far away!
 
If the text in the file is separated by \n (newlines), you can load it up in a TStringList.

TStringList *List = new TStringList();

List->LoadFromFile(FileName);

Since TListBox uses TStrings, you can do the same thing directly.

ListBox1->Items->LoadFromFile(FileName);

Chris
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top