🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Thread View: borland.public.cppbuilder.nativeapi
21 messages
21 total messages Started by Safari Sat, 05 Apr 2003 18:14
Split file.
#17367
Author: Safari
Date: Sat, 05 Apr 2003 18:14
11 lines
1191 bytes
Hello,
How can i split a file with TFileStream?
I want to read Exmp:
1..50,000 bytes of 1,000,000 bytes to MyFile.001
50,001..100,000 bytes of 1,000,000 bytes to MyFile.002
...
//Continue while eof(MyFile);

// Source code or web page address.
Thanks,
Safari
Re: Split file.
#17368
Author: Remy Lebeau (TeamB)
Date: Sat, 05 Apr 2003 23:42
52 lines
2512 bytes
"Safari" <Safari@alloymail.com> wrote in message
news:3E8F1D05.40008@alloymail.com...

> How can i split a file with TFileStream?
> I want to read Exmp:
> 1..50,000 bytes of 1,000,000 bytes to MyFile.001
> 50,001..100,000 bytes of 1,000,000 bytes to MyFile.002

Simply read the file in a loop, each iteration of the loop reading a block
of data and processing it as needed, continuing until the loop has read the
entire stream in full.  For example:

    int iSourceFile = FileOpen("C:\\somefile.ext", fmOpenRead |
fmShareDenyWrite);
    if( iSourceFile == -1 )
    {
        BYTE buffer[50000];
        int iNum = 1;

        do
        {
            int iRead = FileRead(iSourceFile, buffer, 50000);
            if( iRead != -1 )
            {
                if( iRead == 0 )
                    break;    // finished

                AnsiString ext = Format("%.3d", ARRAYOFCONST(( iNum++ )));

                int iNewFile = FileCreate("c:\\MyFile." + ext);
                if( iNewFile != -1 )
                {
                    if( FileWrite(iNewFile, buffer, iRead) != iRead )
                        // an error occured, handle it as needed

                    FileClose(iNewFile);
                }
                else
                    // an error occured, handle it as needed
            }
            else
                // an error occured, handle it as needed
        }
        while( true );
        FileClose(iSourceFile);
    }
    else
        // an error occured, handle it as needed


Gambit
Re: Split file.
#17144
Author: Safari
Date: Mon, 07 Apr 2003 16:25
49 lines
2356 bytes
Hello Remy,
I wrote this code before your answer,
return me "Stream File Error..."
Where is my mistake?

Thanks,
Safari

void __fastcall TMain::BitBtn1Click(TObject *Sender)
{
    int b = 0;
    int hExten = 0;
    BYTE hBufer[1];
    int hBytes = Edit3->Text.ToInt();
    if (FileExists(Edit1->Text))
    {
       TFileStream *hSource = new TFileStream(Edit1->Text,
       fmOpenRead | fmShareDenyWrite);
       Progress->Max = hSource->Size;
       for (int a = 0; a < hSource->Size; a++)
       {
          AnsiString hName = Format("%s.%.3d", ARRAYOFCONST((
          Edit2->Text, hExten++)));
          TFileStream *hTarget = new TFileStream(hName, fmCreate);
          do
          {
             b += 1;
             Progress->Position = hSource->Position;
             hSource->ReadBuffer (hBufer, 1);
             hTarget->WriteBuffer(hBufer, 1);
          }
          while ((b = hBytes) | (b > hSource->Size));
          delete hTarget;
       }
       delete hSource;
       Progress->Position = 0;
    }
}


Remy Lebeau (TeamB) wrote:
> "Safari" <Safari@alloymail.com> wrote in message
> news:3E8F1D05.40008@alloymail.com...
> 
> 
>>How can i split a file with TFileStream?
>>I want to read Exmp:
>>1..50,000 bytes of 1,000,000 bytes to MyFile.001
>>50,001..100,000 bytes of 1,000,000 bytes to MyFile.002
Re: Split file.
#17149
Author: Remy Lebeau (TeamB)
Date: Mon, 07 Apr 2003 17:24
45 lines
2514 bytes
"Safari" <Safari@alloymail.com> wrote in message
news:3E91A68B.8000503@alloymail.com...

> I wrote this code before your answer,
> return me "Stream File Error..."
> Where is my mistake?

You didn't indicate where in the code the error is actually coming from, or
what the full error message was.  Please always be more specific when
reporting errors.  However, there are a few problems with your code that is
preventing it from working correctly.

>     BYTE hBufer[1];

This is not a problem, just a comment - you don't need a 1-element array for
that:

    BYTE hBufer;
    ...
    hSource->ReadBuffer(&hBufer, 1);
    hTarget->WriteBuffer(&hBufer, 1);

