{"id":476,"date":"2013-09-26T09:20:49","date_gmt":"2013-09-26T13:20:49","guid":{"rendered":"http:\/\/templesystems.com\/?page_id=476"},"modified":"2013-09-26T14:28:59","modified_gmt":"2013-09-26T18:28:59","slug":"file-stuff","status":"publish","type":"page","link":"http:\/\/templesystems.com\/?page_id=476","title":{"rendered":"File Stuff"},"content":{"rendered":"<h1>Some basic file operations in C\/C++, Windows operating system<\/h1>\n<h2>Summary<\/h2>\n<p>Where possible, the following functions use standard library functions, which makes the code portable to other operating systems. \u00a0Some of the examples use Windows API functions, because there are no standard library functions that directly do the same thing. \u00a0Copying a file is an example. \u00a0The standard library does not provide a single function to copy a file. \u00a0(it is possible to copy files by reads and writes, but there is no &#8220;copy file&#8221; function that will do it in a single line of code.) \u00a0It that case, I defer to the Windows API functions.<\/p>\n<h3>Open\/Close a file<\/h3>\n<pre>    FILE\u00a0*fp;<\/pre>\n<pre>    string  FilePath(\"c:\\\\test.txt\");<\/pre>\n<pre>    if((fp = fopen(FilePath.c_str(), \"a\")) == NULL)<\/pre>\n<pre>        return;<\/pre>\n<pre>    \/\/ do file operations here<\/pre>\n<pre>    fclose(fp);<\/pre>\n<h3>\u00a0Copy a file<\/h3>\n<pre>    string CopyFromPath(\"c:\\\\test.txt\");<\/pre>\n<pre>    string CopyToPath(\"c:\\\\test2.txt\");<\/pre>\n<pre>    CopyFile(CopyFromPath.c_str(), CopyToPath.c_str(), false);<\/pre>\n<p>The CopyFile function is windows specific. \u00a0This copies c:\\test.txt to c:\\test2.txt. \u00a0If the output file (test2.txt) exists, it will be overwritten. \u00a0If that is not desirable, the &#8216;false&#8217; should be changed to &#8216;true&#8217;. \u00a0When set to true, the last parameter specifies that the operation should fail (not happen) if the output file already exists.<\/p>\n<h3>Rename a file<\/h3>\n<pre>    rename(\"c:\\\\test.txt\", \"c:\\\\test2.txt\");  \/\/ hard coded file names<\/pre>\n<pre>\u00a0   string oldname(\"c:\\\\test.txt\");<\/pre>\n<pre>    string newname(\"c:\\\\test2.txt\");<\/pre>\n<pre>    rename(oldname.c_str(), newname.c_str()); \/\/ this does the same thing.<\/pre>\n<p>To see if the operation succeeded, evaluate what the function returns. \u00a0If it is non-zero, the operation failed.<\/p>\n<pre><span style=\"font-family: Consolas, Monaco, monospace; font-size: 12px; line-height: 18px;\"> int error;<\/span><\/pre>\n<p><span style=\"font-family: Consolas, Monaco, monospace; font-size: 12px; line-height: 18px;\">\u00a0 \u00a0 error = rename(oldname.c_str(), newname.c_str());<\/span><\/p>\n<pre>    if (error)<\/pre>\n<pre>        ... something went wrong... handle it.<\/pre>\n<h3>Delete a file<\/h3>\n<pre>   remove(\"c:\\\\test2.txt\");<\/pre>\n<pre>   string file_to_delete(\"c:\\\\test2.txt\");<\/pre>\n<pre>   remove(file_to_delete.c_str());<\/pre>\n<pre><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Some basic file operations in C\/C++, Windows operating system Summary Where possible, the following functions use standard library functions, which makes the code portable to other operating systems. \u00a0Some of the examples use Windows API functions, because there are no &hellip; <a href=\"http:\/\/templesystems.com\/?page_id=476\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"parent":303,"menu_order":0,"comment_status":"open","ping_status":"open","template":"","meta":{"footnotes":""},"class_list":["post-476","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"http:\/\/templesystems.com\/index.php?rest_route=\/wp\/v2\/pages\/476","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/templesystems.com\/index.php?rest_route=\/wp\/v2\/pages"}],"about":[{"href":"http:\/\/templesystems.com\/index.php?rest_route=\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"http:\/\/templesystems.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/templesystems.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=476"}],"version-history":[{"count":6,"href":"http:\/\/templesystems.com\/index.php?rest_route=\/wp\/v2\/pages\/476\/revisions"}],"predecessor-version":[{"id":478,"href":"http:\/\/templesystems.com\/index.php?rest_route=\/wp\/v2\/pages\/476\/revisions\/478"}],"up":[{"embeddable":true,"href":"http:\/\/templesystems.com\/index.php?rest_route=\/wp\/v2\/pages\/303"}],"wp:attachment":[{"href":"http:\/\/templesystems.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=476"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}