_Rwrite() — Write the Next Record

Format

#include <recio.h>

_RIOFB_T * _Rwrite(_RFILE *fp, void *buf, size_t size);

Language Level

ILE C Extension

Threadsafe

Yes

However, if the file pointer is passed among threads, the I/O feedback area is shared among those threads.

Description

The _Rwrite() function has two modes: move and locate. When buf points to a user buffer, _Rwrite() is in move mode. When buf is NULL, the function is in locate mode.

The _Rwrite() function appends a record to the file specified by fp. The number of bytes copied from buf to the record is the minimum of size and the record length of the file (move mode only). If size is greater than the record length, the data is truncated and errno is set to ETRUNC. One complete record is always written if the operation is successful.

If you are using _Ropen() and then _Rwrite() to output records to a source physical file, the sequence numbers must be manually appended.

The _Rwrite() function has no effect on the position of the file for a subsequent read operation.

Records might be lost although the _Rwrite() function indicates success when the following items are true:
  • Record blocking is taking place.
  • The file associated with fp is approaching the limit of the number of records it can contain and the file cannot be extended.
  • Multiple writers are writing to the same file.
Because the output is buffered, the _Rwrite() function returns success that indicates the record is successfully copied to the buffer. However, when the buffer is flushed, the function might fail because the file has been filled to capacity by another writer. In this case, the _Rwrite() function indicates that an error occurred only on the call to the _Rwrite() function that sends the data to the file.

The _Rwrite() function is valid for all types of files.

Return Value

The _Rwrite() function returns a pointer to the _RIOFB_T structure that is associated with fp. If the _Rwrite() operation is successful the num_bytes field is set to the number of bytes written for both move mode and locate mode. The function transfers the bytes from the user's buffer to the system buffer. If record blocking is taking place, the function only updates the rrn and key fields when it sends the block to the database. If fp is a display, ICF or printer file, the function updates the sysparm field. If it is unsuccessful, the num_bytes field is set to a value less than size specified (move mode) or zero (locate mode) and errno is changed.

The value of errno may be set to:
Value
Meaning
ENOTWRITE
The file is not open for write operations.
ETRUNC
Truncation occurred on an I/O operation.
EIOERROR
A non-recoverable I/O error occurred.
EIORECERR
A recoverable I/O error occurred.
See Table 1 and Table 1 for errno settings.

Example

#include <stdio.h>
#include <recio.h>
#include <string.h>
#include <stdlib.h>
typedef struct {
    char name[20];
    char address[25];
} format1 ;
typedef struct {
    char name[8];
    char password[10];
} format2 ;
typedef union {
    format1 fmt1;
    format2 fmt2;
} formats ;
 
int main(void)
{
    _RFILE   *fp; /* File pointer                                    */
    _RIOFB_T *rfb; /*Pointer to the file's feedback structure        */
    _XXIOFB_T *iofb; /* Pointer to the file's feedback area          */
    formats  buf, in_buf, out_buf; /* Buffers to hold data           */
 /* Open the device file.                                         */
    if (( fp = _Ropen ( "MYLIB/T1677RD2", "ar+" )) == NULL )
    {
        printf ( "Could not open file\n" );
        exit ( 1 );
    }
    _Racquire ( fp,"DEVICE1" );    /* Acquire another device. Replace*/
                                   /* with actual device name.       */
    _Rformat ( fp,"FORMAT1" );     /* Set the record format for the  */
                                   /* display file.                  */
    rfb = _Rwrite ( fp, "", 0 );   /* Set up the display.            */
    _Rpgmdev ( fp,"DEVICE2" );  /* Change the default program device.*/
                                /* Replace with actual device name.  */
    _Rformat ( fp,"FORMAT2" );   /* Set the record format for the    */
                                 /* display file.                    */
    rfb = _Rwrite ( fp, "", 0 );   /* Set up the display.            */
    rfb = _Rwriterd ( fp, &buf, sizeof(buf) );
    rfb = _Rwrread ( fp, &in_buf, sizeof(in_buf), &out_buf,
                     sizeof(out_buf ));
   _Rreadindv ( fp, &buf, sizeof(buf), __DFT );
                                  /* Read from the first device that */
                                  /* enters data - device becomes    */
                                  /* default program device.         */
 /* Determine which terminal responded first.                     */
    iofb = _Riofbk ( fp );
    if ( !strncmp ( "FORMAT1  ", iofb -> rec_format, 10 ))
    {
        _Rrelease ( fp, "DEVICE1" );
    }
    else
    {
        _Rrelease(fp, "DEVICE2" );
    }
 /* Continue processing.                                          */
    printf ( "Data displayed is %45.45s\n", &buf);
    _Rclose ( fp );
}
 

Related Information