{"id":2818,"date":"2017-08-18T15:07:36","date_gmt":"2017-08-18T15:07:36","guid":{"rendered":"https:\/\/raspberry-projects.com\/pi\/?p=2818"},"modified":"2019-01-30T15:36:50","modified_gmt":"2019-01-30T15:36:50","slug":"create-text-file","status":"publish","type":"post","link":"https:\/\/raspberry-projects.com\/pi\/programming-in-c\/file-input-and-output\/file-operations\/create-text-file","title":{"rendered":"Create Text File"},"content":{"rendered":"\n<h4 class=\"wp-block-heading\">Append to text file<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>#include &lt;fstream>\n\t\n\tstd::ofstream ofstream1(MyFilepath.c_str(), std::ios_base::app);\t\t\t\t\/\/Append to existing file if present\n\n\tofstream1 &lt;&lt; \"Hello\";\n\tofstream1.close();<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">Overwrite A Text File<br> <\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>\n#include &lt;fstream>\n\n\/\/****************************************\n\/\/****************************************\n\/\/********** CREATE A TEXT FILE **********\n\/\/****************************************\n\/\/****************************************\nvoid create_some_text_file (void)\n{\n\n\t\/\/----- CREATE A COPY OF THE ORIGINAL FILE IF THIS IS THE FIRST TIME WE'RE DOING THIS -----\n\tstruct stat sb;\n\tint result = stat(\"\/etc\/wpa_supplicant\/wpa_supplicant-original.conf\", &amp;sb);\n\tif ((result != 0) || (sb.st_mode &amp; S_IFDIR))\n\t{\n\t\tsystem(\"sudo cp \/etc\/wpa_supplicant\/wpa_supplicant.conf \/etc\/wpa_supplicant\/wpa_supplicant-original.conf\");\n\t}\n\t\n\t\/\/----- CREATE NEW VERSION OF THE FILE -----\n\t\n\t\/\/You can use this if you want to use the contents of some existing file as part of the output:\n\t\/\/\tstd::ifstream ifstream1(\"\/etc\/some_source_file.conf\", std::ios_base::in);\n\t\/\/Then just use this wherever you want to insert it:\n\t\/\/\toutput_file &lt;&lt; ifstream1.rdbuf();\n\t\n    std::ofstream output_file(\"\/etc\/wpa_supplicant\/wpa_supplicant.conf\");\t\t\t\t\/\/Will overwrite an existing file\n\n\toutput_file &lt;&lt; \"# THIS FILE HAS BEEN AUTO GENERATED \\n\";\n\toutput_file &lt;&lt; \"\\n\";\n\t\t\n    output_file.close();\n}\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Append to text file Overwrite A Text File<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[135],"tags":[],"class_list":["post-2818","post","type-post","status-publish","format-standard","hentry","category-file-operations"],"_links":{"self":[{"href":"https:\/\/raspberry-projects.com\/pi\/wp-json\/wp\/v2\/posts\/2818","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/raspberry-projects.com\/pi\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/raspberry-projects.com\/pi\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/raspberry-projects.com\/pi\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/raspberry-projects.com\/pi\/wp-json\/wp\/v2\/comments?post=2818"}],"version-history":[{"count":5,"href":"https:\/\/raspberry-projects.com\/pi\/wp-json\/wp\/v2\/posts\/2818\/revisions"}],"predecessor-version":[{"id":2994,"href":"https:\/\/raspberry-projects.com\/pi\/wp-json\/wp\/v2\/posts\/2818\/revisions\/2994"}],"wp:attachment":[{"href":"https:\/\/raspberry-projects.com\/pi\/wp-json\/wp\/v2\/media?parent=2818"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/raspberry-projects.com\/pi\/wp-json\/wp\/v2\/categories?post=2818"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/raspberry-projects.com\/pi\/wp-json\/wp\/v2\/tags?post=2818"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}