Board index » cppbuilder » help -plz....--file code....(a simple one)

help -plz....--file code....(a simple one)


2005-11-02 02:12:40 AM
cppbuilder0
// end_of_file is bool
void __fastcall TForm1::Button1Click(TObject *Sender)
{
OpenDialog1->Execute();
Edit1->Text=OpenDialog1->FileName;
TFileStream *t1=new TFileStream(Edit1->Text,fmOpenRead);
if(FileCreate("d:\\trial.cpy")==-1)
Button2->Click();
TFileStream *t2=new TFileStream(s,fmOpenWrite|fmShareDenyNone);
t1->Seek(0,0);
t2->Seek(0,0);
while(end_of_file==false)
{
if(t1->Read((char *)&c,1)==1)
t2->Write((char *)&c,1);
else
end_of_file=true;
}
t1->Free();
t2->Free();
t1->~TFileStream();
t2->~TFileStream();
}
here's the simple code...if u have a nice code plz send it here
 
 

Re:help -plz....--file code....(a simple one)

DILIP K wrote:
Are you trying to copy a file from one place to another ? Because if you
are, Windows includes a CopyFile function that makes it much simpler.
Quote
OpenDialog1->Execute();
If the user clicks Cancel on the OpenDialog, then your code will carry on
executing. You should test to check whether the user cancelled the dialog
before continuing.
Quote
if(FileCreate("d:\\trial.cpy")==-1)
Why are you creating this file ? Also, you should close this file after
you've created it.
Quote
TFileStream *t2=new TFileStream(s,fmOpenWrite|fmShareDenyNone);
What is s ?
Quote
t1->Free();
t2->Free();
t1->~TFileStream();
t2->~TFileStream();
To free objects, simply delete them. Here some code with all those
suggestions put together: (untested)
void __fastcall TForm1::Button1Click(TObject *Sender)
{
// Open the dialog, and if it is cancelled, then return.
if( !OpenDialog1->Execute() )
return;
Edit1->Text = OpenDialog1->FileName;
TFileStream *t1 = new TFileStream( Edit1->Text, fmOpenRead );
int Result = FileCreate( "d:\\trial.cpy" );
if( Result < 0 )
Button2->Click();
else
FileClose( Result );
TFileStream *t2 = new TFileStream( s, fmOpenWrite |
fmShareDenyNone );
t1->Seek( 0, soFromBeginning );
t2->Seek( 0, soFromBeginning );
while( true )
{
char c;
if( t1->Read( &c, sizeof( char ) ) < sizeof( char ) )
break;
t2->Write( &c, sizeof( char ) );
};
delete t1;
delete t2;
}
HTH
Jonathan
 

Re:help -plz....--file code....(a simple one)

"Jonathan Benedicto" < XXXX@XXXXX.COM >wrote:
Quote

[...]
while( true )
{
char c;

if( t1->Read( &c, sizeof( char ) ) < sizeof( char ) )
break;

t2->Write( &c, sizeof( char ) );
};
t2->CopyFrom( t1, t1->Size );
~ JD
 

{smallsort}

Re:help -plz....--file code....(a simple one)

JD wrote:
Quote
t2->CopyFrom( t1, t1->Size );
I was just copying the OP's code incase he had some specific reason for
copying it char-by-char. Otherwise, I'd have used CopyFile.
Jonathan
 

Re:help -plz....--file code....(a simple one)

DILIP K wrote:
You did not try very hard to find a suitable Subject.
How about: 'Trying to read from and write to a file'. ?
And words like help and please do not belong in a Subject.
Hans.
 

Re:help -plz....--file code....(a simple one)

"Remy Lebeau \(TeamB\)" < XXXX@XXXXX.COM >wrote:
Quote

"JD" < XXXX@XXXXX.COM >wrote in message
news:4367dc0c$ XXXX@XXXXX.COM ...

>t2->CopyFrom( t1, t1->Size );

t2->CopyFrom( t1, 0 );
LOL. Ok!
~ JD
 

Re:help -plz....--file code....(a simple one)

"JD" < XXXX@XXXXX.COM >wrote in message
Quote
t2->CopyFrom( t1, t1->Size );
t2->CopyFrom( t1, 0 );
Gambit
 

Re:help -plz....--file code....(a simple one)

Interestingly enough it still got read though didn't it.
Hans Galema wrote:
Quote
DILIP K wrote:

You did not try very hard to find a suitable Subject.

How about: 'Trying to read from and write to a file'. ?

And words like help and please do not belong in a Subject.

Hans.