>           while ((b = hBytes) | (b > hSource->Size));

You're using the wrong operator in there.  You should be using ||, not |.

Why are you comparing the sizes like that?  I think you have your loop
comparisons mixed up.  I looks like you're assuming that the loop will
continue until one of those conditions becomes 'true', while is not the
actual case.  Are you coming from a VB background, by chance?  Your
comparisons should be more like this instead:

    while ((b != hBytes) && (b < hSource->Size));

I'm assuming hBytes is the number of bytes you want each split chunk file to
be?  Why are you splitting the file one byte at a time instead of in larger
chunks to begin with?  You're going to cause a lot of unnecessary overhead
with file I/O doing it that way.

Was there some problem preventing you from using the code I gave you
earlier?


Gambit
Re: Split file.
#17116
Author: Safari
Date: Wed, 09 Apr 2003 18:30
52 lines
2672 bytes
Hello Remy,
1. I fail to write or complete this code! return me two errors.
    {a. Stream read error.
    {b. File1: MyFile.001, File2: MyFile.003, File3: MyFile.005 and ...
2. Your simple code dosn't write any file!, excuse me.

Thanks a lot,
Safari

Remy Lebeau (TeamB) wrote:
> Simply read the file in a loop, each iteration of the loop reading a block
> of data and processing it as needed, continuing until the loop has read the
> entire stream in full.  For example:
> 
>     int iSourceFile = FileOpen("C:\\somefile.ext", fmOpenRead |
> fmShareDenyWrite);
>     if( iSourceFile == -1 )
>     {
>         BYTE buffer[50000];
>         int iNum = 1;
> 
>         do
>         {
>             int iRead = FileRead(iSourceFile, buffer, 50000);
>             if( iRead != -1 )
>             {
>                 if( iRead == 0 )
>                     break;    // finished
> 
>                 AnsiString ext = Format("%.3d", ARRAYOFCONST(( iNum++ )));
> 
>                 int iNewFile = FileCreate("c:\\MyFile." + ext);
>                 if( iNewFile != -1 )
>                 {
>                     if( FileWrite(iNewFile, buffer, iRead) != iRead )
>                         // an error occured, handle it as needed
> 
>                     FileClose(iNewFile);
>                 }
>                 else
>                     // an error occured, handle it as needed
>             }
>             else
>                 // an error occured, handle it as needed
>         }
>         while( true );
>         FileClose(iSourceFile);
>     }
>     else
>         // an error occured, handle it as needed
> 
> Gambit
Re: Split file.
#17117
Author: Remy Lebeau (TeamB)
Date: Wed, 09 Apr 2003 21:46
62 lines
2896 bytes
"Safari" <Safari@alloymail.com> wrote in message
news:3E9466BC.8060508@alloymail.com...

> 1. I fail to write or complete this code! return me two errors.
>     {a. Stream read error.

The only time that can happen is inside TStream::ReadBuffer() when
TStream::Read() returns a value other than the requested amount.  Which
makes sense if you are requesting 50000 bytes and it reads fewer (due to end
of file, for example).

The same applies to WriteBuffer() as well - it will throw an exception if
the amount written does not match the amount requested.

You should be calling Read() and Write() instead of ReadBuffer() and
WriteBuffer().

> 2. Your simple code dosn't write any file!, excuse me.

The following code works fine for me:

    int iSourceFile = FileOpen("C:\\test.rtf", fmOpenRead |
fmShareDenyWrite);
    if( iSourceFile != -1 )
    {
        BYTE buffer[50000];
        int iNum = 1;

        do
        {
            int iRead = FileRead(iSourceFile, buffer, 50000);
            if( iRead != -1 )
            {
                if( iRead == 0 )
                    break;    // finished

                AnsiString ext = Format("%.3d", ARRAYOFCONST(( iNum )));
                ++iNum;

                int iNewFile = FileCreate("C:\\test." + ext);
                if( iNewFile != -1 )
                {
                    if( FileWrite(iNewFile, buffer, iRead) != iRead )
                        // an error occured, handle it as needed

                    FileClose(iNewFile);
                }
                else
                    // an error occured, handle it as needed
            }
            else
                // an error occured, handle it as needed
        }
        while( true );
        FileClose(iSourceFile);
    }
    else
        // an error occured, handle it as needed


Gambit
Re: Split file.
#17035
Author: Safari
Date: Thu, 10 Apr 2003 11:24
40 lines
2199 bytes
Very tanaks Remy,
I used your source and worked fine.
1. How can i choose size file with your source now?
2. My source return me: Stream read error! why?

void __fastcall TMain::BitBtn1Click(TObject *Sender)
{
     BYTE hBuf;
     int hBin = 0;
     int hExt = 0;
     int hLen = Edit3->Text.ToInt();
     if (FileExists(Edit1->Text))
     {
        TFileStream *hSource = new TFileStream(Edit1->Text, fmOpenRead |
        fmShareDenyWrite);
        Progress->Max = hSource->Size;
        for (int a = 0; a < hSource->Size; a++)
        {
           hExt++;
           AnsiString hName = Format("%s.%.3d",
           ARRAYOFCONST((Edit2->Text, hExt));
           hExt));
           TFileStream *hTarget = new TFileStream(hName, fmCreate);
           do
           {
              hBin++;
              hSource->ReadBuffer (&hBuf, 1);
              hTarget->WriteBuffer(&hBuf, 1);
              Progress->Position = hSource->Position;
           }
           while (hBin != hLen);
           delete hTarget;
        }
        delete hSource;
        Progress->Position = 0;
     }
}

Thanks again,
Safari
Re: Split file.
#17037
Author: Remy Lebeau (TeamB)
Date: Fri, 11 Apr 2003 01:27
42 lines
2539 bytes
"Safari" <Safari@alloymail.com> wrote in message
news:3E955473.6000807@alloymail.com...

> I used your source

No, you're not.  I've posted my code twice for you now, but your code, as
you've posted it here, still does not resemble my code at all.  You're off
doing your own things.  My code does not use TFileStream and sets aside a
fixed-sized buffer and then read/wrote the files in full 50000b blocks at a
time.  Your code, on the other hand, is using TFileStream and is
reading/writing the files one byte at a time instead.

> 1. How can i choose size file with your source now?

Your code, as you've posted it here, is already taking that into account.
Although, your outer for() loop is very very wrong, you're not managing the
loop counter correctly at all.  If you're going to continuing using your own
code instead of mine, then you need to use a while() loop instead of a for()
loop, ie:

    void __fastcall TMain::BitBtn1Click(TObject *Sender)
    {
        ...
        // this loop is wrong!
        // for (int a = 0; a < hSource->Size; a++)

        while( hSource->Position < hSource->Size )
        {
            ...
        }
    }

> 2. My source return me: Stream read error! why?

If you had actually read my other replies, I've already explained to you
exactly why that is occuring.  Do not call ReadBuffer() and WriteBuffer(),
use Read() and Write() instead.  You're getting an exception because you are
using methods that are specifically designed to throw an exception.


Gambit
Re: Split file.
#17043
Author: Asger J�rgensen
Date: Fri, 11 Apr 2003 05:42
20 lines
1457 bytes
Hi Remy

  In article <3e9619fe$1@newsgroups.borland.com>,  "Remy Lebeau \(TeamB\)" 
<gambit47@yahoo.com> says :

> 
> "Safari" <Safari@alloymail.com> wrote in message
> news:3E955473.6000807@alloymail.com...
> 
> > I used your source
> 
> No, you're not. 

As i read his answer i think he is using Your code and 
then he post his own code to get an answer to his last 
question..;-)
But You already gave him that so.....

Best regards
Asger
Re: Split file.
#17044
Author: Remy Lebeau (TeamB)
Date: Fri, 11 Apr 2003 06:41
13 lines
1557 bytes
"Asger J�rgensen" <Asger.Pnotthis@AJ-World.dk> wrote in message
news:MPG.19007419b0bed1e79896d2@newsgroups.borland.com...

> As i read his answer i think he is using Your code and
> then he post his own code to get an answer to his last
> question..;-)

