@@ -375,7 +375,7 @@ bool ExternalFileUnit::BeginReadingRecord(IoErrorHandler &handler) {
375
375
if (access == Access::Sequential) {
376
376
if (endfileRecordNumber && currentRecordNumber >= *endfileRecordNumber) {
377
377
handler.SignalEnd ();
378
- } else if (isFixedRecordLength) {
378
+ } else if (isFixedRecordLength && access == Access::Direct ) {
379
379
RUNTIME_CHECK (handler, recordLength.has_value ());
380
380
auto need{
381
381
static_cast <std::size_t >(recordOffsetInFrame_ + *recordLength)};
@@ -406,7 +406,7 @@ void ExternalFileUnit::FinishReadingRecord(IoErrorHandler &handler) {
406
406
// avoid bogus crashes in END/ERR circumstances
407
407
} else if (access == Access::Sequential) {
408
408
RUNTIME_CHECK (handler, recordLength.has_value ());
409
- if (isFixedRecordLength) {
409
+ if (isFixedRecordLength && access == Access::Direct ) {
410
410
frameOffsetInFile_ += recordOffsetInFrame_ + *recordLength;
411
411
recordOffsetInFrame_ = 0 ;
412
412
} else {
@@ -444,17 +444,17 @@ bool ExternalFileUnit::AdvanceRecord(IoErrorHandler &handler) {
444
444
} else { // Direction::Output
445
445
bool ok{true };
446
446
RUNTIME_CHECK (handler, isUnformatted.has_value ());
447
- if (isFixedRecordLength && recordLength) {
447
+ if (isFixedRecordLength && recordLength &&
448
+ furthestPositionInRecord < *recordLength) {
448
449
// Pad remainder of fixed length record
449
- if (furthestPositionInRecord < *recordLength) {
450
- WriteFrame (
451
- frameOffsetInFile_, recordOffsetInFrame_ + *recordLength, handler);
452
- std::memset (Frame () + recordOffsetInFrame_ + furthestPositionInRecord,
453
- isUnformatted.value_or (false ) ? 0 : ' ' ,
454
- *recordLength - furthestPositionInRecord);
455
- }
456
- } else {
457
- positionInRecord = furthestPositionInRecord;
450
+ WriteFrame (
451
+ frameOffsetInFile_, recordOffsetInFrame_ + *recordLength, handler);
452
+ std::memset (Frame () + recordOffsetInFrame_ + furthestPositionInRecord,
453
+ isUnformatted.value_or (false ) ? 0 : ' ' ,
454
+ *recordLength - furthestPositionInRecord);
455
+ furthestPositionInRecord = *recordLength;
456
+ }
457
+ if (!(isFixedRecordLength && access == Access::Direct)) {
458
458
if (isUnformatted.value_or (false )) {
459
459
// Append the length of a sequential unformatted variable-length record
460
460
// as its footer, then overwrite the reserved first four bytes of the
@@ -464,6 +464,7 @@ bool ExternalFileUnit::AdvanceRecord(IoErrorHandler &handler) {
464
464
// headers &/or footers
465
465
std::uint32_t length;
466
466
length = furthestPositionInRecord - sizeof length;
467
+ positionInRecord = furthestPositionInRecord;
467
468
ok = ok &&
468
469
Emit (reinterpret_cast <const char *>(&length), sizeof length,
469
470
sizeof length, handler);
@@ -498,7 +499,7 @@ void ExternalFileUnit::BackspaceRecord(IoErrorHandler &handler) {
498
499
DoImpliedEndfile (handler);
499
500
if (frameOffsetInFile_ + recordOffsetInFrame_ > 0 ) {
500
501
--currentRecordNumber;
501
- if (isFixedRecordLength) {
502
+ if (isFixedRecordLength && access == Access::Direct ) {
502
503
BackspaceFixedRecord (handler);
503
504
} else {
504
505
RUNTIME_CHECK (handler, isUnformatted.has_value ());
0 commit comments