_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 複製到記錄的位元組數下限為 size 及檔案的記錄長度 (僅限移動模式)。 如果大小大於記錄長度,則會截斷資料,並將錯誤碼設為 ETRUNC。 如果作業成功,一律會寫入一筆完整記錄。
如果您使用 _Ropen() ,然後使用 _Rwrite() 將記錄輸出至來源實體檔,則必須手動附加序號。
_Rwrite() 函數不會影響後續讀取作業的檔案位置。
當下列項目為真時,雖然
_Rwrite() 函數指出成功,但記錄可能會遺失:- 正在進行記錄封鎖。
- 與 fp 相關聯的檔案接近其可包含的記錄數限制,且無法延伸檔案。
- 多個寫出器正在寫入相同的檔案。
_Rwrite() 函數會傳回成功,指出記錄已順利複製到緩衝區。 不過,當清除緩衝區時,函數可能會失敗,因為檔案已由另一個寫出器填入容量。 在此情況下, _Rwrite() 函數指出僅在呼叫將資料傳送至檔案的 _Rwrite() 函數時發生錯誤。_Rwrite() 函數適用於所有類型的檔案。
回覆值
_Rwrite() 函數會傳回與 fp相關聯之 _RIOFB_T 結構的指標。 如果 _Rwrite() 作業成功,則 num_bytes 欄位會設為針對移動模式及尋找模式寫入的位元組數。 此函數會將位元組從使用者的緩衝區傳送至系統緩衝區。 如果正在進行記錄區塊處理,則該函數只會在將區塊傳送至資料庫時更新 rrn 及 索引鍵 欄位。 如果 fp 是顯示器、ICF 或印表機檔案,則函數會更新 sysparm 欄位。 如果不成功,則 num_bytes 欄位會設為小於指定 size (移動模式) 或零 (尋找模式) 的值,並且會變更錯誤碼。
錯誤碼的值可以設為:
- Value
- 意義
- 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 );
}