Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Sign In with Facebook Sign In with Google Sign In with OpenID

Categories

We have migrated to a new platform! Please note that you will need to reset your password to log in (your credentials are still in-tact though). Please contact lee@programmersheaven.com if you have questions.
Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

Can Anyone Look At this peace of code and help me figure out...

tj29tj29 Posts: 45Member
the following code all it does is gets a file and splits it into smaller files. at the moment is set up to split a large file in sets of 2megs files. it works fine. THE PROBLEM i am having is that when it splits the file it does not completes the last line instead it cuts it when it reaches the 2meg size. for example:
IF THE LAST LINE IS: "the house is next to the car."
if the 2meg limit is at the U position it will copy
"the hou" in one file and the "se is next to the car." in the next file. what i want to do is either copy the last line in the current file or just copy the whole line in the next file. thanks for you help.

here is the code i am using:

//--------------------------------------------------------------------
procedure TForm1.Button1Click(Sender: TObject);
var
FS : TFileStream;
MS : TMemoryStream;
i,zSize,OriginalFilesize,SmallerFileSize : integer;

begin
i:=0;
FS := TFileStream.Create('C:Transaction.log', fmOpenRead);
try

FS.Seek(0, soFromBeginning);
MS := TMemoryStream.Create;

try
SmallerFileSize := ((2*1024)*1024); //2 megs
OriginalFilesize := FS.Size;

{loops thru the file and copy the data in to the smaller files
while is looping it decrements the OriginalFilesize to keep
track of how much is left}

while SmallerFileSize < OriginalFilesize do
begin
OriginalFilesize := OriginalFilesize - SmallerFileSize;
MS.CopyFrom(FS, SmallerFileSize);
inc(i);
MS.Seek(0, soFromBeginning);
MS.SaveToFile('C: estfile'+inttostr(i)+'.text');
MS.Size := 0;
end;

//if there is any data remaining in the file
If (SmallerFileSize > OriginalFilesize) then
begin
MS.CopyFrom(FS, OriginalFilesize);
MS.SaveToFile('C: estfile'+inttostr(i+1)+'.text');
end;
finally
MS.Free;
end;
finally
FS.Free;
end;
end;


Comments

  • zibadianzibadian Posts: 6,349Member
    : the following code all it does is gets a file and splits it into smaller files. at the moment is set up to split a large file in sets of 2megs files. it works fine. THE PROBLEM i am having is that when it splits the file it does not completes the last line instead it cuts it when it reaches the 2meg size. for example:
    : IF THE LAST LINE IS: "the house is next to the car."
    : if the 2meg limit is at the U position it will copy
    : "the hou" in one file and the "se is next to the car." in the next file. what i want to do is either copy the last line in the current file or just copy the whole line in the next file. thanks for you help.
    :
    : here is the code i am using:
    :
    : //--------------------------------------------------------------------
    : procedure TForm1.Button1Click(Sender: TObject);
    : var
    : FS : TFileStream;
    : MS : TMemoryStream;
    : i,zSize,OriginalFilesize,SmallerFileSize : integer;
    :
    : begin
    : i:=0;
    : FS := TFileStream.Create('C:Transaction.log', fmOpenRead);
    : try
    :
    : FS.Seek(0, soFromBeginning);
    : MS := TMemoryStream.Create;
    :
    : try
    : SmallerFileSize := ((2*1024)*1024); //2 megs
    : OriginalFilesize := FS.Size;
    :
    : {loops thru the file and copy the data in to the smaller files
    : while is looping it decrements the OriginalFilesize to keep
    : track of how much is left}
    :
    : while SmallerFileSize < OriginalFilesize do
    : begin
    : OriginalFilesize := OriginalFilesize - SmallerFileSize;
    : MS.CopyFrom(FS, SmallerFileSize);
    : inc(i);
    : MS.Seek(0, soFromBeginning);
    : MS.SaveToFile('C: estfile'+inttostr(i)+'.text');
    : MS.Size := 0;
    : end;
    :
    : //if there is any data remaining in the file
    : If (SmallerFileSize > OriginalFilesize) then
    : begin
    : MS.CopyFrom(FS, OriginalFilesize);
    : MS.SaveToFile('C: estfile'+inttostr(i+1)+'.text');
    : end;
    : finally
    : MS.Free;
    : end;
    : finally
    : FS.Free;
    : end;
    : end;
    :
    :
    :
    The TFileStream and TMemoryStream treat text files as binary files. In this sense the end-of-line symbols are simply other characters to them and will be ignored. If you want to have to smaller pieces to be cut at the end-of-line symbols, you need to search from the end of the memory stream for the end-of-line symbols, and delete all other characters behind it. Then save it to file. Remember to move the file cursor in the original back with the same amount or you will lose data.
    An alternative approach to this is to use a TStringList and a normal textfile variable. Open the original file using the text file variable and read it line for line, while adding each line to the TStringList. Keep adding lines until the length of the TStringList.Text property is equal to or larger than 2MB. Then save the TStringList result, clear it, and continue adding to the now clear list. It might not be as fast as the streams option, but it is easier to code.
Sign In or Register to comment.