Skip to content

fpos_t and fread #1468

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 16 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions reference/cstdio.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
|------|------|----------------|
| [`size_t`](/reference/cstddef/size_t.md) | 符号なし整数型 | |
| [`FILE`](/reference/cstdio/file.md) | ストリームの制御に必要な情報を持つオブジェクト型 | |
| [`fpos_t`](/reference/cstdio/fpos_t.md.nolink) | ファイルの全ての位置にアクセスするための配列以外の完全オブジェクト型 | |
| [`fpos_t`](/reference/cstdio/fpos_t.md) | ファイルの全ての位置にアクセスするための配列以外の完全オブジェクト型 | |


## マクロ
Expand Down Expand Up @@ -54,7 +54,7 @@
| [`fgets`](/reference/cstdio/fgets.md) | ファイルからN文字入力する | |
| [`fputc`](/reference/cstdio/fputc.md) | ファイルに1文字出力する | |
| [`fputs`](/reference/cstdio/fputs.md) | ファイルにN文字出力する | |
| [`fread`](/reference/cstdio/fread.md.nolink) | ファイルからN文字読み込む | |
| [`fread`](/reference/cstdio/fread.md) | ファイルからN文字読み込む | |
| [`fwrite`](/reference/cstdio/fwrite.md.nolink) | ファイルにN文字書き込む | |
| [`fgetpos`](/reference/cstdio/fgetpos.md.nolink) | ファイルの現在位置を取得する | |
| [`fseek`](/reference/cstdio/fseek.md.nolink) | ファイルの現在位置を移動する | |
Expand Down
67 changes: 67 additions & 0 deletions reference/cstdio/fpos_t.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
# fpos_t
* cstdio[meta header]
* type-alias[meta id-type]
* std[meta namespace]

```cpp
namespace std {
using fpos_t = object-type;
}
```
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
```
```
* object-type[italic]


## 概要
ファイルの位置を保持するための型。

[`fgetpos()`](/reference/cstdio/fgetpos.md.nolink)関数や[`fsetpos()`](/reference/cstdio/fsetpos.md.nolink)関数で用いられる。これらの関数は[`fseek()`](/reference/cstdio/fseek.md.nolink)関数や[`ftell()`](/reference/cstdio/ftell.md.nolink)関数と違い、巨大なファイルやマルチバイトファイルに対しても適切に動作するすることを目的に設計された。

## 例
```cpp example
#include <cstdio>

int main() {
std::FILE *file = std::fopen("sample.txt", "r");
if (!file) {
std::perror("ファイルを開けませんでした");
return 1;
}

std::fpos_t pos;
std::fgetpos(file, &pos); // ファイルの位置をposに保存する

int c = std::fgetc(file);
std::printf("1文字目: %c\n", c);

std::fsetpos(file, &pos); // ファイルの読み取り位置をposにする

c = std::fgetc(file);
std::printf("もう一度1文字目: %c\n", c);

std::fclose(file);
return 0;
}

```
* std::fpos_t[color ff0000]
* std::fgetpos[link /reference/cstdio/fgetpos.md.nolink]
* std::fsetpos[link /reference/cstdio/fsetpos.md.nolink]
* std::fopen[link /reference/cstdio/fopen.md]
* std::fclose[link /reference/cstdio/fclose.md]
* std::fgetc[link /reference/cstdio/fgetc.md]
* std::printf[link /reference/cstdio/printf.md]
* std::perror[link /reference/cstdio/perror.md.nolink]

### ファイル内容(sample.txt)
```
Hello, World!
```

### 出力
```
1文字目: H
もう一度1文字目: H
```

## 処理系
- [Clang](/implementation.md#clang): ??
- [GCC](/implementation.md#gcc): ??
- [Visual C++](/implementation.md#visual_cpp): ??
78 changes: 78 additions & 0 deletions reference/cstdio/fread.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
# fread
* cstdio[meta header]
* std[meta namespace]
* function[meta id-type]

```cpp
namespace std {
size_t fread(void *buffer, size_t size, size_t count, FILE *stream);
}
```

## 概要
ファイルから`count`文字の`size`バイトのデータを読み込む。

ファイル内の位置は、読み取られたバイトだけ進む。

## 要件
- `buffer`は有効なポインタであること。
- `stream`は有効なファイルストリームであること。

## 戻り値
正常に読み込むことのできた項目の数を返す。

`size`または`count`のいずれかが0だった場合、必ず`0`を返し、`buffer`は変更されない。

## 効果
`stream`から`count`個の`size`バイトのデータを読み込む。

読み込んだデータは、`buffer`が指すメモリの先頭から順に格納される。

読み取りエラーが生じなければ、読み込むデータの大きさは`count * size`バイトである。

## 例
```cpp example
#include <cstdio>

int main() {
std::FILE *file = std::fopen("sample.txt", "r");
if (!file) {
std::perror("ファイルを開けませんでした");
return 1;
}

char buffer[100];
/*
厳密には、sizeof(char)は1バイトであることが保証されているため、
sizeof(buffer) / sizeof(char)は、bufferの要素数と等しくなる。
*/
std::size_t count = std::fread(buffer, sizeof(char), sizeof(buffer) / sizeof(char), file);
std::printf("読み込んだデータの長さ: %zu\n", count);

std::fclose(file);
return 0;
}
```
* std::fread[color ff0000]
* std::fopen[link /reference/cstdio/fopen.md]
* std::fclose[link /reference/cstdio/fclose.md]
* std::perror[link /reference/cstdio/perror.md.nolink]
* std::printf[link /reference/cstdio/printf.md]

### ファイル内容(sample.txt)
```
Hello, World!
```

### 出力例
```
読み込んだデータの長さ: 14
```

## 処理系
- [Clang](/implementation.md#clang): ??
- [GCC](/implementation.md#gcc): ??
- [Visual C++](/implementation.md#visual_cpp): ??

## 参照
- [sizeof演算子にまつわるアレコレ](https://qiita.com/yohhoy/items/a2ab2900a2bd36c31879)