{"id":3428,"date":"2024-03-13T06:57:29","date_gmt":"2024-03-13T06:57:29","guid":{"rendered":"https:\/\/www.silicloud.com\/blog\/what-is-the-usage-of-the-fwrite-function-in-c\/"},"modified":"2025-07-30T16:10:57","modified_gmt":"2025-07-30T16:10:57","slug":"what-is-the-usage-of-the-fwrite-function-in-c","status":"publish","type":"post","link":"https:\/\/www.silicloud.com\/blog\/what-is-the-usage-of-the-fwrite-function-in-c\/","title":{"rendered":"fwrite in C++: Write Data Blocks to Files"},"content":{"rendered":"<p>In C++, the fwrite function is used to write data blocks to a file. Its declaration is as follows:<\/p>\n<pre class=\"post-pre\"><code><span class=\"hljs-function\"><span class=\"hljs-type\">size_t<\/span> <span class=\"hljs-title\">fwrite<\/span><span class=\"hljs-params\">(<span class=\"hljs-type\">const<\/span> <span class=\"hljs-type\">void<\/span>* ptr, <span class=\"hljs-type\">size_t<\/span> size, <span class=\"hljs-type\">size_t<\/span> count, FILE* stream)<\/span><\/span>;\r\n<\/code><\/pre>\n<p>ptr is a pointer to the data block to be written, size is the number of bytes in each data block, count is the number of data blocks to be written, and stream is a pointer to the file to be written to.<\/p>\n<p>The fwrite function writes size*count bytes of data to the file stream and returns the actual number of data blocks written.<\/p>\n<p>Example code:<\/p>\n<pre class=\"post-pre\"><code><span class=\"hljs-meta\">#<span class=\"hljs-keyword\">include<\/span> <span class=\"hljs-string\">&lt;iostream&gt;<\/span><\/span>\r\n<span class=\"hljs-meta\">#<span class=\"hljs-keyword\">include<\/span> <span class=\"hljs-string\">&lt;cstdio&gt;<\/span><\/span>\r\n\r\n<span class=\"hljs-function\"><span class=\"hljs-type\">int<\/span> <span class=\"hljs-title\">main<\/span><span class=\"hljs-params\">()<\/span> <\/span>{\r\n    FILE* file = <span class=\"hljs-built_in\">fopen<\/span>(<span class=\"hljs-string\">\"data.txt\"<\/span>, <span class=\"hljs-string\">\"wb\"<\/span>); <span class=\"hljs-comment\">\/\/ \u4ee5\u4e8c\u8fdb\u5236\u5199\u5165\u6a21\u5f0f\u6253\u5f00\u6587\u4ef6<\/span>\r\n    <span class=\"hljs-keyword\">if<\/span> (file == <span class=\"hljs-literal\">nullptr<\/span>) {\r\n        std::cout &lt;&lt; <span class=\"hljs-string\">\"Failed to open file.\"<\/span> &lt;&lt; std::endl;\r\n        <span class=\"hljs-keyword\">return<\/span> <span class=\"hljs-number\">1<\/span>;\r\n    }\r\n    \r\n    <span class=\"hljs-type\">const<\/span> <span class=\"hljs-type\">char<\/span>* data = <span class=\"hljs-string\">\"Hello, World!\"<\/span>;\r\n    <span class=\"hljs-type\">size_t<\/span> size = <span class=\"hljs-built_in\">sizeof<\/span>(<span class=\"hljs-type\">char<\/span>);\r\n    <span class=\"hljs-type\">size_t<\/span> count = <span class=\"hljs-built_in\">strlen<\/span>(data);\r\n    \r\n    <span class=\"hljs-type\">size_t<\/span> written = <span class=\"hljs-built_in\">fwrite<\/span>(data, size, count, file);\r\n    <span class=\"hljs-keyword\">if<\/span> (written != count) {\r\n        std::cout &lt;&lt; <span class=\"hljs-string\">\"Error writing to file.\"<\/span> &lt;&lt; std::endl;\r\n    } <span class=\"hljs-keyword\">else<\/span> {\r\n        std::cout &lt;&lt; <span class=\"hljs-string\">\"Data written to file successfully.\"<\/span> &lt;&lt; std::endl;\r\n    }\r\n    \r\n    <span class=\"hljs-built_in\">fclose<\/span>(file); <span class=\"hljs-comment\">\/\/ \u5173\u95ed\u6587\u4ef6<\/span>\r\n    \r\n    <span class=\"hljs-keyword\">return<\/span> <span class=\"hljs-number\">0<\/span>;\r\n}\r\n<\/code><\/pre>\n<p>In this example, we open a file named data.txt, write the string &#8220;Hello, World!&#8221; to the file, and then close the file.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In C++, the fwrite function is used to write data blocks to a file. Its declaration is as follows: size_t fwrite(const void* ptr, size_t size, size_t count, FILE* stream); ptr is a pointer to the data block to be written, size is the number of bytes in each data block, count is the number of [&hellip;]<\/p>\n","protected":false},"author":6,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_import_markdown_pro_load_document_selector":0,"_import_markdown_pro_submit_text_textarea":"","footnotes":""},"categories":[1],"tags":[274,698,890,855,1654],"class_list":["post-3428","post","type-post","status-publish","format-standard","hentry","category-uncategorized","tag-c","tag-c-functions","tag-file-handling","tag-fwrite","tag-write-to-file"],"yoast_head":"<!-- This site is optimized with the Yoast SEO Premium plugin v21.5 (Yoast SEO v21.5) - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>fwrite in C++: Write Data Blocks to Files - Blog - Silicon Cloud<\/title>\n<meta name=\"description\" content=\"Learn C++ fwrite function: write data blocks to files. Explains parameters, return value, and usage examples.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.silicloud.com\/blog\/what-is-the-usage-of-the-fwrite-function-in-c\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"fwrite in C++: Write Data Blocks to Files\" \/>\n<meta property=\"og:description\" content=\"Learn C++ fwrite function: write data blocks to files. Explains parameters, return value, and usage examples.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.silicloud.com\/blog\/what-is-the-usage-of-the-fwrite-function-in-c\/\" \/>\n<meta property=\"og:site_name\" content=\"Blog - Silicon Cloud\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/SiliCloudGlobal\/\" \/>\n<meta property=\"article:published_time\" content=\"2024-03-13T06:57:29+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2025-07-30T16:10:57+00:00\" \/>\n<meta name=\"author\" content=\"Benjamin Taylor\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@SiliCloudGlobal\" \/>\n<meta name=\"twitter:site\" content=\"@SiliCloudGlobal\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Benjamin Taylor\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"1 minute\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/www.silicloud.com\/blog\/what-is-the-usage-of-the-fwrite-function-in-c\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/www.silicloud.com\/blog\/what-is-the-usage-of-the-fwrite-function-in-c\/\"},\"author\":{\"name\":\"Benjamin Taylor\",\"@id\":\"https:\/\/www.silicloud.com\/blog\/#\/schema\/person\/ac801fe9549a25960ce48aa2e0a691c9\"},\"headline\":\"fwrite in C++: Write Data Blocks to Files\",\"datePublished\":\"2024-03-13T06:57:29+00:00\",\"dateModified\":\"2025-07-30T16:10:57+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/www.silicloud.com\/blog\/what-is-the-usage-of-the-fwrite-function-in-c\/\"},\"wordCount\":117,\"publisher\":{\"@id\":\"https:\/\/www.silicloud.com\/blog\/#organization\"},\"keywords\":[\"c#\",\"C++ functions\",\"file handling\",\"fwrite\",\"write to file\"],\"inLanguage\":\"en-US\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.silicloud.com\/blog\/what-is-the-usage-of-the-fwrite-function-in-c\/\",\"url\":\"https:\/\/www.silicloud.com\/blog\/what-is-the-usage-of-the-fwrite-function-in-c\/\",\"name\":\"fwrite in C++: Write Data Blocks to Files - Blog - Silicon Cloud\",\"isPartOf\":{\"@id\":\"https:\/\/www.silicloud.com\/blog\/#website\"},\"datePublished\":\"2024-03-13T06:57:29+00:00\",\"dateModified\":\"2025-07-30T16:10:57+00:00\",\"description\":\"Learn C++ fwrite function: write data blocks to files. Explains parameters, return value, and usage examples.\",\"breadcrumb\":{\"@id\":\"https:\/\/www.silicloud.com\/blog\/what-is-the-usage-of-the-fwrite-function-in-c\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.silicloud.com\/blog\/what-is-the-usage-of-the-fwrite-function-in-c\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.silicloud.com\/blog\/what-is-the-usage-of-the-fwrite-function-in-c\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/www.silicloud.com\/blog\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"fwrite in C++: Write Data Blocks to Files\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.silicloud.com\/blog\/#website\",\"url\":\"https:\/\/www.silicloud.com\/blog\/\",\"name\":\"Silicon Cloud Blog\",\"description\":\"\",\"publisher\":{\"@id\":\"https:\/\/www.silicloud.com\/blog\/#organization\"},\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/www.silicloud.com\/blog\/#organization\",\"name\":\"Silicon Cloud Blog\",\"url\":\"https:\/\/www.silicloud.com\/blog\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.silicloud.com\/blog\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/www.silicloud.com\/blog\/wp-content\/uploads\/2023\/11\/EN-SILICON-Full.png\",\"contentUrl\":\"https:\/\/www.silicloud.com\/blog\/wp-content\/uploads\/2023\/11\/EN-SILICON-Full.png\",\"width\":1024,\"height\":1024,\"caption\":\"Silicon Cloud Blog\"},\"image\":{\"@id\":\"https:\/\/www.silicloud.com\/blog\/#\/schema\/logo\/image\/\"},\"sameAs\":[\"https:\/\/www.facebook.com\/SiliCloudGlobal\/\",\"https:\/\/twitter.com\/SiliCloudGlobal\"]},{\"@type\":\"Person\",\"@id\":\"https:\/\/www.silicloud.com\/blog\/#\/schema\/person\/ac801fe9549a25960ce48aa2e0a691c9\",\"name\":\"Benjamin Taylor\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.silicloud.com\/blog\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/ec2e3d3e2d525fd148047c4520ae7c1cdccd1f4b48a1a488422b31f04f345c14?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/ec2e3d3e2d525fd148047c4520ae7c1cdccd1f4b48a1a488422b31f04f345c14?s=96&d=mm&r=g\",\"caption\":\"Benjamin Taylor\"},\"url\":\"https:\/\/www.silicloud.com\/blog\/author\/benjamintaylor\/\"}]}<\/script>\n<!-- \/ Yoast SEO Premium plugin. -->","yoast_head_json":{"title":"fwrite in C++: Write Data Blocks to Files - Blog - Silicon Cloud","description":"Learn C++ fwrite function: write data blocks to files. Explains parameters, return value, and usage examples.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.silicloud.com\/blog\/what-is-the-usage-of-the-fwrite-function-in-c\/","og_locale":"en_US","og_type":"article","og_title":"fwrite in C++: Write Data Blocks to Files","og_description":"Learn C++ fwrite function: write data blocks to files. Explains parameters, return value, and usage examples.","og_url":"https:\/\/www.silicloud.com\/blog\/what-is-the-usage-of-the-fwrite-function-in-c\/","og_site_name":"Blog - Silicon Cloud","article_publisher":"https:\/\/www.facebook.com\/SiliCloudGlobal\/","article_published_time":"2024-03-13T06:57:29+00:00","article_modified_time":"2025-07-30T16:10:57+00:00","author":"Benjamin Taylor","twitter_card":"summary_large_image","twitter_creator":"@SiliCloudGlobal","twitter_site":"@SiliCloudGlobal","twitter_misc":{"Written by":"Benjamin Taylor","Est. reading time":"1 minute"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.silicloud.com\/blog\/what-is-the-usage-of-the-fwrite-function-in-c\/#article","isPartOf":{"@id":"https:\/\/www.silicloud.com\/blog\/what-is-the-usage-of-the-fwrite-function-in-c\/"},"author":{"name":"Benjamin Taylor","@id":"https:\/\/www.silicloud.com\/blog\/#\/schema\/person\/ac801fe9549a25960ce48aa2e0a691c9"},"headline":"fwrite in C++: Write Data Blocks to Files","datePublished":"2024-03-13T06:57:29+00:00","dateModified":"2025-07-30T16:10:57+00:00","mainEntityOfPage":{"@id":"https:\/\/www.silicloud.com\/blog\/what-is-the-usage-of-the-fwrite-function-in-c\/"},"wordCount":117,"publisher":{"@id":"https:\/\/www.silicloud.com\/blog\/#organization"},"keywords":["c#","C++ functions","file handling","fwrite","write to file"],"inLanguage":"en-US"},{"@type":"WebPage","@id":"https:\/\/www.silicloud.com\/blog\/what-is-the-usage-of-the-fwrite-function-in-c\/","url":"https:\/\/www.silicloud.com\/blog\/what-is-the-usage-of-the-fwrite-function-in-c\/","name":"fwrite in C++: Write Data Blocks to Files - Blog - Silicon Cloud","isPartOf":{"@id":"https:\/\/www.silicloud.com\/blog\/#website"},"datePublished":"2024-03-13T06:57:29+00:00","dateModified":"2025-07-30T16:10:57+00:00","description":"Learn C++ fwrite function: write data blocks to files. Explains parameters, return value, and usage examples.","breadcrumb":{"@id":"https:\/\/www.silicloud.com\/blog\/what-is-the-usage-of-the-fwrite-function-in-c\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.silicloud.com\/blog\/what-is-the-usage-of-the-fwrite-function-in-c\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.silicloud.com\/blog\/what-is-the-usage-of-the-fwrite-function-in-c\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.silicloud.com\/blog\/"},{"@type":"ListItem","position":2,"name":"fwrite in C++: Write Data Blocks to Files"}]},{"@type":"WebSite","@id":"https:\/\/www.silicloud.com\/blog\/#website","url":"https:\/\/www.silicloud.com\/blog\/","name":"Silicon Cloud Blog","description":"","publisher":{"@id":"https:\/\/www.silicloud.com\/blog\/#organization"},"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/www.silicloud.com\/blog\/#organization","name":"Silicon Cloud Blog","url":"https:\/\/www.silicloud.com\/blog\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.silicloud.com\/blog\/#\/schema\/logo\/image\/","url":"https:\/\/www.silicloud.com\/blog\/wp-content\/uploads\/2023\/11\/EN-SILICON-Full.png","contentUrl":"https:\/\/www.silicloud.com\/blog\/wp-content\/uploads\/2023\/11\/EN-SILICON-Full.png","width":1024,"height":1024,"caption":"Silicon Cloud Blog"},"image":{"@id":"https:\/\/www.silicloud.com\/blog\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/SiliCloudGlobal\/","https:\/\/twitter.com\/SiliCloudGlobal"]},{"@type":"Person","@id":"https:\/\/www.silicloud.com\/blog\/#\/schema\/person\/ac801fe9549a25960ce48aa2e0a691c9","name":"Benjamin Taylor","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.silicloud.com\/blog\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/ec2e3d3e2d525fd148047c4520ae7c1cdccd1f4b48a1a488422b31f04f345c14?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/ec2e3d3e2d525fd148047c4520ae7c1cdccd1f4b48a1a488422b31f04f345c14?s=96&d=mm&r=g","caption":"Benjamin Taylor"},"url":"https:\/\/www.silicloud.com\/blog\/author\/benjamintaylor\/"}]}},"_links":{"self":[{"href":"https:\/\/www.silicloud.com\/blog\/wp-json\/wp\/v2\/posts\/3428","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.silicloud.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.silicloud.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.silicloud.com\/blog\/wp-json\/wp\/v2\/users\/6"}],"replies":[{"embeddable":true,"href":"https:\/\/www.silicloud.com\/blog\/wp-json\/wp\/v2\/comments?post=3428"}],"version-history":[{"count":3,"href":"https:\/\/www.silicloud.com\/blog\/wp-json\/wp\/v2\/posts\/3428\/revisions"}],"predecessor-version":[{"id":148071,"href":"https:\/\/www.silicloud.com\/blog\/wp-json\/wp\/v2\/posts\/3428\/revisions\/148071"}],"wp:attachment":[{"href":"https:\/\/www.silicloud.com\/blog\/wp-json\/wp\/v2\/media?parent=3428"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.silicloud.com\/blog\/wp-json\/wp\/v2\/categories?post=3428"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.silicloud.com\/blog\/wp-json\/wp\/v2\/tags?post=3428"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}