He should always be posting the code he's actually using, not using one code
and then posting with a completely different one.


Gambit
Re: Split file.
#17051
Author: Phil V
Date: Fri, 11 Apr 2003 13:06
18 lines
1729 bytes
As well as Remy's comment about using the wrong operator in the or '|'
instead of '||' I believe you are also using the wrong comparison operator,
you have b = hBytes, which means that you are setting b to be hBytes,
whereas I think you mean b == hBytes, which means, 'does b equate to
hBytes?'

Phil

"Remy Lebeau (TeamB)" <gambit47@yahoo.com> wrote in message
news:3e91b462$1@newsgroups.borland.com...
>
> "Safari" <Safari@alloymail.com> wrote in message
> news:3E91A68B.8000503@alloymail.com...
>
<SNIP>
> >           while ((b = hBytes) | (b > hSource->Size));
>
> You're using the wrong operator in there.  You should be using ||, not |.
Re: Split file.
#17073
Author: Safari
Date: Sat, 12 Apr 2003 03:45
65 lines
3014 bytes
Hello again Remy,
I say my source return error, not your source.
My source here is:

Procedure TMain.BitBtn3Click(Sender: TObject);
Var hSource, hTarget: TFileStream; hChoose: String;
     hBuf: Byte; hBin, hExt, hInt: Integer; hLen: Longint;
