1+ <!doctype html>
2+ < html class ="cpprefjp " lang ="ja " itemscope ="" itemtype ="http://schema.org/WebPage ">
3+ < head >
4+
5+
6+ <!-- Google tag (gtag.js) -->
7+ < script async src ="https://www.googletagmanager.com/gtag/js?id=G-NXNBNVBTJS "> </ script >
8+ < script >
9+ window . dataLayer = window . dataLayer || [ ] ;
10+ function gtag ( ) { dataLayer . push ( arguments ) ; }
11+ gtag ( 'js' , new Date ( ) ) ;
12+
13+ gtag ( 'config' , 'G-NXNBNVBTJS' ) ;
14+ </ script >
15+
16+
17+ < meta charset ="UTF-8 ">
18+
19+ < title > fwrite - cpprefjp C++日本語リファレンス</ title >
20+
21+ < meta name ="viewport " content ="width=device-width,initial-scale=1 ">
22+ < meta name ="keywords " content ="
23+ C++,標準ライブラリ,リファレンス,ドキュメント,STL,std,cstdio,std,function
24+ ">
25+ < meta name ="title " content ="fwrite - cpprefjp C++日本語リファレンス " />
26+ < meta itemprop ="name " content ="fwrite - cpprefjp C++日本語リファレンス " />
27+ < meta property ="og:title " content ="fwrite - cpprefjp C++日本語リファレンス " />
28+ < meta property ="og:url " content ="https://cpprefjp.github.io/reference/cstdio/fwrite.html " />
29+ < meta property ="og:site_name " content ="cpprefjp - C++日本語リファレンス " />
30+ < meta property ="og:type " content ="article " />
31+ < meta property ="og:description " content ="ストリームに`count`個の`size`バイトの要素を書き込む。 " />
32+ < meta name ="twitter:card " content ="summary " />
33+ < meta name ="twitter:title " content ="fwrite - cpprefjp C++日本語リファレンス " />
34+ < meta name ="twitter:url " content ="https://cpprefjp.github.io/reference/cstdio/fwrite.html " />
35+ < meta name ="twitter:description " content ="ストリームに`count`個の`size`バイトの要素を書き込む。 " />
36+
37+
38+ < link rel ="alternate " type ="application/atom+xml " title ="Atom " href ="https://cpprefjp.github.io/rss.xml " />
39+
40+
41+ < link rel ="apple-touch-icon " sizes ="180x180 " href ="../../static/favicons/apple-touch-icon.png?cachebust=4e72de3088f20b2f0177531c42a1fa9d0d822e93 ">
42+ < link rel ="icon " type ="image/png " sizes ="32x32 " href ="../../static/favicons/favicon-32x32.png?cachebust=4e72de3088f20b2f0177531c42a1fa9d0d822e93 ">
43+ < link rel ="icon " type ="image/png " sizes ="16x16 " href ="../../static/favicons/favicon-16x16.png?cachebust=4e72de3088f20b2f0177531c42a1fa9d0d822e93 ">
44+ < link rel ="manifest " href ="../../manifest.json?cachebust=4e72de3088f20b2f0177531c42a1fa9d0d822e93 ">
45+ < meta name ="theme-color " content ="#f5f8fc ">
46+
47+ < link rel ="stylesheet " href ="../../static/pygments/default.css?cachebust=4e72de3088f20b2f0177531c42a1fa9d0d822e93 ">
48+ <!-- <link rel="stylesheet" href="../../static/css/root.css"> -->
49+
50+
51+
52+
53+
54+ < link href ="../../static/kunai/css/kunai-stage-0.css?cachebust=4e72de3088f20b2f0177531c42a1fa9d0d822e93 " rel ="stylesheet ">
55+ < link href ="../../static/kunai/css/kunai-stage-1.css?cachebust=4e72de3088f20b2f0177531c42a1fa9d0d822e93 " rel ="stylesheet ">
56+ < link href ="../../static/kunai/css/kunai-stage-2.css?cachebust=4e72de3088f20b2f0177531c42a1fa9d0d822e93 " rel ="stylesheet ">
57+ < link href ="../../static/kunai/css/kunai-stage-3.css?cachebust=4e72de3088f20b2f0177531c42a1fa9d0d822e93 " rel ="stylesheet ">
58+
59+ < script type ="text/javascript " src ="../../static/kunai/js/kunai-vendor.js?cachebust=4e72de3088f20b2f0177531c42a1fa9d0d822e93 "> </ script >
60+ < script type ="text/javascript " src ="../../static/kunai/js/kunai.js?cachebust=4e72de3088f20b2f0177531c42a1fa9d0d822e93 "> </ script >
61+
62+ < script type ="text/javascript ">
63+ document . addEventListener ( 'DOMContentLoaded' , function ( ) {
64+ var kn = new Kunai ;
65+ kn . cpprefjp ( ) ;
66+ } ) ;
67+ </ script >
68+
69+ </ head >
70+ < body >
71+ < header data-kunai-mdinfo ="{"meta": {"header": ["cstdio"], "namespace": ["std"], "id-type": ["function"]}, "sources": [{"id": "e8da0f4d5689dd5aef360271f856d5c63dbbc1cb", "source": "#include <cstdio>\n\nint main() {\n std::FILE *file = std::fopen(\"output.txt\", \"w\");\n if (!file) {\n std::perror(\"\u30d5\u30a1\u30a4\u30eb\u3092\u958b\u3051\u307e\u305b\u3093\u3067\u3057\u305f\");\n return 1;\n }\n\n const char data[] = \"Hello, World!\\n\";\n /*\n \u53b3\u5bc6\u306b\u306f\u3001sizeof(char)\u306f1\u30d0\u30a4\u30c8\u3067\u3042\u308b\u3053\u3068\u304c\u4fdd\u8a3c\u3055\u308c\u3066\u3044\u308b\u305f\u3081\u3001\n sizeof(data) - 1\u306f\u3001\u6587\u5b57\u5217\u306e\u9577\u3055\uff08\u30cc\u30eb\u7d42\u7aef\u6587\u5b57\u3092\u9664\u304f\uff09\u3068\u7b49\u3057\u304f\u306a\u308b\u3002\n */\n std::size_t count = std::fwrite(data, sizeof(char), sizeof(data) - 1, file);\n std::printf(\"\u66f8\u304d\u8fbc\u3093\u3060\u30c7\u30fc\u30bf\u306e\u9577\u3055: %zu\\n\", count);\n\n std::fclose(file);\n return 0;\n}\n"}], "page_id": ["reference", "cstdio", "fwrite"]} ">
72+ < nav class ="navbar navbar-default " role ="navigation ">
73+ < div class ="container-fluid ">
74+ < div class ="navbar-header ">
75+ < button type ="button " class ="navbar-toggle collapsed " data-toggle ="collapse " data-target ="#navbar-collapse ">
76+ < span class ="sr-only "> Toggle navigation</ span >
77+ < span class ="icon-bar "> </ span >
78+ < span class ="icon-bar "> </ span >
79+ < span class ="icon-bar "> </ span >
80+ </ button >
81+ < a class ="navbar-brand " href ="../../index.html ">
82+ < div class ="title-wrapper clearfix ">
83+ < div class ="title "> cpprefjp - C++日本語リファレンス</ div >
84+ </ div >
85+ </ a >
86+ </ div >
87+
88+ < div class ="collapse navbar-collapse " id ="navbar-collapse ">
89+ < ul class ="nav navbar-nav navbar-right ">
90+ < li >
91+ < div class ="google-search ">
92+
93+
94+ < script >
95+ ( function ( ) {
96+ var cx = '013316413321391058734:ji_u66hl7hq' ;
97+ var gcse = document . createElement ( 'script' ) ;
98+ gcse . type = 'text/javascript' ;
99+ gcse . async = true ;
100+ gcse . src = ( document . location . protocol == 'https:' ? 'https:' : 'http:' ) +
101+ '//www.google.com/cse/cse.js?cx=' + cx ;
102+ var s = document . getElementsByTagName ( 'script' ) [ 0 ] ;
103+ s . parentNode . insertBefore ( gcse , s ) ;
104+ } ) ( ) ;
105+ </ script >
106+ < div class ="gcse-search "> </ div >
107+
108+
109+ </ div >
110+ </ li >
111+ < li >
112+ < a href ="https://github.com/cpprefjp/site "> GitHub Project</ a >
113+ </ li >
114+ </ ul >
115+ </ div >
116+ </ div >
117+ </ nav >
118+ </ header >
119+
120+ < main id ="main " role ="main ">
121+ < div class ="container-fluid ">
122+ < div class ="row ">
123+ < div class ="col-sm-9 col-sm-push-3 " itemscope itemtype ="http://schema.org/Article ">
124+ < div class ="row ">
125+ < div class ="col-sm-12 google-search-result ">
126+ < gcse:searchresults > </ gcse:searchresults >
127+ </ div >
128+ </ div >
129+ < div class ="row ">
130+ < div class ="col-sm-12 content-header ">
131+
132+ < ol class ="breadcrumb ">
133+
134+ < li itemscope itemtype ="http://www.schema.org/SiteNavigationElement ">
135+ < span >
136+
137+ < a href ="../../index.html " itemprop ="url ">
138+
139+ < i class ="fa fa-fw fa-home "> </ i >
140+
141+ </ a >
142+
143+ </ span >
144+ </ li >
145+
146+ < li itemscope itemtype ="http://www.schema.org/SiteNavigationElement ">
147+ < span >
148+
149+ < a href ="../../reference.html " itemprop ="url ">
150+
151+ < span itemprop ="name "> リファレンス</ span >
152+
153+ </ a >
154+
155+ </ span >
156+ </ li >
157+
158+ < li itemscope itemtype ="http://www.schema.org/SiteNavigationElement ">
159+ < span >
160+
161+ < a href ="../../reference/cstdio.html " itemprop ="url ">
162+
163+ < span itemprop ="name "> cstdio</ span >
164+
165+ </ a >
166+
167+ </ span >
168+ </ li >
169+
170+ < li class ="active " itemscope itemtype ="http://www.schema.org/SiteNavigationElement ">
171+ < span >
172+
173+
174+ < span itemprop ="name "> fwrite</ span >
175+
176+
177+ </ span >
178+ </ li >
179+
180+ </ ol >
181+ < div class ="crsearch "> </ div >
182+
183+ </ div >
184+ </ div >
185+ < div class ="row ">
186+ < div class ="col-sm-12 edit-button ">
187+
188+
189+ < p class ="text-right "> < small >
190+ 最終更新日時(UTC):
191+ < span itemprop ="datePublished " content ="2025-09-05T12:29:52 ">
192+ 2025年09月05日 12時29分52秒
193+ </ span >
194+ < br />
195+ < span itemprop ="author " itemscope itemtype ="http://schema.org/Person ">
196+ < span itemprop ="name "> rotarymars</ span >
197+ </ span >
198+ が更新
199+ </ small > </ p >
200+
201+
202+ < p class ="text-right ">
203+ < a class ="history " target ="_blank " href ="https://github.com/cpprefjp/site/commits/master/reference/cstdio/fwrite.md ">
204+ < span class ="fa fa-fw fa-clock-o fa-flip-horizontal "> </ span > 履歴
205+ </ a >
206+ < a class ="edit " target ="_blank " href ="https://github.com/cpprefjp/site/edit/master/reference/cstdio/fwrite.md ">
207+ < span class ="fa fa-fw fa-pencil "> </ span > 編集
208+ </ a >
209+ </ p >
210+ </ div >
211+ </ div >
212+ < div class ="row ">
213+ < div class ="col-sm-12 content-body ">
214+
215+ < div class ="identifier-type "> function</ div > < div class ="header "> <cstdio></ div > < h1 itemprop ="name "> < span class ="namespace " title ="namespace std "> std::</ span > < span class ="token "> fwrite</ span > </ h1 >
216+ < div itemprop ="articleBody "> < p > < div class ="codehilite "> < pre > < span > </ span > < code > < span class ="k "> namespace</ span > < span class ="n "> std</ span > < span class ="p "> {</ span >
217+ < span class ="kt "> size_t</ span > < span class ="n "> fwrite</ span > < span class ="p "> (</ span > < span class ="k "> const</ span > < span class ="kt "> void</ span > < span class ="o "> *</ span > < span class ="n "> buffer</ span > < span class ="p "> ,</ span > < span class ="kt "> size_t</ span > < span class ="n "> size</ span > < span class ="p "> ,</ span > < span class ="kt "> size_t</ span > < span class ="n "> count</ span > < span class ="p "> ,</ span > < span class ="kt "> FILE</ span > < span class ="o "> *</ span > < span class ="n "> stream</ span > < span class ="p "> );</ span >
218+ < span class ="p "> }</ span >
219+ </ code > </ pre > </ div >
220+ </ p >
221+ < h2 > 概要</ h2 >
222+ < p > ストリームに< code > count</ code > 個の< code > size</ code > バイトの要素を書き込む。</ p >
223+ < p > ストリーム内の位置は、書き込まれたバイトだけ進む。</ p >
224+ < p > 書き込み時にエラーが発生した場合、ストリーム内の位置は不定である。</ p >
225+ < p > また、書き込みには内部的に< code > < a href ="fputc.html "> fputc</ a > </ code > を使用する。</ p >
226+ < h2 > 要件</ h2 >
227+ < ul >
228+ < li > < code > buffer</ code > は有効なポインタであること。</ li >
229+ < li > < code > stream</ code > は有効なファイルストリームであること。</ li >
230+ < li > < code > buffer</ code > が指すメモリ領域と< code > stream</ code > が指すファイルストリームの内部バッファが重複していないこと。</ li >
231+ </ ul >
232+ < h2 > < a class ="cpprefjp-defined-word " data-desc ="関数呼び出し式の評価結果となるオブジェクト・値 "> 戻り値</ a > </ h2 >
233+ < p > 正常に書き込むことのできた要素の数を返す。</ p >
234+ < h2 > 効果</ h2 >
235+ < p > < code > buffer</ code > が指すメモリから< code > count</ code > 個の< code > size</ code > バイトの要素を< code > stream</ code > に書き込む。</ p >
236+ < p > 書き込みエラーが生じなければ、書き込むデータの大きさは< code > count * size</ code > バイトである。</ p >
237+ < h2 > 例</ h2 >
238+ < p > < div class ="yata " id ="e8da0f4d5689dd5aef360271f856d5c63dbbc1cb "> < div class ="codehilite "> < pre > < span > </ span > < code > < span class ="cp "> #include</ span > < span class ="cpf "> < a href ="../cstdio.html "> <cstdio></ a > </ span > < span class ="cp "> </ span >
239+
240+ < span class ="kt "> int</ span > < span class ="nf "> main</ span > < span class ="p "> ()</ span > < span class ="p "> {</ span >
241+ < span class ="n "> std</ span > < span class ="o "> ::</ span > < span class ="kt "> FILE</ span > < span class ="o "> *</ span > < span class ="n "> file</ span > < span class ="o "> =</ span > < span class ="n "> < a href ="fopen.html "> std::fopen</ a > </ span > < span class ="p "> (</ span > < span class ="s "> "output.txt"</ span > < span class ="p "> ,</ span > < span class ="s "> "w"</ span > < span class ="p "> );</ span >
242+ < span class ="k "> if</ span > < span class ="p "> (</ span > < span class ="o "> !</ span > < span class ="n "> file</ span > < span class ="p "> )</ span > < span class ="p "> {</ span >
243+ < span class ="n "> < span href ="https://cpprefjp.github.io/reference/cstdio/perror.md.nolink "> std::perror</ span > </ span > < span class ="p "> (</ span > < span class ="s "> "ファイルを開けませんでした"</ span > < span class ="p "> );</ span >
244+ < span class ="k "> return</ span > < span class ="mi "> 1</ span > < span class ="p "> ;</ span >
245+ < span class ="p "> }</ span >
246+
247+ < span class ="k "> const</ span > < span class ="kt "> char</ span > < span class ="n "> data</ span > < span class ="p "> []</ span > < span class ="o "> =</ span > < span class ="s "> "Hello, World!</ span > < span class ="se "> \n</ span > < span class ="s "> "</ span > < span class ="p "> ;</ span >
248+ < span class ="cm "> /*</ span >
249+ < span class ="cm "> 厳密には、sizeof(char)は1バイトであることが保証されているため、</ span >
250+ < span class ="cm "> sizeof(data) - 1は、文字列の長さ(ヌル終端文字を除く)と等しくなる。</ span >
251+ < span class ="cm "> */</ span >
252+ < span class ="n "> < a href ="../cstddef/size_t.html "> std::size_t</ a > </ span > < span class ="n "> count</ span > < span class ="o "> =</ span > < span class ="n "> < span style ="color:#ff0000 "> std::fwrite</ span > </ span > < span class ="p "> (</ span > < span class ="n "> data</ span > < span class ="p "> ,</ span > < span class ="k "> sizeof</ span > < span class ="p "> (</ span > < span class ="kt "> char</ span > < span class ="p "> ),</ span > < span class ="k "> sizeof</ span > < span class ="p "> (</ span > < span class ="n "> data</ span > < span class ="p "> )</ span > < span class ="o "> -</ span > < span class ="mi "> 1</ span > < span class ="p "> ,</ span > < span class ="n "> file</ span > < span class ="p "> );</ span >
253+ < span class ="n "> < a href ="printf.html "> std::printf</ a > </ span > < span class ="p "> (</ span > < span class ="s "> "書き込んだデータの長さ: %zu</ span > < span class ="se "> \n</ span > < span class ="s "> "</ span > < span class ="p "> ,</ span > < span class ="n "> count</ span > < span class ="p "> );</ span >
254+
255+ < span class ="n "> < a href ="fclose.html "> std::fclose</ a > </ span > < span class ="p "> (</ span > < span class ="n "> file</ span > < span class ="p "> );</ span >
256+ < span class ="k "> return</ span > < span class ="mi "> 0</ span > < span class ="p "> ;</ span >
257+ < span class ="p "> }</ span >
258+ </ code > </ pre > </ div >
259+ </ div > </ p >
260+ < h3 > 出力例</ h3 >
261+ < p > < pre > < code > 書き込んだデータの長さ: 13
262+ </ code > </ pre > </ p >
263+ < h3 > ファイル出力(output.txt)</ h3 >
264+ < p > < pre > < code > Hello, World!
265+ </ code > </ pre > </ p >
266+ < h2 > 処理系</ h2 >
267+ < ul >
268+ < li > < a href ="../../implementation.html#clang "> Clang</ a > : ??</ li >
269+ < li > < a href ="../../implementation.html#gcc "> GCC</ a > : ??</ li >
270+ < li > < a href ="../../implementation.html#visual_cpp "> Visual C++</ a > : ??</ li >
271+ </ ul > </ div >
272+
273+ </ div >
274+ </ div >
275+ </ div >
276+
277+
278+ < div id ="sidebar " class ="col-sm-3 col-sm-pull-9 ">
279+ </ div >
280+
281+
282+ </ div >
283+ </ div >
284+ </ main >
285+ < footer class ="footer navbar navbar-default ">
286+ < div class ="container-fluid ">
287+ < p > < small >
288+ 本サイトの情報は、
289+ < a href ="https://creativecommons.org/licenses/by/3.0/deed.ja " rel ="nofollow "> クリエイティブ・コモンズ 表示 3.0 非移植 ライセンス(CC BY)</ a >
290+ の下に提供されています。
291+ </ small > </ p >
292+ </ div >
293+ </ footer >
294+ </ body >
295+ </ html >
0 commit comments