-
Notifications
You must be signed in to change notification settings - Fork 175
created fgetpos and fsetpos #1583
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
4
commits into
cpprefjp:master
Choose a base branch
from
rotarymars:cstdio
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.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
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,72 @@ | ||
| # fgetpos | ||
| * cstdio[meta header] | ||
| * std[meta namespace] | ||
| * function[meta id-type] | ||
|
|
||
| ```cpp | ||
| namespace std { | ||
| int fgetpos(FILE* stream, fpos_t* pos); | ||
| } | ||
| ``` | ||
|
|
||
| ## 概要 | ||
| ファイルの現在位置を取得する。 | ||
|
|
||
| ## 戻り値 | ||
| 正常に実行されれば0を返す。 | ||
|
|
||
| 失敗した場合は、0以外を返し、エラーの内容は[`errno`](/reference/cerrno/errno.md)から参照することができる。 | ||
|
|
||
| ## この機能が必要になった背景・経緯 | ||
| かつては[`ftell`](/reference/cstdio/ftell.md.nolink)で位置を取得していたが、`ftell`は位置を`long int`で返すため、非常に大きなファイルやマルチバイト文字を含む特殊なファイルでは正確に表現できないことがあった。 | ||
|
|
||
| そこで、どのようなファイルでも位置を正確に表現できる`fpos_t`型が導入されたことに伴い、この関数が登場した。 | ||
|
|
||
| ## 例 | ||
| ```cpp example | ||
| #include <iostream> | ||
| #include <cstdio> | ||
|
|
||
| int main() { | ||
| std::FILE *file = std::fopen("example.txt", "w"); | ||
| if (file == nullptr) { | ||
| std::perror("Failed to open file"); | ||
| return 1; | ||
| } | ||
| std::fpos_t current_pos; | ||
| std::fgetpos(file, ¤t_pos); | ||
| std::fputs("Hello, World!\n", file); | ||
| std::fsetpos(file, ¤t_pos); | ||
| std::fputs("h", file); | ||
| std::fclose(file); | ||
| file = std::fopen("example.txt", "r"); | ||
| if (file == nullptr) { | ||
rotarymars marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| std::perror("Failed to open file"); | ||
| return 1; | ||
| } | ||
| int ch; | ||
| while ((ch = std::fgetc(file)) != EOF) { | ||
| std::putchar(ch); | ||
| } | ||
| std::fclose(file); | ||
| return 0; | ||
| } | ||
| ``` | ||
| * std::fgetpos[color ff0000] | ||
| * std::fsetpos[link /reference/cstdio/fsetpos.md] | ||
| * std::fopen[link /reference/cstdio/fopen.md] | ||
| * std::fputs[link /reference/cstdio/fputs.md] | ||
| * std::fclose[link /reference/cstdio/fclose.md] | ||
| * std::perror[link /reference/cstdio/perror.md.nolink] | ||
rotarymars marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| * std::fgetc[link /reference/cstdio/fgetc.md] | ||
| * std::putchar[link /reference/cstdio/putchar.md] | ||
|
|
||
| ## 出力 | ||
| ``` | ||
| hello, World! | ||
| ``` | ||
|
|
||
| ## 処理系 | ||
| - [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
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,72 @@ | ||
| # fsetpos | ||
| * cstdio[meta header] | ||
| * std[meta namespace] | ||
| * function[meta id-type] | ||
|
|
||
| ```cpp | ||
| namespace std { | ||
| int fsetpos(FILE* stream, const fpos_t* pos); | ||
| } | ||
| ``` | ||
|
|
||
| ## 概要 | ||
| ファイルの現在位置を設定する。 | ||
|
|
||
| ## 戻り値 | ||
| 正常に実行されれば0を返す。 | ||
|
|
||
| 失敗した場合は、0以外を返し、エラーの内容は[`errno`](/reference/cerrno/errno.md)から参照することができる。 | ||
|
|
||
| ## この機能が必要になった背景・経緯 | ||
| かつては[`fseek`](/reference/cstdio/fseek.md.nolink)で位置を指定していたが、`fseek`は位置を`long int`で指定するため、非常に大きなファイルやマルチバイト文字を含む特殊なファイルでは正確にファイル位置を指定できないことがあった。 | ||
|
|
||
| そこで、どのようなファイルでも位置を正確に表現できる`fpos_t`型が導入されたことに伴い、この関数が登場した。 | ||
|
|
||
| ## 例 | ||
| ```cpp example | ||
| #include <iostream> | ||
| #include <cstdio> | ||
|
|
||
| int main() { | ||
| std::FILE *file = std::fopen("example.txt", "w"); | ||
| if (file == nullptr) { | ||
rotarymars marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| std::perror("Failed to open file"); | ||
| return 1; | ||
| } | ||
| std::fpos_t current_pos; | ||
| std::fgetpos(file, ¤t_pos); | ||
| std::fputs("Hello, World!\n", file); | ||
| std::fsetpos(file, ¤t_pos); | ||
| std::fputs("h", file); | ||
| std::fclose(file); | ||
| file = std::fopen("example.txt", "r"); | ||
| if (file == nullptr) { | ||
rotarymars marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| std::perror("Failed to open file"); | ||
| return 1; | ||
| } | ||
| int ch; | ||
| while ((ch = std::fgetc(file)) != EOF) { | ||
| std::putchar(ch); | ||
| } | ||
| std::fclose(file); | ||
| return 0; | ||
| } | ||
| ``` | ||
| * std::fsetpos[color ff0000] | ||
| * std::fgetpos[link /reference/cstdio/fgetpos.md] | ||
| * std::fopen[link /reference/cstdio/fopen.md] | ||
| * std::fputs[link /reference/cstdio/fputs.md] | ||
| * std::fclose[link /reference/cstdio/fclose.md] | ||
| * std::perror[link /reference/cstdio/perror.md.nolink] | ||
rotarymars marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| * std::fgetc[link /reference/cstdio/fgetc.md] | ||
| * std::putchar[link /reference/cstdio/putchar.md] | ||
|
|
||
| ## 出力 | ||
| ``` | ||
| hello, World! | ||
| ``` | ||
|
|
||
| ## 処理系 | ||
| - [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
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.
Uh oh!
There was an error while loading. Please reload this page.