Begin
    hBin := 0;
    hExt := 0;
    hLen := StrToInt(Edit3.Text);
    hSource := TFileStream.Create(Edit1.Text, fmOpenRead
    or fmShareDenyWrite);
    Progress.Max := hSource.Size;
    for hInt := 0 to hSource.Size - 1 do
    Begin
       Inc(hExt);
       hChoose := Format('%s.%.3d', [Edit2.Text, hExt]);
       hTarget := TFileStream.Create(hChoose, fmCreate);
       Repeat
          Inc(hBin);
          hSource.ReadBuffer (hBuf, 1);
          hTarget.WriteBuffer(hBuf, 1);
          Progress.Position := hSource.Size;
       Until hBin = hLen;
       hTarget.Free;
    End;
    hSource.Free;
    Progress.Position := 0;
End;
-------------------------------------------------------------------------
void __fastcall TMain::BitBtn1Click(TObject *Sender)
{
    BYTE hBuf;
    int hBin = 0;
    int hExt = 0;
    int hLen = Edit3->Text.ToInt();
    if (FileExists(Edit1->Text))
    {
       TFileStream *hSource = new TFileStream(Edit1->Text,
       fmOpenRead | fmShareDenyWrite);
       Progress->Max = hSource->Size;
       for (int a = 0; a < hSource->Size; a++)
       {
          hExt++;
          AnsiString hName = Format("%s.%.3d", ARRAYOFCONST((Edit2->Text,
          hExt)));
          TFileStream *hTarget = new TFileStream(hName, fmCreate);
          do
          {
             hBin++;
             hSource->ReadBuffer (&hBuf, 1);
             hTarget->WriteBuffer(&hBuf, 1);
             Progress->Position = hSource->Position;
          }
          while (hBin != hLen);
          delete hTarget;
       }
       delete hSource;
       Progress->Position = 0;
    }
}

Thanks a lot,
Safari
Re: Split file.
#17074
Author: Safari
Date: Sat, 12 Apr 2003 03:46
44 lines
2475 bytes
Remy Thanks^(n+1),
Your source here is, work very fine & success.

void __fastcall TMain::BitBtn1Click(TObject *Sender)
{
    int iSourceFile = FileOpen(Edit1->Text, fmOpenRead
    | fmShareDenyWrite);
    if(iSourceFile != -1)
    {
       BYTE hBuf[50000];
       int iNum = 1;
       do
       {
          int iRead = FileRead(iSourceFile, hBuf, 50000);
          if(iRead != -1)
          {
             if(iRead == 0) break;
             AnsiString ext = Format("%.3d", ARRAYOFCONST((iNum)));
             ++iNum;
             int iNewFile = FileCreate(Edit2->Text + ext);
             if(iNewFile != -1)
             {
                if(FileWrite(iNewFile, hBuf, iRead) != iRead)
                FileClose(iNewFile);
             }
          }
       }
       while(true);
       FileClose(iSourceFile);
    }
}

Remy Lebeau (TeamB) wrote:
 > "Safari" <Safari@alloymail.com> wrote in message
 > news:3E955473.6000807@alloymail.com...
  >
 > No, you're not.  I've posted my code twice for you now, but your code, as
 > you've posted it here, still does not resemble my code at all. 
You're off
 > doing your own things.  My code does not use TFileStream and sets aside a
 > fixed-sized buffer and then read/wrote the files in full 50000b 
blocks at a
 > time.  Your code, on the other hand, is using TFileStream and is
 > reading/writing the files one byte at a time instead.
Re: Split file.
#17072
Author: Safari
Date: Sat, 12 Apr 2003 03:55
11 lines
1402 bytes
Hello,
You said to me:
Try to use "Read/Write(&hBuf, 1)"
instead "ReadBuffer/WriteBuffer(&hBuf, 1)"

Okey.
Thanks,

Remy Lebeau (TeamB) wrote:
 > "Safari" <Safari@alloymail.com> wrote in message
 > news:3E955473.6000807@alloymail.com...
Re: Split file.
#16975
Author: Safari
Date: Sat, 12 Apr 2003 04:02
15 lines
1296 bytes
Hello,
You said to me:
Try to use "Read/Write(&hBuf, 1)"
instead "ReadBuffer/WriteBuffer(&hBuf, 1)"

