Re:Parsing a file
"Jonathan Benedicto" <
XXXX@XXXXX.COM >wrote in message
Quote
the OP is VCL only
What makes you think that? The OP did not say one way or another what
environment is being used.
Quote
his file contains quoted spaces
Not according to the sample that the OP actually showed.
Quote
std::auto_ptr<TStringList>InFileData( new TStringList() );
std::auto_ptr<TStringList>LineData( new TStringList() );
std::auto_ptr<TStringList>OutFileData( new TStringList() );
<snip>
Your code does not do what the OP asked for. Assuming the source file
contains blocks of data that are always 4 lines of text followed by a blank
line then a more accurate approach would be more like the following instead:
#include <memory>
std::auto_ptr<TStringList>InFileData( new TStringList() );
std::auto_ptr<TFileStream>OutFileData( new
TFileStream("filename_parsed.txt", fmCreate) );
InFileData->LoadFromFile( "filename.txt" );
for(int i = 1; i <= InFileData->Count; ++i)
{
if( (i % 5) == 0 )
OutFileData->Write("\r\n", 2);
else
{
AnsiString Line = InFileData->Strings[i-1];
OutFileData->Write(Line.c_str(), Line.Length());
OutFileData->Write("\t", 1);
}
};
You could then optimize the reading of the source file to read a line at a
time from the file so that the entire file is not loaded into memory at one
time.
Gambit