{"id":2776,"date":"2017-11-01T00:01:35","date_gmt":"2017-11-01T07:01:35","guid":{"rendered":"http:\/\/c-for-dummies.com\/blog\/?p=2776"},"modified":"2017-11-08T08:23:01","modified_gmt":"2017-11-08T16:23:01","slug":"filename-extractor","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=2776","title":{"rendered":"Filename Extractor"},"content":{"rendered":"<p><a href=\"http:\/\/c-for-dummies.com\/blog\/?p=2719\">Last month&#8217;s Exercise<\/a> demonstrated a filename-extractor, but it cheated: The filename was always the same: same text, same length. That&#8217;s not always the case in the real world.<br \/>\n<!--more--><br \/>\nThe Big Picture view of the Exercise was to pull a filename from a pathname. Pathnames can start anywhere, though a full pathname begins at the root. In Windows, it includes the drive letter, such as <code>C:\\<\/code>. In Unix, full pathnames start in the root directory, <code>\/<\/code>.<\/p>\n<p>You can also have a partial pathname, which might include the <code>~<\/code> home directory shortcut or assume which directory is current. Pathnames can be relative, such as <code>..\/..\/another\/directory<\/code>. And pathnames don&#8217;t need to include a filename; a pathname can merely reference a directory. Traditionally, such a pathname terminates with a separator character, <code>\/<\/code> (Unix) or <code>\\<\/code> (Windows), though this rule isn&#8217;t strict.<\/p>\n<blockquote><p>In my code, I always end a pathname with a separator, <code>\/<\/code> or <code>\\<\/code>. That&#8217;s because filenames aren&#8217;t prefixed with a separator. Therefore, when I concatenate pathnames, I know that the pathname ends in a separator character and I don&#8217;t need to add one before the filename.<\/p><\/blockquote>\n<p>For this month&#8217;s Exercise, I present a list of six pathnames. Your task is to extract the filename from each pathname. The path may not have a filename, and further the filename may not have an extension.<\/p>\n<p>To assist you, I present the following code skeleton:<\/p>\n<pre class=\"screen\">\r\n#include &lt;stdio.h&gt;\r\n\r\nchar *filename(char *path)\r\n{   \r\n}   \r\n\r\nint main()\r\n{\r\n    char *pathnames[] = {\r\n        \"~\/Documents\/web\/images\/title.png\",\r\n        \"\/mount\/omni\/rex\/sto\/finance\",\r\n        \"~\/Documents\/web\/media\/menu.swf\",\r\n        \"~\/Pictures\/family\/2011\/birthdays\/jonah_cake.jpeg\",\r\n        \"\/users\/dan\/downloads\/\",\r\n        \"yellow.doc\"\r\n    };  \r\n    \r\n    <span class=\"comments\">\/* display all pathnames \/ filenames *\/<\/span>\r\n    for(int x=0;x&lt;6;x++)\r\n    {\r\n    }\r\n        \r\n    return(0);  \r\n}<\/pre>\n<p>The bulk of the action occurs in the <em>filename()<\/em> function, which digests pathnames and spews out the filename portion &mdash; if any. The <em>main()<\/em> function contains the sample pathnames, plus a <em>for<\/em> loop to process each one.<\/p>\n<p>To further guide you, here is sample output from my solution:<\/p>\n<pre><code>~\/Documents\/web\/images\/title.png -&gt; title.png\r\n\/mount\/omni\/rex\/sto\/finance -&gt; finance\r\n~\/Documents\/web\/media\/menu.swf -&gt; menu.swf\r\n~\/Pictures\/family\/2011\/birthdays\/jonah_cake.jpeg -&gt; jonah_cake.jpeg\r\n\/users\/dan\/downloads\/ -&gt; [No filename]\r\nyellow.doc -&gt; yellow.doc<\/code><\/pre>\n<p>Please attempt to conquer this Exercise on your own. View <a href=\"http:\/\/c-for-dummies.com\/blog\/?p=2791\">my solution here<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Given a pathname of any length, return all but the final part, the filename. <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=2776\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3],"tags":[],"class_list":["post-2776","post","type-post","status-publish","format-standard","hentry","category-exercise"],"_links":{"self":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/2776","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=2776"}],"version-history":[{"count":7,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/2776\/revisions"}],"predecessor-version":[{"id":2825,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/2776\/revisions\/2825"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2776"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=2776"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=2776"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}