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 Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Listview-box connecting ;-seperated textfile 1

Status
Not open for further replies.

david71

Programmer
Aug 6, 2001
14
NL
Hi,

ad.1
I would like to make a Listview-box. Has somebody the example sourcecode for it?

ad.2
Then I would like to make a connection to a ';' seperated textfile, so that the contents of the textfile can fill the listview box made in ad.1

Thanks a lot,

David Reuvekamp
The Netherlands
 
ad.1 -- See thread101-112945 and for some examples.

ad.2 -- I'll post some code that I regularly use later today if someone else doesn't beat me to it.

James P. Cottingham

I am the Unknown lead by the Unknowing.
I have done so much with so little
for so long that I am now qualified
to do anything with nothing.
 
ad.2
[tab]Here is how I frequently parse files with fields delimited by something like ";" or "|". It is by no means the only method nor may it be the best but it works for me so I use it a lot. Please note that while the code works for me, I do not know the particulars about your file so you will have to modify this code.

[tab]Just for this example, let us assume that the text file you want to parse looks something like this:
I1; I2 ; i3
thing1; thing2;thing3
1; 2 ;3

[tab]Notice that I don't make any assumptions about whether there are spaces before or after the ";". I do make an assumption that there are three fields on each line. This is only for the example.

[tab]To start out with, you will need to include the file stream and string stream libraries in your file. Include these libraries after the
Code:
#pragma hdrstop
. You code may look something like this:
Code:
#pragma hdrstop

// Add the following comments and code manually
//---------------------------------------------------------------------------
// Standard C++ libraries
#include <fstream>
#include <sstream>

[tab]Now you can start adding your parsing code. I don't know where you want to pick up your file. I usually pick up a file in the section where a button is &quot;pressed.&quot; If my form (FORM1) has a bitbtn, I would put my code in the &quot;click&quot; method. So my code would go into
Code:
void __fastcall Form1::BitBtn1Click(TObject *Sender)

[tab]So here we go:
Code:
// First set up the file name to be parsed.
String FileName = &quot;D:\\SomeDirectory\\SomeSubDirectory\\TheFile.txt&quot;;

// If you really want some error checking, do this
bool FileIsThereBool = FileExists(FileName);

if (!FileIsThereBool)
{
     // The file does not exist so do some sort of message
     Application->MessageBox(&quot;Could not find the file.&quot;, &quot;File Error&quot;, MB_OK);
     // Exit the method
     return;
}

// Now open the file
ifstream ParseFile; // input file stream set up
ParseFile.open(FileName.c_str()); // c_str will put Ansistring to char string

// Can we process the file?
if (!ParseFile)
{
    // No! there was an error opening file
    Application->MessageBox(&quot;Could not process file.&quot;, &quot;File Error&quot;, MB_OK);
    return;
}
else
{
    // Now we are going to get each line in the file before we parse it.
    // Start by setting up a C++ string to hold the file's line
    string ImpLine;

    // Now we get the line in the file up to the ending newline character
    // The while loop will get every line in the file
    while (getline(PostingFile, ImpLine, '\n'))
    {
        // Now we will process the input string into parts via string stream
        // You could also use substrings but this is cleaner, IMHO

        // Get line from input string
        istringstream StrLine(ImpLine);

        // Now let's set up the parts of the file. You could do this through
        // a ListView, a vector, or like this example, through C++ string.
        string OnePart, TwoPart, ThreePart

        // The real work of parsing the line is done here
        // Notice that the last part must pick up the linefeed since there
        // isn't a ; after it
        getline(StrLine,OnePart,';');
        getline(StrLine,TwoPart,';');
        getline(StrLine,ThreePart,'\n');

        // Now you can do what you want with the parts
        // For example, you could trim the spaces from the string
        // or you could convert them to numbers if possible.
    }

    // Now close the file
    ParseFile.close();
}

[tab]That's it. You will probably notice some errors that crept in as I translated this code from my &quot;working&quot; code to the example. Good luck! (You'll probably need it with my advice. ;-) )

James P. Cottingham

I am the Unknown lead by the Unknowing.
I have done so much with so little
for so long that I am now qualified
to do anything with nothing.
 
To James P. Cottingham,

Yhanks for your answer and the time you offered. It is for me a good start en now I will work with it.

Greetings,

David Reuvekamp
The Netherlands
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top