// 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();
}