{"id":3758,"date":"2021-12-11T06:38:54","date_gmt":"2021-12-11T06:38:54","guid":{"rendered":"http:\/\/truelogic.org\/wordpress\/?p=3758"},"modified":"2021-12-11T06:51:53","modified_gmt":"2021-12-11T06:51:53","slug":"2f-wxwidgets-files-directories","status":"publish","type":"post","link":"https:\/\/truelogic.org\/wordpress\/2021\/12\/11\/2f-wxwidgets-files-directories\/","title":{"rendered":"2f.wxWidgets &#8211; Files &#038; Directories"},"content":{"rendered":"\n<p>All standard file and directory operations are supported in wxWidgets. Below are two sample programs  &#8211; the first writes and reads a text file while the second does directory functions.<\/p>\n\n\n\n<p>SAMPLE CODE<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">filereadwrite.cpp<\/pre>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\n#include &lt;wx\/wxprec.h&gt;\n#ifndef WX_PRECOMP\n\t#include &lt;wx\/wx.h&gt;\n#endif\n#include &lt;wx\/string.h&gt;\n#include &lt;wx\/file.h&gt;\n#include &lt;wx\/textfile.h&gt;\n\nint main(int argc, char **argv) {\n\twxInitialize();\n\n\twxString line = wxT(&quot;This is a line of text\\nThis is second line.\\nThis is last line.\\n&quot;);\n\twxFile file;\n\tfile.Create(wxT(&quot;file.dat&quot;), true);\n\tif (file.IsOpened()) {\n\t\tfile.Write(line);\n\t\tfile.Close();\n\t} else {\n\t\twxPuts(wxT(&quot;Could not open file&quot;));\n\t\treturn -1;\n\t}\n\t\n\t\n\twxTextFile tfile;\n\ttfile.Open(wxT(&quot;file.dat&quot;));\n\tif (!tfile.IsOpened()) {\n\t\twxPuts(&quot;File could not be read&quot;);\n\t\treturn -1;\n\t}\n\twxPrintf(wxT(&quot;Line count: %ld\\n&quot;), tfile.GetLineCount());\n\n\twxString data;\n\tdata = tfile.GetFirstLine();\n\twhile (!tfile.Eof()) {\n\t   wxPuts(data);\n\t   data = tfile.GetNextLine();\n\n\t}\n\ttfile.Close();\n\n\twxUninitialize();\n}\n\n<\/pre><\/div>\n\n\n<p>The output is given below:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">Line count: 3\n This is a line of text\n This is second line.\n This is last line.\n\n<\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\">directory.cpp<\/pre>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\n#include &lt;wx\/wxprec.h&gt;\n#ifndef WX_PRECOMP\n\t#include &lt;wx\/wx.h&gt;\n#endif\n#include &lt;wx\/string.h&gt;\n#include &lt;wx\/filefn.h&gt;\n#include &lt;wx\/dir.h&gt;\n\nint main(int argc, char **argv) {\n\twxInitialize();\n\n\twxDir curr(wxGetCwd());\n\t\n\twxString f;\n\tbool flag = curr.GetFirst(&amp;f, wxEmptyString, wxDIR_FILES | wxDIR_DIRS);\n\twhile (flag) {\n\t\twxPuts(f);\n\t\tflag = curr.GetNext(&amp;f);\n\t}\t\n\n\twxPuts(&quot;========================&quot;);\n\tflag = curr.GetFirst(&amp;f, wxT(&quot;*.cpp&quot;), wxDIR_FILES | wxDIR_DIRS);\n\twhile (flag) {\n\t\twxPuts(f);\n\t\tflag = curr.GetNext(&amp;f);\n\t}\t\n\n\twxPuts(&quot;========================&quot;);\n\tbool ret = wxMkdir(wxT(&quot;tempdir&quot;));\n\tif (!ret) {\n\t    wxPuts(&quot;Could not create tempdir&quot;);\n\t} else {\n\t    wxPuts(&quot;tempdir created&quot;);\n\t    ret = wxRmdir(wxT(&quot;tempdir&quot;));\n\t    if (ret)\n\t\t    wxPuts(&quot;tempdir deleted&quot;);\n\t}\n\n\twxUninitialize();\n}\n\n<\/pre><\/div>\n\n\n<p>The output is given below:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">file.dat\n platform.cpp\n platform\n directory\n filereadwrite\n directory.cpp\n console\n datetime\n pcontrol\n pcontrol.cpp\n strings\n datetime.cpp\n filereadwrite.cpp\n strings.cpp\n console.cpp\n platform.cpp\n directory.cpp\n pcontrol.cpp\n datetime.cpp\n filereadwrite.cpp\n strings.cpp\n console.cpp\n tempdir created\n tempdir deleted<\/pre>\n","protected":false},"excerpt":{"rendered":"<div class=\"mh-excerpt\"><p>All standard file and directory operations are supported in wxWidgets. Below are two sample programs &#8211; the first writes and reads a text file while <a class=\"mh-excerpt-more\" href=\"https:\/\/truelogic.org\/wordpress\/2021\/12\/11\/2f-wxwidgets-files-directories\/\" title=\"2f.wxWidgets &#8211; Files &#038; Directories\">[&#8230;]<\/a><\/p>\n<\/div>","protected":false},"author":1,"featured_media":3595,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[302,365],"tags":[],"class_list":["post-3758","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-cc","category-wxwidgets"],"_links":{"self":[{"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/posts\/3758","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/comments?post=3758"}],"version-history":[{"count":3,"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/posts\/3758\/revisions"}],"predecessor-version":[{"id":3767,"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/posts\/3758\/revisions\/3767"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/media\/3595"}],"wp:attachment":[{"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/media?parent=3758"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/categories?post=3758"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/tags?post=3758"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}