-
Notifications
You must be signed in to change notification settings - Fork 170
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
rotarymars
wants to merge
16
commits into
cpprefjp:master
Choose a base branch
from
rotarymars:master
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+147
−2
Open
fpos_t and fread #1468
Changes from all commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
f2c2262
new page `fpos_t.md`
rotarymars 3a5b8ac
new page `fread.md`
rotarymars 05c1017
removed brackets for function declaration
rotarymars 57c8735
Merge branch 'cpprefjp:master' into master
rotarymars 9702775
fixed function declaration
rotarymars dd34ab2
fixed explanation in fread.md
rotarymars d074e9f
Update reference/cstdio/fread.md
rotarymars 3f22e22
fixed declaration of fpos_t
rotarymars 0e9f6d7
Merge branch 'master' of https://github.com/rotarymars/cpprefjp-site
rotarymars b645fd6
fixed declaration of fpos_t
rotarymars 3e84961
Merge branch 'master' of https://github.com/rotarymars/cpprefjp-site
rotarymars 1ebe252
Merge branch 'cpprefjp:master' into master
rotarymars 5050bbe
Update reference/cstdio/fpos_t.md
rotarymars 3aec3d6
Update reference/cstdio/fpos_t.md
rotarymars f8f78d5
Merge branch 'cpprefjp:master' into master
rotarymars 4124913
fixed declaration of `fpos_t`
rotarymars File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
``` | ||
|
||
## 概要 | ||
ファイルの位置を保持するための型。 | ||
|
||
[`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): ?? |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.