pro wrtQS, lun, fpoint, bytout, qs_struct, qs_struct2
;
;
;Input:
;	bytout	- when entered, it should point to the end of the data section
;		  it will be updated if the QS section has been expanded
;	qs_struct2	- Optional structure to be saved (only used by BCS)
;
qdebug = 1
;
rsiz = 0	;RDWRT will not perform any checks
n1 = get_nbytes(qs_struct) + get_nbytes(qs_struct2) ;#bytes to write
n2 = fpoint.data_section - fpoint.qs_section        ;#bytes originally / space allocated for QS section
off = n1-n2
;
if (off gt 0) then begin
    if (qdebug) then print, '#b original, #now, off', n2, n1, off
    err_log, 'STR', 'WRN/GEN: QS Section Larger - moving file back ' + strtrim(off,2) + ' bytes'
    ;
    buff = bytarr(off)
    rdwrt, 'W', lun, bytout, rsiz, buff		;extend size of file by necessary # of bytes
						;to avoid errors pointing past the end of the file
    ;
    ist = fpoint.data_section
    ien = bytout-16				;back off 16 bytes to grab one record
    if (qdebug) then print, 'WRTQS: Expanding file ', ist, ien
    buff = bytarr(16)
    for ibyt = ien,ist,-16 do begin
	rdwrt, 'R', lun, ibyt, rsiz, buff
	rdwrt, 'W', lun, ibyt+off, rsiz, buff 
    end

    bytout = bytout + off			;update end of data section pointer
end
;
ibyt = fpoint.qs_section
rdwrt, 'W', lun, ibyt, rsiz, qs_struct, 1
if (n_elements(qs_struct2) ne 0) then rdwrt, 'W', lun, ibyt, rsiz, qs_struct2, 1
if (ibyt gt fpoint.data_section) then fpoint.data_section = ibyt	;if data was moved back, then update pointer
;
end
