 |
BorlandTalk.com Borland discussion newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Steven Guest
|
Posted: Tue May 16, 2006 4:14 am Post subject: Parsing into commatext |
|
|
I have a file with a few hundred lines that I am trying to convert
into commatext values. The values are like this
1 1
2 2
3 3
4 4
5 5
6 6
7 7
8 8
Each record has four lines then a space. How can I parse this
entire text file, even though it has the space after each record,
and output each record as commatext into a new file?
i.e.
"1 1","2 2","3 3","4 4"
"5 5","6 6","7 7","8 8"
Appreciate the help, this is killing me....... |
|
| Back to top |
|
 |
Clayton Arends Guest
|
Posted: Tue May 16, 2006 7:14 am Post subject: Re: Parsing into commatext |
|
|
Here is some psuedocode for a straight-forward no error detection approach:
While not Input.EOF do
begin
for i = 1 to 4
begin
str = Input.ReadLine
if i > 1
Output.Write(comma)
Output.Write(quote)
Output.Write(str)
Output.Write(quote)
end
Input.ReadLine
Output.WriteLine
end
- Clayton |
|
| Back to top |
|
 |
Thorsten Kettner Guest
|
Posted: Tue May 16, 2006 10:14 am Post subject: Re: Parsing into commatext |
|
|
"Steven" <stef (AT) bondo (DOT) edu> wrote:
| Quote: | I have a file with a few hundred lines that I am trying to
convert into commatext values. The values are like this
[snip] |
As you are asking in vcl.components.using group, here is one
simple VCL solution:
TStringList* pStringList = new TStringList;
pStringList->LoadFromFile(InputFile);
AnsiString TempString = pStringList->Text.Trim();
TempString = StringReplace(TempString, "\r\n", "\",\"", TReplaceFlags() << rfReplaceAll);
TempString = StringReplace(TempString, "\",\"\",\"", "\"\r\n\"", TReplaceFlags() << rfReplaceAll);
TempString = "\"" + TempString + "\"";
pStringList->Text = TempString;
pStringList->SaveToFile(OutputFile);
delete pStringList; |
|
| Back to top |
|
 |
JD Guest
|
Posted: Tue May 16, 2006 3:14 pm Post subject: Re: Parsing into commatext |
|
|
"Thorsten Kettner" <ANSWERBYthorsten.kettner (AT) otto (DOT) de> wrote:
| Quote: |
As you are asking in vcl.components.using group, here is one
simple VCL solution:
|
Your sample fails to eliminate the extra space found at the
end of each set of numbers and it completely removes all line
breaks so the output is one long string.
~ JD |
|
| Back to top |
|
 |
JD Guest
|
Posted: Tue May 16, 2006 3:14 pm Post subject: Re: Parsing into commatext |
|
|
"Steven" <stef (AT) bondo (DOT) edu> wrote:
| Quote: |
[...] How can I parse this entire text file, even though it
has the space after each record, and output each record as
commatext into a new file?
|
#include <memory>
//-------------------------------------------------------------
// create a data file as specified
void __fastcall TForm1::Button1Click(TObject *Sender)
{
AnsiString S;
std::auto_ptr<TStringList> pList ( new TStringList );
for( int x = 0; x < 21; ++x )
{
for( int y = 0; y < 11; ++y )
{
pList->Add( S.sprintf("%02d %02d ", x, y) );
}
}
pList->SaveToFile( "Test.txt" );
Memo1->Lines->Clear();
for( int x = 0; x < pList->Count; ++x ) Memo1->Lines->Add( pList->Strings[x] );
}
//-------------------------------------------------------------
// convert the data file to CommaText
void __fastcall TForm1::Button2Click(TObject *Sender)
{
char Quote = '"';
std::auto_ptr<TStringList> In ( new TStringList );
std::auto_ptr<TStringList> Out ( new TStringList );
In->LoadFromFile( "Test.txt" );
AnsiString S = "";
for( int x = 0; x < In->Count; ++x )
{
S += AnsiQuotedStr( In->Strings[x].Trim(), Quote );
if( (x % 4) == 3 )
{
Out->Add( S );
S = "";
}
else S += ",";
}
if( !S.IsEmpty() ) // just in case
{
Out->Add( S.Delete( S.Length(), 1) );
}
Out->SaveToFile( "NewTest.txt" );
Memo1->Lines->Clear();
for( int x = 0; x < Out->Count; ++x ) Memo1->Lines->Add( Out->Strings[x] );
}
//-------------------------------------------------------------
// Parse the CommaText file
void __fastcall TForm1::Button3Click(TObject *Sender)
{
Memo1->Lines->Clear();
std::auto_ptr<TStringList> Table ( new TStringList );
std::auto_ptr<TStringList> Record ( new TStringList );
Table->LoadFromFile( "NewTest.txt" );
for( int x = 0; x < Table->Count; ++x )
{
Record->CommaText = Table->Strings[x];
for( int y = 0; y < Record->Count; ++y )
{
Memo1->Lines->Add( Record->Strings[y] );
}
}
}
//-------------------------------------------------------------
~ JD |
|
| Back to top |
|
 |
Steven Guest
|
Posted: Wed May 17, 2006 12:14 am Post subject: Re: Parsing into commatext |
|
|
| Quote: | Your sample fails to eliminate the extra space found at the
end of each set of numbers and it completely removes all line
breaks so the output is one long string.
|
Hey JD,
I noticed that, I was kinda wondering what to do about it. Thanks
for the fix.
Steven |
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|