_Rwrite () -写入下一个记录
格式
#include <recio.h>
_RIOFB_T * _Rwrite(_RFILE *fp, void *buf, size_t size);语言级别
ILE C 扩展
线程安全
是
但是,如果文件指针在线程之间传递,那么 I/O 反馈区域将在这些线程之间共享。
描述
_Rwrite() 函数有两种方式: 移动和定位。 当 buf 指向用户缓冲区时, _Rwrite() 处于移动方式。 当 buf 为 NULL 时,该函数处于定位方式。
_Rwrite() 函数将记录附加到 fp指定的文件。 从 buf 复制到记录的字节数是文件的最小 大小 和记录长度 (仅移动方式)。 如果大小大于记录长度,那么将截断数据并将 errno 设置为 ETRUNC。 如果操作成功,那么将始终写入一条完整记录。
如果使用 _Ropen() ,然后使用 _Rwrite() 将记录输出到源物理文件,那么必须手动追加序号。
_Rwrite() 函数对后续读操作的文件位置没有影响。
虽然
_Rwrite() 函数指示在以下项为 true 时成功,但可能会丢失记录:- 正在进行记录分块。
- 与 fp 关联的文件已接近其可包含的记录数限制,无法扩展该文件。
- 多个写程序正在写入同一个文件。
_Rwrite() 函数将返回指示记录已成功复制到缓冲区的成功。 但是,当清空缓冲区时,该函数可能会失败,因为文件已由另一个写程序填充到容量。 在这种情况下, _Rwrite() 函数指示仅在调用将数据发送到文件的 _Rwrite() 函数时发生错误。_Rwrite() 函数对所有类型的文件都有效。
返回值
_Rwrite() 函数返回指向与 fp关联的 _RIOFB_T 结构的指针。 如果 _Rwrite() 操作成功,那么 num_bytes 字段将设置为针对移动方式和定位方式写入的字节数。 此函数将字节从用户的缓冲区传输到系统缓冲区。 如果正在进行记录分块,那么该函数仅在将块发送到数据库时更新 rrn 和 key 字段。 如果 fp 是显示, ICF 或打印机文件,那么该函数将更新 sysparm 字段。 如果不成功,那么 num_bytes 字段将设置为小于指定的 size (移动方式) 或零 (定位方式) 的值,并且会更改 errno。
errno 的值可以设置为:
- 值
- 含义
- ENOTWRITE
- 文件未针对写操作打开。
- ETRUNC
- 在 I/O 操作上发生截断。
- EIOERROR
- 发生了不可恢复的I/O错误。
- EIORECERR
- 发生了可恢复的I/O错误。
示例
#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 );
}