#include <stdio.h> int getc(FILE *stream) stream : FILE 構造体へのポインタ
#include <stdio.h>
int main()
{
int i1;
char str[81];
FILE *stream;
fpos_t pos;
/*
テストデータをファイルへ出力
*/
stream = fopen("data", "w");
fputs("This is a test", stream);
fclose(stream);
/*
ファイルのオープン
*/
stream = fopen("data", "r");
fgetpos(stream, &pos); /* ファイル位置の取得 */
/*
fgets関数による入力
*/
fgets(str, 50, stream);
printf("%s\n", str);
/*
getc関数による入力
*/
i1 = 0;
fsetpos(stream, &pos); /* ファイル位置を戻す */
while ((str[i1] = (char)getc(stream)) != EOF)
i1++;
str[i1] = '\0';
printf("%s\n", str);
return 0;
}
This is a test This is a test
#include <stdio.h>
int main()
{
int k1 = 0x61626364; /* "abcd" */
int k2;
char c1 = 'a'; /* 0x61 */
char c2 = 'b'; /* 0x62 */
char c3, c4;
FILE *stream;
/*
テストデータをファイルへ出力
*/
printf("k1 0x%8x c1 %c c2 %c\n", k1, c1, c2);
stream = fopen("data", "w");
putc(c1, stream);
putc(c2, stream);
putw(k1, stream);
fclose(stream);
/*
データの読込
*/
stream = fopen("data", "r");
k2 = getw(stream);
c3 = (char)getc(stream);
c4 = (char)getc(stream);
printf("k2 0x%8x c3 %c c4 %c\n", k2, c3, c4);
fclose(stream);
return 0;
}
k1 0x61626364 c1 a c2 b k2 0x63646261 c3 b c4 a
| 菅沼ホーム | 本文目次 | 演習問題解答例 | 付録目次 | 索引 |