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.
- Integer (OnAfterGetRecord I write field label data, but only when the file is being created)
- Sales Header (OnAfterGetRecord, I fill some variables with Sales Header values)
- 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!