{"id":426,"date":"2010-12-21T18:19:02","date_gmt":"2010-12-21T18:19:02","guid":{"rendered":"https:\/\/ibex.tech\/visualcpp\/?p=426"},"modified":"2022-02-17T06:24:04","modified_gmt":"2022-02-17T06:24:04","slug":"write-new-file","status":"publish","type":"post","link":"https:\/\/ibex.tech\/visualcpp\/file-input-output-serialization\/write-new-file","title":{"rendered":"Write New File"},"content":{"rendered":"<pre>\r\n<code>\r\nusing namespace System::IO;\r\n<\/code><\/pre>\n<h4>\nSave As Dialog Box<br \/>\n<\/h4>\n<pre>\r\n<code>\r\n\tString ^SaveAsFilename;\r\n\r\n\t\/\/If last directory is not valid then default to My Documents\r\n\tif (!Directory::Exists(Path::GetDirectoryName(LastFileDirectory)))\r\n\t\tLastFileDirectory = Environment::GetFolderPath(Environment::SpecialFolder::MyDocuments);\r\n\r\n\t\/\/----- SAVE FILE DIALOG BOX -----\r\n\tSaveFileDialog ^SelectFileDialog = gcnew SaveFileDialog();\r\n\r\n\tSelectFileDialog-&gt;Filter = &quot;Comma Separated Values (*.&quot; + MY_FILE_EXTENSION + &quot;)|*.&quot; + MY_FILE_EXTENSION;\t\t\/\/&quot;txt files (*.txt)|*.txt|All files (*.*)|*.*&quot;\r\n\tSelectFileDialog-&gt;FilterIndex = 1;\t\t\t\t\/\/(First = 1, not 0)\r\n\tSelectFileDialog-&gt;Title = &quot;Save Live Log As&quot;;\r\n\tSelectFileDialog-&gt;OverwritePrompt = true;\r\n\ttry\r\n\t{\r\n\t\tSelectFileDialog-&gt;InitialDirectory = LastFileDirectory;\r\n\t}\r\n\tcatch (Exception ^)\r\n\t{\r\n\t\tSelectFileDialog-&gt;InitialDirectory = Environment::GetFolderPath(Environment::SpecialFolder::MyDocuments);\r\n\t}\r\n\t\/\/Display dialog box\r\n\tif (SelectFileDialog-&gt;ShowDialog() == System::Windows::Forms::DialogResult::OK)\t\t\/\/This ensures correct filename extension is used\r\n\t{\r\n\t\t\/\/----- SAVE THE FILE -----\r\n\t\tSaveAsFilename = SelectFileDialog-&gt;FileName;\r\n\r\n\t\t\/\/STORE LAST USED DIRECTORY\r\n\t\tif (SaveAsFilename-&gt;LastIndexOf(&quot;\\\\&quot;) &gt;= 0)\r\n\t\t\tLastFileDirectory = SaveAsFilename-&gt;Substring(0, (SaveAsFilename-&gt;LastIndexOf(&quot;\\\\&quot;) + 1));\r\n\t\telse\r\n\t\t\tLastFileDirectory = Environment::GetFolderPath( Environment::SpecialFolder::MyDocuments);\r\n<\/code><\/pre>\n<h4>\nWrite StringTo A New File<br \/>\n<\/h4>\n<pre>\r\n<code>\r\n\tStreamWriter ^StreamWriter1= gcnew StreamWriter(&quot;C:\\\\test.txt&quot;);\r\n\t\/\/StreamWriter ^StreamWriter1= gcnew StreamWriter(&quot;C:\\\\test.txt&quot;, false, System::Text::Encoding::Unicode);\t\/\/False = overwrite - Alternative method specifying the encoding\r\n\tStreamWriter1-&gt;Write(MyString);\r\n\tStreamWriter1-&gt;Close();\r\n<\/code><\/pre>\n<h4>\nWrite Bytes To A New File<br \/>\n<\/h4>\n<p>\n(Write unsigned char or arrays of unsigned char to a file &#8211; not strings or anything else)\n<\/p>\n<pre>\r\n<code>\r\n\tFileStream ^HeaderOutputFile = gcnew FileStream(MyFilePath, FileMode::Create,\r\n\t\t\t\t\t\t\t\tFileAccess::Write, FileShare::None);\r\n\r\n\tHeaderOutputFile-&gt;WriteByte(0x01);\r\n\t\/\/...\r\n\tHeaderOutputFile-&gt;Close();\r\n<\/code><\/pre>\n<h4>\nWrite Binary (Inc Strings) To A New File<br \/>\n<\/h4>\n<p>\nThe problem with writing using FileStream is that its not string friendly. You can write strings using the StreamWriter, but if you want to write strings and binary data then it&#39;s easier to use the Binary Writer.\n<\/p>\n<pre>\r\n<code>\r\n\tString ^sTemp;\r\n\r\n\tFileStream ^MyOutputFile = gcnew FileStream(&quot;C:\\\\test.txt&quot;, FileMode::Create, FileAccess::Write, FileShare::None);\r\n\tBinaryWriter ^MyOutputWriter = gcnew BinaryWriter(MyOutputFile);\r\n\r\n\tsTemp = &quot;Hello World!\\r\\n&quot;;\r\n\tMyOutputWriter-&gt;Write(sTemp-&gt;ToCharArray());\t\/\/ToCharArray to avoid String type byte being written at start of the line\r\n\tMyOutputWriter-&gt;Write(Convert::ToByte(0x01));\t\/\/Write will write any variable it is given using as many bytes as needed\r\n\t\/\/...\r\n\tMyOutputWriter-&gt;Close();\r\n\tMyOutputFile-&gt;Close();\r\n\r\n\t\/\/GOOD IDEA TO ADD THIS TO TRY CATCH BLOCK AND REMOVE THE TWO -&gt;CLOSE() LINES ABOVE:-\r\n\tfinally\r\n\t{\r\n\t\tif (MyOutputWriter != nullptr)\r\n\t\t\tMyOutputWriter-&gt;Close();\r\n\t\tif (MyOutputFile != nullptr)\r\n\t\t\tMyOutputFile-&gt;Close();\r\n\t}\r\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>using namespace System::IO; Save As Dialog Box String ^SaveAsFilename; \/\/If last directory is not valid then default to My Documents if (!Directory::Exists(Path::GetDirectoryName(LastFileDirectory))) LastFileDirectory = Environment::GetFolderPath(Environment::SpecialFolder::MyDocuments); \/\/&#8212;&#8211; SAVE FILE DIALOG BOX &#8212;&#8211; SaveFileDialog ^SelectFileDialog = gcnew SaveFileDialog(); SelectFileDialog-&gt;Filter = &quot;Comma Separated Values (*.&quot; + MY_FILE_EXTENSION + &quot;)|*.&quot; + MY_FILE_EXTENSION; \/\/&quot;txt files (*.txt)|*.txt|All files (*.*)|*.*&quot; SelectFileDialog-&gt;FilterIndex = [&hellip;]<\/p>\n","protected":false},"author":5,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[29],"tags":[],"class_list":["post-426","post","type-post","status-publish","format-standard","hentry","category-file-input-output-serialization"],"_links":{"self":[{"href":"https:\/\/ibex.tech\/visualcpp\/wp-json\/wp\/v2\/posts\/426","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ibex.tech\/visualcpp\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ibex.tech\/visualcpp\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ibex.tech\/visualcpp\/wp-json\/wp\/v2\/users\/5"}],"replies":[{"embeddable":true,"href":"https:\/\/ibex.tech\/visualcpp\/wp-json\/wp\/v2\/comments?post=426"}],"version-history":[{"count":5,"href":"https:\/\/ibex.tech\/visualcpp\/wp-json\/wp\/v2\/posts\/426\/revisions"}],"predecessor-version":[{"id":980,"href":"https:\/\/ibex.tech\/visualcpp\/wp-json\/wp\/v2\/posts\/426\/revisions\/980"}],"wp:attachment":[{"href":"https:\/\/ibex.tech\/visualcpp\/wp-json\/wp\/v2\/media?parent=426"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ibex.tech\/visualcpp\/wp-json\/wp\/v2\/categories?post=426"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ibex.tech\/visualcpp\/wp-json\/wp\/v2\/tags?post=426"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}