6 Aug 01 9:47
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
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:
// Add the following comments and code manually
// Standard C++ libraries
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);
// 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
// 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?
// No! there was an error opening file
Application->MessageBox("Could not process file.", "File Error", MB_OK);
// 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
// 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
// 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
// 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
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.