Board index » cppbuilder » Ran into a file problem

Ran into a file problem


2006-05-28 11:01:26 AM
cppbuilder61
Can someone help me out with this?
I was using this program on my pda, it uses some format of file
to store the data. The data can be read from a desktop app,
plus the pda app too. Somehow something went wrong with the
data file, I really need to recover the information in the file. I
can't
seem to get in touch with the author, but I am hoping that a few lines
of code will allow me to retrieve what I lost.
The data looks like this ex.
F r i d a y D e c e m b e r 1 6 , 2 0 0 5
I don't know if that's unicode or not.
I tried a quick approach by doing a search and replace for every space
I could find, but I didn't think that the line would end up being.
FridayDecember16,2005
instead of
Friday December 16, 2005
Does anyone have a parser for doing something like this already? I
don't do
parsing too good.
Thanks
Steven
 
 

Re:Ran into a file problem

"Steven" < XXXX@XXXXX.COM >wrote:
Quote

[...] The data looks like this ex.

F r i d a y D e c e m b e r 1 6 , 2 0 0 5

I don't know if that's unicode or not.
It is not.
Quote
I tried a quick approach by doing a search and replace for
every space I could find,
The trick is to work baskwards on each string. Try this
(untested):
TStringList *pList = new TStringList();
pList->LoadFromFile( "SomeFile" );
for( int x = 0; x < pList->Count; ++x )
{
for( int y = pList->Strings[x].Length(); y>1; --y )
{
if( pList->Strings[x][y] == ' ' && pList->Strings[x][y - 1] != ' ' )
{
pList->Strings[x].Delete( y, 1 );
}
}
}
pList->SaveToFile( "UseNewNameHereOrYouMightBeSorry" );
delete pList;
~ JD
 

Re:Ran into a file problem

Quote
The trick is to work baskwards on each string. Try this
(untested):
delete pList;

~ JD
Appreciate it JD, this sure freaked me out. I have tons of entries
in there that I just put in yesterday, then all of a sudden it's
messed
up.
Thanks again
Steven
 

{smallsort}

Re:Ran into a file problem

JD,
I tried the code, but it seems nothing ever changes with the string.
When I look at the file in wordpad, it shows the spaces as a white
square box. In a hex editor, the chars are displayed as 00, meaning
a space.
Is there a way to identify what the char is and replace it? Its seems
like that may be the only possible solution for me to retrieve my data
back. Unless I sit there and do back spaces for a few years :)
 

Re:Ran into a file problem

"Steven" < XXXX@XXXXX.COM >wrote:
Quote

[...] In a hex editor, the chars are displayed as 00, meaning
a space.
That is not a space. IIRC, 0x20 is a space and of course,
0x00 is NULL which presents a problem because I don't know
how TStringList will deal with that in terms of loading and
breaking lines and line Length.
The basic premis of working backwards and testing [y] & [y - 1]
and deleting [y] remains unchanged. However, instead of
testing for a char value (' '), test for a hex value (0x00).
Do note that in the original sample, I tested for space in
both compares. Use a hex editor to determine what you should
actually be testing for.
In addition to changing the compare, you'll most likely need
to change how you load the file. If using a TStringList doesn't
work, you'll have to use a TFileStream that you read directly
into an AnsiString *after* you set the AnsiString's length.
I'm a former U.S. Marine so I've been out and about this week
end so I am unable (at the moment) to provide exact details.
However, the details are in the help and easy to find if you
know what you're looking for.
~ JD
 

Re:Ran into a file problem

Quote
That is not a space. IIRC, 0x20 is a space and of course,
0x00 is NULL which presents a problem because I don't know
Appreciate it JD, I will see how I can test for the value and then
kill it.
I know they are NULL's after looking at it in the hexeditor.
I will do search and see ow to test for a hex value, I have never
done
that one before.
Thanks Again.
 

Re:Ran into a file problem

"Steven" < XXXX@XXXXX.COM >wrote:
Quote

[...] search and see ow to test for a hex value, I have
never done that one before.
Character data is 8 bits which is just another number
irrespective of the base. I suggested using base-16
(aka hex) because you referenced the data in that way.
In the original sample I used a litteral character for the
compare:
if( pList->Strings[x][y] == ' ' && pList->Strings[x][y - 1] != ' ' )
{
pList->Strings[x].Delete( y, 1 );
}
but I could have just as well used a numeric with a hex value
of 0x20 or a decimal value of 32:
if( pList->Strings[x][y] == 0x20 && pList->Strings[x][y - 1] != 32 )
Now, you'll have to examine the data and determine the actual
logic for the compare and which characters to delete from the
file but the rest is here. Note that I did not add error
checking so if you get unexpected results (like a short file)
you'll have to add it in to see where it's going wrong:
//-------------------------------------------------------------
void __fastcall TForm1::Button1Click(TObject *Sender)
{
AnsiString Buffer;
TFileStream *In = new TFileStream( "SomeFile", fmOpenRead );
In->Seek( 0, soFromBeginning );
Buffer.SetLength( In->Size );
In->Read( Buffer.c_str(), In->Size );
delete In;
for( int x = Buffer.Length(); x>1; --x )
{
if( Buffer[x] == 0x00 && Buffer[x - 1] != 0x00 )
{
Buffer.Delete( x, 1 );
}
}
TFileStream *Out = new TFileStream( "SomeOtherFile", fmCreate );
Out->Seek( 0, soFromBeginning );
Out->Write( Buffer.c_str(), Buffer.Length() );
delete Out;
}
//-------------------------------------------------------------
~ JD
 

Re:Ran into a file problem

JD, I was just working on this again. I used a regular file
created in notepad, I put the following information in there.
H e l l o T h i s a t e s t
After it was all processed, absolutely nothing happened. I
suppose there is something going on when the space is being
removed, somehow they are not being removed at all.
I wil try the new code and see if that works.
Thanks
 

Re:Ran into a file problem

It works now, I did a little reformatting of the file, kinda copy and
paste
into a new text file, plus some other things.
Thanks again JD.
Steven