{"id":239,"date":"2010-04-29T17:53:55","date_gmt":"2010-04-29T17:53:55","guid":{"rendered":"https:\/\/ibex.tech\/visualcpp\/?p=239"},"modified":"2022-02-17T06:24:05","modified_gmt":"2022-02-17T06:24:05","slug":"read-file","status":"publish","type":"post","link":"https:\/\/ibex.tech\/visualcpp\/file-input-output-serialization\/read-file","title":{"rendered":"Read File"},"content":{"rendered":"<pre>\r\n<code>\r\nusing namespace System::IO;\r\n<\/code><\/pre>\n<h4>\nRead Bytes From A File<br \/>\n<\/h4>\n<pre>\r\n<code>\r\n\t__int64 FileByteNumber;\r\n\tint ReadByte;\r\n\r\n\tFileStream ^SourceFile = gcnew FileStream(SelectFileDialog-&gt;FileName, FileMode::Open, FileAccess::Read, FileShare::None);\r\n\r\n\tfor (FileByteNumber = 0; FileByteNumber &lt; SourceFile-&gt;Length; FileByteNumber++)\r\n\t{\r\n\t\t\/\/----- READ EACH BYTE -----\r\n\t\tif ((ReadByte = SourceFile-&gt;ReadByte()) == -1)\r\n\t\t\tthrow gcnew Exception(&quot;File read too short&quot;);\r\n\r\n\t\t\/\/ReadByte has the value but is actually an integer\r\n\t}\r\n\tSourceFile-&gt;Close();\r\n<\/code><\/pre>\n<h4>\nOverwrite Bytes In A File As Its Read<br \/>\n<\/h4>\n<pre>\r\n<code>\r\n\t\/\/Use\r\n\tFileAccess::ReadWrite\r\n\t\/\/and\r\n\tSourceFile-&gt;WriteByte(Convert::ToByte(&#39;-&#39;))\r\n\r\nTo move back 1 byte before writing:\r\n\tSourceFile-&gt;Seek(-1,SeekOrigin::Current);\r\n\tSourceFile-&gt;WriteByte(Convert::ToByte(&#39;-&#39;));\r\n\tSourceFile-&gt;ReadByte();\r\n<\/code><\/pre>\n<h4>\nRead String From A File<br \/>\n<\/h4>\n<pre>\r\n<code>\r\n\tif (File::Exists(TemporaryDirectory + EXTERNAL_EXPORTER_STATUS_FILE))\r\n\t{\r\n\t\tStreamReader ^StreamReader1 = gcnew StreamReader(TemporaryDirectory + EXTERNAL_EXPORTER_STATUS_FILE);\r\n\t\tMyString = StreamReader1-&gt;ReadToEnd();\r\n\t\tStreamReader1-&gt;Close();\r\n\t}\r\n<\/code><\/pre>\n<p>\n&nbsp;\n<\/p>\n<p>\n&nbsp;\n<\/p>\n<p>\n&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>using namespace System::IO; Read Bytes From A File __int64 FileByteNumber; int ReadByte; FileStream ^SourceFile = gcnew FileStream(SelectFileDialog-&gt;FileName, FileMode::Open, FileAccess::Read, FileShare::None); for (FileByteNumber = 0; FileByteNumber &lt; SourceFile-&gt;Length; FileByteNumber++) { \/\/&#8212;&#8211; READ EACH BYTE &#8212;&#8211; if ((ReadByte = SourceFile-&gt;ReadByte()) == -1) throw gcnew Exception(&quot;File read too short&quot;); \/\/ReadByte has the value but is actually an integer [&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-239","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\/239","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=239"}],"version-history":[{"count":5,"href":"https:\/\/ibex.tech\/visualcpp\/wp-json\/wp\/v2\/posts\/239\/revisions"}],"predecessor-version":[{"id":985,"href":"https:\/\/ibex.tech\/visualcpp\/wp-json\/wp\/v2\/posts\/239\/revisions\/985"}],"wp:attachment":[{"href":"https:\/\/ibex.tech\/visualcpp\/wp-json\/wp\/v2\/media?parent=239"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ibex.tech\/visualcpp\/wp-json\/wp\/v2\/categories?post=239"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ibex.tech\/visualcpp\/wp-json\/wp\/v2\/tags?post=239"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}