Smart questions
Smart answers
Smart people
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Member Login

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips now!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

Join Tek-Tips
*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

LINK TO THIS FORUM!

Add Stickiness To Your Site By Linking To This Professionally Managed Technical Forum.
Just copy and paste the
code below into your site.

Partner With Us!

"Best Of Breed" Forums Add Stickiness To Your Site
Partner Button
(Download This Button Today!)

Feedback

"...The enviroment is simple, natural and efficient. The members are competent, educated and professionals..."

Geography

Where in the world do Tek-Tips members come from?

Listview-box connecting ;-seperated textfileHelpful Member! 

david71 (Programmer)
6 Aug 01 6:33
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
Helpful Member!  2ffat (Programmer)
6 Aug 01 7:48
ad.1 -- See Thread101-112945 and www.bcbcaq.com 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.

david71 (Programmer)
6 Aug 01 8:12
You are great!!
David
2ffat (Programmer)
6 Aug 01 9:47
ad.2
    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.

    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

    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.

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

#pragma hdrstop

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


    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 "pressed." If my form (FORM1) has a bitbtn, I would put my code in the "click" method. So my code would go into void __fastcall Form1::BitBtn1Click(TObject *Sender)

    So here we go:

// First set up the file name to be parsed.
String FileName = "D:\\SomeDirectory\\SomeSubDirectory\\TheFile.txt";

// 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("Could not find the file.", "File Error", 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("Could not process file.", "File Error", 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();
}


    That's it. You will probably notice some errors that crept in as I translated this code from my "working" 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.

david71 (Programmer)
7 Aug 01 2:46
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

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Back To Forum

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close