Listview-box connecting ;-seperated textfile
Listview-box connecting ;-seperated textfile
(OP)
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
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
RE: Listview-box connecting ;-seperated textfile
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.
RE: Listview-box connecting ;-seperated textfile
David
RE: Listview-box connecting ;-seperated textfile
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.
RE: Listview-box connecting ;-seperated textfile
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