#include <stdio.h> char *fgets(char *string, int n, FILE *stream) string : 読み込んだデータの格納場所 n : 読み込む文字数 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);
/*
fgetc関数による入力
*/
i1 = 0;
fsetpos(stream, &pos); /* ファイル位置を戻す */
while ((str[i1] = (char)fgetc(stream)) != EOF)
i1++;
str[i1] = '\0';
printf("%s\n", str);
return 0;
}
This is a test This is a test
| 菅沼ホーム | 本文目次 | 演習問題解答例 | 付録目次 | 索引 |