fgetpos
提供: cppreference.com
<tbody>
</tbody>
<tbody class="t-dcl-rev ">
</tbody><tbody>
</tbody>
| ヘッダ <stdio.h> で定義
|
||
int fgetpos( FILE *stream, fpos_t *pos ); |
(C99未満) | |
int fgetpos( FILE *restrict stream, fpos_t *restrict pos ); |
(C99以上) | |
ファイルストリーム stream のファイル位置指示子および現在のパース状態 (もしあれば) を取得し、それらを pos の指すオブジェクトに格納します。 格納された値は fsetpos への入力としてのみ意味を持ちます。
引数
| stream | - | 調べるファイルストリーム |
| pos | - | ファイル位置指示子を格納する fpos_t オブジェクトを指すポインタ |
戻り値
成功した場合は 0、そうでなければ非ゼロの値。
例
Run this code
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
int main(void)
{
// prepare a file holding 4 values of type double
enum {SIZE = 4};
FILE* fp = fopen("test.bin", "wb");
assert(fp);
int rc = fwrite((double[SIZE]){1.1, 2.2, 3.3, 4.4}, sizeof(double), SIZE, fp);
assert(rc == SIZE);
fclose(fp);
// demo using fsetpos to return to the beginning of a file
fp = fopen("test.bin", "rb");
fpos_t pos;
fgetpos(fp, &pos); // store start of file in pos
double d;
rc = fread(&d, sizeof d, 1, fp); // read the first double
assert(rc == 1);
printf("First value in the file: %.1f\n", d);
fsetpos(fp,&pos); // move file position back to the start of the file
rc = fread(&d, sizeof d, 1, fp); // read the first double again
assert(rc == 1);
printf("First value in the file again: %.1f\n", d);
fclose(fp);
// demo error handling
rc = fsetpos(stdin, &pos);
if(rc) perror("could not fsetpos stdin");
}
出力:
First value in the file: 1.1
First value in the file again: 1.1
could not fsetpos stdin: Illegal seek
参考文献
- C11 standard (ISO/IEC 9899:2011):
- 7.21.9.1 The fgetpos function (p: 336)