OSDN Git Service

info/GNU_which: Add some explanations.
[linuxjm/jm.git] / manual / LDP_man-pages / draft / man3 / fread.3
1 .\" Copyright (c) 1990, 1991 The Regents of the University of California.
2 .\" and Copyright (c) 2020 Arkadiusz Drabczyk <arkadiusz@drabczyk.org>
3 .\" All rights reserved.
4 .\"
5 .\" This code is derived from software contributed to Berkeley by
6 .\" Chris Torek and the American National Standards Committee X3,
7 .\" on Information Processing Systems.
8 .\"
9 .\" %%%LICENSE_START(BSD_4_CLAUSE_UCB)
10 .\" Redistribution and use in source and binary forms, with or without
11 .\" modification, are permitted provided that the following conditions
12 .\" are met:
13 .\" 1. Redistributions of source code must retain the above copyright
14 .\"    notice, this list of conditions and the following disclaimer.
15 .\" 2. Redistributions in binary form must reproduce the above copyright
16 .\"    notice, this list of conditions and the following disclaimer in the
17 .\"    documentation and/or other materials provided with the distribution.
18 .\" 3. All advertising materials mentioning features or use of this software
19 .\"    must display the following acknowledgement:
20 .\"     This product includes software developed by the University of
21 .\"     California, Berkeley and its contributors.
22 .\" 4. Neither the name of the University nor the names of its contributors
23 .\"    may be used to endorse or promote products derived from this software
24 .\"    without specific prior written permission.
25 .\"
26 .\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
27 .\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28 .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29 .\" ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
30 .\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31 .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32 .\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33 .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34 .\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35 .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 .\" SUCH DAMAGE.
37 .\" %%%LICENSE_END
38 .\"
39 .\"     @(#)fread.3     6.6 (Berkeley) 6/29/91
40 .\"
41 .\" Converted for Linux, Mon Nov 29 15:37:33 1993, faith@cs.unc.edu
42 .\" Sun Feb 19 21:26:54 1995 by faith, return values
43 .\" Modified Thu Apr 20 20:43:53 1995 by Jim Van Zandt <jrv@vanzandt.mv.com>
44 .\" Modified Fri May 17 10:21:51 1996 by Martin Schulze <joey@infodrom.north.de>
45 .\"
46 .\"*******************************************************************
47 .\"
48 .\" This file was generated with po4a. Translate the source file.
49 .\"
50 .\"*******************************************************************
51 .\"
52 .\" Japanese Version Copyright (c) 1997 YOSHINO Takashi
53 .\"       all rights reserved.
54 .\" Translated Tue Jan 21 21:57:33 JST 1997
55 .\"       by YOSHINO Takashi <yoshino@civil.jcn.nihon-u.ac.jp>
56 .\" Updated & Modified Mon Mar 1 1999
57 .\"       by NAKANO Takeo <nakano@apm.seikei.ac.jp>
58 .\" Updated Sat Nov  3 JST 2001 by Kentaro Shirakata <argrath@ub32.org>
59 .\" Updated 2012-04-30, Akihiro MOTOKI <amotoki@gmail.com>
60 .\"
61 .TH FREAD 3 2020\-08\-13 GNU "Linux Programmer's Manual"
62 .SH 名前
63 fread, fwrite \- バイナリストリームの入出力
64 .SH 書式
65 .nf
66 \fB#include <stdio.h>\fP
67 .PP
68 \fBsize_t fread(void *\fP\fIptr\fP\fB, size_t \fP\fIsize\fP\fB, size_t \fP\fInmemb\fP\fB, FILE *\fP\fIstream\fP\fB);\fP
69 .PP
70 \fBsize_t fwrite(const void *\fP\fIptr\fP\fB, size_t \fP\fIsize\fP\fB, size_t \fP\fInmemb\fP\fB,\fP
71 \fB              FILE *\fP\fIstream\fP\fB);\fP
72 .fi
73 .SH 説明
74 \fBfread\fP()  関数は \fIstream\fP ポインターで指定されたストリームから \fInmemb\fP 個のデータを読み込み、 \fIptr\fP
75 で与えられた場所に格納する。 個々のデータは \fIsize\fP バイトの長さを持つ。
76 .PP
77 \fBfwrite\fP()  関数は \fIptr\fP で指定された場所から得た \fInmemb\fP 個のデータを、 \fIstream\fP
78 ポインターで指定されたストリームに書き込む。 個々のデータは \fIsize\fP バイトの長さを持つ。
79 .PP
80 これらの処理を停止せずに行いたいときは、 \fBunlocked_stdio\fP(3)  を参照のこと。
81 .SH 返り値
82 成功すると、 \fBfread\fP() と \fBfwrite\fP() は読み書きを行った要素の個数を返す。
83 \fIsize\fP が 1 の場合は、この数字は転送されたバイト数と等しい。
84 エラーが生じた場合や、ファイルの末尾 (end\-of\-file) に達した場合、
85 返り値は指定した個数よりも小さい値 (または 0) となる。
86 .PP
87 The file position indicator for the stream is advanced by the number of
88 bytes successfully read or written.
89 .PP
90 \fBfread\fP()  は end\-of\-file とエラーを区別しないので、 どちらが生じたかを判断するためには、 呼び出し側で \fBfeof\fP(3)
91 と \fBferror\fP(3)  とを使用しなければならない。
92 .SH 属性
93 この節で使用されている用語の説明については、 \fBattributes\fP(7) を参照。
94 .TS
95 allbox;
96 lbw17 lb lb
97 l l l.
98 インターフェース        属性  値
99 T{
100 \fBfread\fP(),
101 \fBfwrite\fP()
102 T}      Thread safety   MT\-Safe
103 .TE
104 .SH 準拠
105 POSIX.1\-2001, POSIX.1\-2008, C89.
106 .SH 例
107 The program below demonstrates the use of \fBfread\fP()  by parsing /bin/sh ELF
108 executable in binary mode and printing its magic and class:
109 .PP
110 .in +4n
111 .EX
112 $ \fB./a.out\fP
113 ELF magic: 0x7f454c46
114 Class: 0x02
115 .EE
116 .in
117 .SS プログラムのソース
118 \&
119 .EX
120 #include <stdio.h>
121 #include <stdlib.h>
122
123 #define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]))
124
125 int
126 main(void)
127 {
128     FILE *fp = fopen("/bin/sh", "rb");
129     if (!fp) {
130         perror("fopen");
131         return EXIT_FAILURE;
132     }
133
134     unsigned char buffer[4];
135
136     size_t ret = fread(buffer, ARRAY_SIZE(buffer), sizeof(*buffer), fp);
137     if (ret != sizeof(*buffer)) {
138         fprintf(stderr, "fread() failed: %zu\en", ret);
139         exit(EXIT_FAILURE);
140     }
141
142     printf("ELF magic: %#04x%02x%02x%02x\en", buffer[0], buffer[1],
143            buffer[2], buffer[3]);
144
145     ret = fread(buffer, 1, 1, fp);
146     if (ret != 1) {
147         fprintf(stderr, "fread() failed: %zu\en", ret);
148         exit(EXIT_FAILURE);
149     }
150
151     printf("Class: %#04x\en", buffer[0]);
152
153     fclose(fp);
154
155     exit(EXIT_SUCCESS);
156 }
157 .EE
158 .SH 関連項目
159 \fBread\fP(2), \fBwrite\fP(2), \fBfeof\fP(3), \fBferror\fP(3), \fBunlocked_stdio\fP(3)
160 .SH この文書について
161 この man ページは Linux \fIman\-pages\fP プロジェクトのリリース 5.10 の一部である。プロジェクトの説明とバグ報告に関する情報は
162 \%https://www.kernel.org/doc/man\-pages/ に書かれている。