| 日本-日本語 |
|
|
|
|
OpenVMS マニュアル |
|
HP OpenVMS
|
| 目次 | 索引 |
2.5 端末 I/O |
HP C では,通常は端末 (対話型ジョブの場合) またはバッチ・ストリーム (バッチ・ジョブの場合) に関連付けられている論理デバイスとの間で I/O を実行するために, 3 つのファイル・ポインタが定義されています。 OpenVMS 環境では, 3 つのパーマネント・プロセス・ファイル SYS$INPUT,SYS$OUTPUT,SYS$ERROR は,対話型ジョブの場合もバッチ・ジョブの場合も同じ機能を実行します。端末 I/O は,端末 I/O とバッチ・ストリーム I/O の両方を示します。ファイル・ポインタ stdin, stdout, stderrは, #includeプリプロセッサ・ディレクティブを使用して <stdio.h>ヘッダ・ファイルを取り込むときに定義されます。
stdinファイル・ポインタは,入力を実行するために端末に関連付けられます。このファイルは SYS$INPUT に対応します。 stdoutファイル・ポインタは,出力を実行するために端末に関連付けられます。このファイルは SYS$OUTPUT に対応します。 stderrファイル・ポインタは,実行時エラーを報告するために端末に関連付けられます。このファイルは SYS$ERROR に対応します。
端末を参照する 3 つのファイル記述子があります。ファイル記述子 0 は SYS$INPUT に対応し, 1 は SYS$OUTPUT に対応し,2 は SYS$ERROR に対応します。
端末で I/O を実行する場合,標準 I/O 関数とマクロを使用でき ( 引数としてポインタ
stdin,
stdout,
stderrを指定します), UNIX I/O 関数を使用することもでき ( 引数として対応するファイル記述子を指定します ),端末 I/O 関数とマクロを指定することもできます。機能的にいずれかの I/O が他の種類の I/O より優れているというわけではありません。ただし,端末 I/O 関数の場合は引数がないため,必要なキーストロークを削減できます。
![]()
2.6 プログラムの例
![]()
ここでは,アプリケーションで I/O 関数を使用する方法を示すために,いくつかのサンプル・プログラムを示します。
例 2-1 は printf関数を示しています。
| 例 2-1 変換指定の出力 |
|---|
/* CHAP_2_OUT_CONV.C */
/* This program uses the printf function to print the */
/* various conversion specifications and their effect */
/* on the output. */
/* Include the proper header files in case printf has */
/* to return EOF. */
#include <stdlib.h>
#include <stdio.h>
#include <wchar.h>
#define WIDE_STR_SIZE 20
main()
{
double val = 123345.5;
char c = 'C';
int i = -1500000000;
char *s = "thomasina";
wchar_t wc;
wchar_t ws[WIDE_STR_SIZE];
/* Produce a wide character and a wide character string */
if (mbtowc(&wc, "W", 1) == -1) {
perror("mbtowc");
exit(EXIT_FAILURE);
}
if (mbstowcs(ws, "THOMASINA", WIDE_STR_SIZE) == -1) {
perror("mbstowcs");
exit(EXIT_FAILURE);
}
/* Print the specification code, a colon, two tabs, and the */
/* formatted output value delimited by the angle bracket */
/* characters (<>). */
printf("%%9.4f:\t\t<%9.4f>\n", val);
printf("%%9f:\t\t<%9f>\n", val);
printf("%%9.0f:\t\t<%9.0f>\n", val);
printf("%%-9.0f:\t\t<%-9.0f>\n\n", val);
printf("%%11.6e:\t\t<%11.6e>\n", val);
printf("%%11e:\t\t<%11e>\n", val);
printf("%%11.0e:\t\t<%11.0e>\n", val);
printf("%%-11.0e:\t\t<%-11.0e>\n\n", val);
printf("%%11g:\t\t<%11g>\n", val);
printf("%%9g:\t\t<%9g>\n\n", val);
printf("%%d:\t\t<%d>\n", c);
printf("%%c:\t\t<%c>\n", c);
printf("%%o:\t\t<%o>\n", c);
printf("%%x:\t\t<%x>\n\n", c);
printf("%%d:\t\t<%d>\n", i);
printf("%%u:\t\t<%u>\n", i);
printf("%%x:\t\t<%x>\n\n", i);
printf("%%s:\t\t<%s>\n", s);
printf("%%-9.6s:\t\t<%-9.6s>\n", s);
printf("%%-*.*s:\t\t<%-*.*s>\n", 9, 5, s);
printf("%%6.0s:\t\t<%6.0s>\n\n", s);
printf("%%C:\t\t<%C>\n", wc);
printf("%%S:\t\t<%S>\n", ws);
printf("%%-9.6S:\t\t<%-9.6S>\n", ws);
printf("%%-*.*S:\t\t<%-*.*S>\n", 9, 5, ws);
printf("%%6.0S:\t\t<%6.0S>\n\n", ws);
}
|
例 2-1 を実行すると,次の出力が生成されます。
$ RUN EXAMPLE %9.4f: <123345.5000> %9f: <123345.500000> %9.0f: < 123346> %-9.0f: <123346 > %11.6e: <1.233455e+05> %11e: <1.233455e+05> %11.0e: < 1e+05> %-11.0e: <1e+05 > %11g: < 123346> %9g: < 123346> %d: <67> %c: <C> %o: <103> %x: <43> %d: <-1500000000> %u: <2794967296> %x: <a697d100> %s: <thomasina> %-9.6s: <thomas > %-*.*s: <thoma > %6.0s: < > %C: <W> %S: <THOMASINA> %-9.6S: <THOMAS > %-*.*S: <THOMA > %6.0S: < > $ |
例 2-2 は, fopen, ftell, sprintf, fputs, fseek, fgets, fclose関数の使い方を示しています。
| 例 2-2 標準 I/O 関数の使用 |
|---|
/* CHAP_2_STDIO.C */
/* This program establishes a file pointer, writes lines from */
/* a buffer to the file, moves the file pointer to the second */
/* record, copies the record to the buffer, and then prints */
/* the buffer to the screen. */
#include <stdio.h>
#include <stdlib.h>
main()
{
char buffer[32];
int i,
pos;
FILE *fptr;
/* Set file pointer. */
fptr = fopen("data.dat", "w+");
if (fptr == NULL) {
perror("fopen");
exit(EXIT_FAILURE);
}
for (i = 1; i < 5; i++) {
if (i == 2) /* Get position of record 2. */
pos = ftell(fptr);
/* Print a line to the buffer. */
sprintf(buffer, "test data line %d\n", i);
/* Print buffer to the record. */
fputs(buffer, fptr);
}
/* Go to record number 2. */
if (fseek(fptr, pos, 0) < 0) {
perror("fseek"); /* Exit on fseek error. */
exit(EXIT_FAILURE);
}
/* Read record 2 in the buffer. */
if (fgets(buffer, 32, fptr) == NULL) {
perror("fgets"); /* Exit on fgets error. */
exit(EXIT_FAILURE);
}
/* Print the buffer. */
printf("Data in record 2 is: %s", buffer);
fclose(fptr); /* Close the file. */
}
|
例 2-2 を実行すると,次の結果が生成されます。
$ RUN EXAMPLE Data in record 2 is: test data line 2 |
例 2-2 から DATA.DAT への出力は次のようになります。
test data line 1 test data line 2 test data line 3 test data line 4 |
| 例 2-3 ワイド文字 I/O 関数の使用 |
|---|
/* CHAP_2_WC_IO.C */
/* This program establishes a file pointer, writes lines from */
/* a buffer to the file using wide-character codes, moves the */
/* file pointer to the second record, copies the record to the */
/* wide-character buffer, and then prints the buffer to the */
/* screen. */
#include <stdio.h>
#include <stdlib.h>
#include <wchar.h>
main()
{
char flat_buffer[32];
wchar_t wide_buffer[32];
wchar_t format[32];
int i,
pos;
FILE *fptr;
/* Set file pointer. */
fptr = fopen("data.dat", "w+");
if (fptr == NULL) {
perror("fopen");
exit(EXIT_FAILURE);
}
for (i = 1; i < 5; i++) {
if (i == 2) /* Get position of record 2. */
pos = ftell(fptr);
/* Print a line to the buffer. */
sprintf(flat_buffer, "test data line %d\n", i);
if (mbstowcs(wide_buffer, flat_buffer, 32) == -1) {
perror("mbstowcs");
exit(EXIT_FAILURE);
}
/* Print buffer to the record. */
fputws(wide_buffer, fptr);
}
/* Go to record number 2. */
if (fseek(fptr, pos, 0) < 0) {
perror("fseek"); /* Exit on fseek error. */
exit(EXIT_FAILURE);
}
/* Put record 2 in the buffer. */
if (fgetws(wide_buffer, 32, fptr) == NULL) {
perror("fgetws"); /* Exit on fgets error. */
exit(EXIT_FAILURE);
}
/* Print the buffer. */
printf("Data in record 2 is: %S", wide_buffer);
fclose(fptr); /* Close the file. */
}
|
例 2-3 を実行すると,次の結果が生成されます。
$ RUN EXAMPLE Data in record 2 is: test data line 2 |
例 2-3 から DATA.DAT への出力は次のようになります。
test data line 1 test data line 2 test data line 3 test data line 4 |
例 2-4 は,1 つのファイルにアクセスするための,ファイル・ポインタとファイル記述子の両方の使い方を示しています。
| 例 2-4 ファイル記述子とファイル・ポインタを使用した I/O |
|---|
/* CHAP_2_FILE_DIS_AND_POINTER.C */
/* The following example creates a file with variable-length */
/* records (rfm=var) and the carriage-return attribute (rat=cr).*/
/* */
/* The program uses creat to create and open the file, and */
/* fdopen to associate the file descriptor with a file pointer. */
/* After using the fdopen function, the file must be referenced */
/* using the Standard I/O functions. */
#include <unixio.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define ERROR 0
#define ERROR1 -1
#define BUFFSIZE 132
main()
{
char buffer[BUFFSIZE];
int fildes;
FILE *fp;
if ((fildes = creat("data.dat", 0, "rat=cr",
"rfm=var")) == ERROR1) {
perror("DATA.DAT: creat() failed\n");
exit(EXIT_FAILURE);
}
if ((fp = fdopen(fildes, "w")) == NULL) {
perror("DATA.DAT: fdopen() failed\n");
exit(EXIT_FAILURE);
}
while (fgets(buffer, BUFFSIZE, stdin) != NULL)
if (fwrite(buffer, strlen(buffer), 1, fp) == ERROR) {
perror("DATA.DAT: fwrite() failed\n");
exit(EXIT_FAILURE);
}
if (fclose(fp) == EOF) {
perror("DATA.DAT: fclose() failed\n");
exit(EXIT_FAILURE);
}
}
|
| 目次 | 索引 |
|
||||||||