Okey.
Thanks,


"Remy Lebeau (TeamB)" <gambit47@yahoo.com> wrote in message
news:3e9619fe$1@newsgroups.borland.com...
|
| "Safari" <Safari@alloymail.com> wrote in message
| news:3E955473.6000807@alloymail.com...
Re: Split file.
#16976
Author: Remy Lebeau (TeamB)
Date: Sat, 12 Apr 2003 06:03
16 lines
1580 bytes
"Safari" <Safari@alloymail.com> wrote in message
news:3E978BC8.2020907@alloymail.com...

> I say my source return error, not your source.

And I originally asked you if there was some reason why you weren't using my
source.  You never answered that question.

I've already made several comments repeatedly regarding problems with your
source, and I see you still haven't taken them into account yet.  Please go
re-read my earlier replies regarding how to fix your code, and stop posting
the same broken code over and over.


Gambit
Re: Split file.
#17002
Author: Safari
Date: Sun, 13 Apr 2003 12:05
22 lines
1583 bytes
Hello Remy,
I used your source and thanks,
but i want to complete my code
with your help. I want to use
from TFileStream.

Thanks,
Safari

"Remy Lebeau (TeamB)" <gambit47@yahoo.com> wrote in message
news:3e97ac20$1@newsgroups.borland.com...
|
| "Safari" <Safari@alloymail.com> wrote in message
| news:3E978BC8.2020907@alloymail.com...
|
| > I say my source return error, not your source.
|
| And I originally asked you if there was some reason why you weren't using
my
| source.  You never answered that question.
|
| Gambit
Re: Split file.
#17006
Author: Remy Lebeau (TeamB)
Date: Mon, 14 Apr 2003 01:31
14 lines
1431 bytes
"Safari" <Safari@alloymail.com> wrote in message
news:3e99f994@newsgroups.borland.com...

> I used your source and thanks,
> but i want to complete my code
> with your help. I want to use
> from TFileStream.

Please re-read my earlier responses, I already pointed out how to fix
problems with your own code.


Gambit
Re: Split file.
#16942
Author: Safari
Date: Tue, 15 Apr 2003 18:31
22 lines
1688 bytes
Hello & Thanks Remy,
I not receive your replay!
Please send again.

Thanks a lot,
Safari

"Remy Lebeau (TeamB)" <gambit47@yahoo.com> wrote in message
news:3e9a0f44@newsgroups.borland.com...
|
| "Safari" <Safari@alloymail.com> wrote in message
| news:3e99f994@newsgroups.borland.com...
|
| > I used your source and thanks,
| > but i want to complete my code
| > with your help. I want to use
| > from TFileStream.
|
| Please re-read my earlier responses, I already pointed out how to fix
| problems with your own code.
|
| Gambit
Re: Split file.
#16943
Author: Remy Lebeau (TeamB)
Date: Wed, 16 Apr 2003 00:49
18 lines
1772 bytes
"Safari" <Safari@alloymail.com> wrote in message
news:3e9ca2b5@newsgroups.borland.com...

> I not receive your replay!
> Please send again.

Please reset your newsreader to download previous messages again.  Or else
just go to www.deja.com or www.mers.com and search through the archives.
For example, our entire conversation is available here:

http://groups.google.com/groups?th=2ef5348ed2d12068
http://groups.google.com/groups?th=2ef5348ed2d12068

Namely, your loops are faulty and you're using the wrong TStream methods.


Gambit
Re: Split file.
#16958
Author: Safari
Date: Wed, 16 Apr 2003 00:53
22 lines
1896 bytes
Very thanks Remy,
Safari

"Remy Lebeau (TeamB)" <gambit47@yahoo.com> wrote in message
news:3e9ca86b$1@newsgroups.borland.com...
|
| "Safari" <Safari@alloymail.com> wrote in message
| news:3e9ca2b5@newsgroups.borland.com...
|
| > I not receive your replay!
| > Please send again.
|
| Please reset your newsreader to download previous messages again.  Or else
| just go to www.deja.com or www.mers.com and search through the archives.
| For example, our entire conversation is available here:
|
| http://groups.google.com/groups?th=2ef5348ed2d12068
| http://groups.google.com/groups?th=2ef5348ed2d12068
|
| Namely, your loops are faulty and you're using the wrong TStream methods.
|
| Gambit
Thread Navigation

This is a paginated view of messages in the thread with full content displayed inline.

Messages are displayed in chronological order, with the original post highlighted in green.

Use pagination controls to navigate through all messages in large threads.

Back to All Threads