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

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.

Comments

: 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.