Difficulty w/ Append to an Existing File

I have read a lot of threads regarding this, and all prior solutions do not seem to resolve my issue.

I have created a Report with nested dataitems. a line of code in our business logic will call this report if a certain condition is met. The report will create a CSV file if none exists, and append to the CSV file when it does exist.

Creation of the file is clockwork. no problems. I am, however, having issues with the append. Even after I SEEK (LEN), the new data is written to the beginning of the file. It also ‘overwrites’ part of the first line, which is only written during file creation. I assume both issues are related to some mistake I am making.

I have three data items.

  1. Integer (OnAfterGetRecord I write field label data, but only when the file is being created)
  2. Sales Header (OnAfterGetRecord, I fill some variables with Sales Header values)
  3. Sales Line (linked to the header, on AfterGetRecord, I fill some var. w/ data from tables linked to sales line and do my outstream.write).

On PreReport, I have the following code:

Code: Select all //Mibuso sample code for appending to an existing file. FileName is dynamic so function gets file name

POFile.TEXTMODE(TRUE);
POFile.WRITEMODE(TRUE);
IF FileForExport THEN
POFile.OPEN(sFileName)
ELSE POFile.CREATE(sFileName);
IF POFile.LEN > 0 THEN
POFile.SEEK(POFile.LEN);
POFile.CREATEOUTSTREAM(POOutstream);

FileForExport() FileFound : Boolean
//temporary hard-coded directory to store CSV files for upload to Customer
sFilePath := ‘\fps\test’; CLEAR(recPOFile);
recPOFile.SETFILTER(Path,’%1’,sFilePath);
recPOFile.SETFILTER(Name,’%1’,’@*.csv’);
recPOFile.SETRANGE(“Is a file”,TRUE);
IF NOT recPOFile.FIND(’-’) THEN BEGIN
sNoSeries := ‘WM_POTRACK’;
NoSeriesMgt.InitSeries(sNoSeries,sNoSeries,0D,sBatchNo,sNoSeries);
sFile := OutVendorNo + ‘_’ + sBatchNo + ‘.csv’;
sFileName := sFilePath + sFile;
bCreateCSV := TRUE;
EXIT(FALSE)
END ELSE BEGIN
sFileName := sFilePath + recPOFile.Name;
EXIT(TRUE);
END;

it is still early draft of code, so forgive the hard-coding. my issue is that when i open the existing file and seek the LEN, I begin writing at the start of the file, not the end. I assume I am missing a line of code somewhere, but my search of the forum only talks about using LEN and going from there. Any thoughts on what i am doing wrong? thanks!

My file looks “like” this after initial creation:

Vendor,PO,Item
339242,ABCD,I-123
339242,ABCD,I-456

My file should look “like” this after append (file exists, add to end):

Vendor,PO,Item
339242,ABCD,I-123
339242,ABCD,I-456
339242,EFGH,I-456
339242,EFGH,I-789
339242,EFGH,I-123

What I am actually getting, however, looks like this:

339242,EFGH,I-456
339242,EFGH,I-789
339242,EFGH,I-123
PO,Item
339242,ABCD,I-123
339242,ABCD,I-456

Notice that the new data is added to the beginning of the file, and the ‘header’ line was impacted as well.

With help from DaveT at MiBuso, I got rid of the outstream, and outstream.WRITETEXT I was using in favor of file.WRITE. Now it works as I wanted.

Solved by using WRITE instead on OUTSTEAM

See the details here

http://www.mibuso.com/forum/viewtopic.php?f=23&t=27718&p=150384#p150384