標準/拡張機能 | C/C++ | 依存項目 |
---|---|---|
ISO C |
両方 |
#include <stdio.h>
int fgetc(FILE *stream);
#define _OPEN_SYS_UNLOCKED_EXT 1
#include <stdio.h>
int fgetc_unlocked(FILE *stream);
単一バイトの符号なし文字を、現在位置の stream で指定される入力ストリームから読み取り、その次の文字を指すように関連するファイル・ポインターを増加させます。
fgetc() 関数は、type=record または type=blocked を使用してオープンしたファイルで サポートされません。
fgetc() には、書き込みの直後に読み取り、または読み取りの直後に 書き込みを行うための読み取り操作と同じ制約事項があります。書き込みと後続の読み取りの間には、介入フラッシュまたは位置変更が 必要です。読み取りと後続の書き込みの間でも、EOF に達していない限り、介入フラッシュまたは位置変更が必要です。
fgetc_unlocked() は、スレッド・セーフでないことを除いて、機能的に fgetc() と 等価です。この関数をマルチスレッドのアプリケーション内で安全に使用できるのは、 flockfile() 関数または ftrylockfile() 関数のどちらかへの呼び出しが成功した後のように、 呼び出しスレッドが (FILE*) オブジェクトを所有している間に、この関数を呼び出す場合だけです。
正常に実行された場合、fgetc() は、整数として読み取った文字を戻します。
⁄* CELEBF16
This example gathers a line of input from a stream.
It tests to see if the file can be opened.
If the file cannot be opened &perror. is called.
*⁄
#include <stdio.h>
#define MAX_LEN 80
int main(void)
{
FILE *stream;
char buffer[MAX_LEN + 1];
int i, ch;
if ((stream = fopen("myfile.dat","r")) != NULL) {
for (i = 0; (i < (sizeof(buffer)-1) &&
((ch = fgetc(stream)) != EOF) && (ch != '¥n')); i++)
printf("character is %d¥n",ch);
buffer[i] = ch;
buffer[i] = '¥0';
if (fclose(stream))
perror("fclose error");
}
else
perror("fopen error");
}