{"id":6054,"date":"2023-10-07T00:01:42","date_gmt":"2023-10-07T07:01:42","guid":{"rendered":"https:\/\/c-for-dummies.com\/blog\/?p=6054"},"modified":"2023-10-14T11:08:29","modified_gmt":"2023-10-14T18:08:29","slug":"finding-the-dictionary","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=6054","title":{"rendered":"Finding the Dictionary"},"content":{"rendered":"<p>My first Unix System Administrator job was pretty routine: I did backups. It was only later that I discovered some of the many nerdy treasures available in that operating system.<br \/>\n<!--more--><br \/>\nOne of the Unix treasures (shared by Linux and macOS) is the dictionary. It&#8217;s a text file containing words, but not the definitions. The dictionary most likely came about to assist with spell-checking. For example, the <em>spell<\/em> command uses the dictionary. Beyond that, someone somewhere at some time decided that Unix needed a dictionary. It has one.<\/p>\n<p>The dictionary file may not be installed on your Linux distro. Use the Linux package manager, such as <em>apt<\/em>, to obtain a dictionary. Different types are available for every language and variation. For example, English dictionaries include British, American, and Canadian versions. Then you can elect to install small, medium, or large dictionaries. The size is based on the number of words included.<\/p>\n<p>As with any file on your computer, you can use your C programming kung fu to access and examine the dictionary file&#8217;s contents. You can search for words, word patterns, or even code a program to pluck out random words and have your computer blabber at you. The process starts by obtaining and installing the dictionary and then accessing its file.<\/p>\n<p>The dictionary traditionally dwelles in the <code>\/usr\/share\/dict<\/code> directory, though the location may be different on a given system. The dictionary name is different as well, depending on the version you install. For example, on my computer the file is named <code>american-english<\/code>. Even so, the installer creates an alias <code>words<\/code> that reference the dictionary file no matter what its true name.<\/p>\n<p>The following code performs a test to ensure that the named dictionary file is installed and available. The defined constant <code>DICTIONARY<\/code> is set equal to the dictionary file&#8217;s full pathname. Remember to change it on your own system if necessary.<\/p>\n<h3><a href=\"https:\/\/github.com\/dangookin\/C-For-Dummies-Blog\/blob\/master\/2023_10_07-Lesson.c\" rel=\"noopener\" target=\"_blank\">2023_10_07-Lesson.c<\/a><\/h3>\n<pre class=\"screen\">\r\n<span class=\"comments\">\/* Look up the dictionary *\/<\/span>\r\n#include &lt;stdio.h&gt;\r\n#include &lt;stdlib.h&gt;\r\n#include &lt;unistd.h&gt;\r\n\r\n#define DICTIONARY \"\/usr\/share\/dict\/words\"\r\n\r\nint main()\r\n{\r\n    int r;\r\n\r\n    r = access(DICTIONARY,F_OK);\r\n    if( r==-1 )\r\n        puts(\"Dictionary file is not installed\");\r\n    else\r\n        puts(\"Dictionary file is installed\");\r\n\r\n    return(0);\r\n}<\/pre>\n<p>This code uses the <em>access()<\/em> function to determine whether the file exists. You can refer to my <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=3463\">Lesson on the <em>access()<\/em> function<\/a> for more details.<\/p>\n<p>Here&#8217;s a sample run:<\/p>\n<p><code>Dictionary file is installed<\/code><\/p>\n<p>Once confirmed that the dictionary file is installed, you can access its contents in your C programs. Think of the fun you can have! I plunge into this topic starting with <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=6065\">next week&#8217;s Lesson<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Linux and Unix systems have access to a dictionary file chock full of words. <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=6054\">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":[2],"tags":[],"class_list":["post-6054","post","type-post","status-publish","format-standard","hentry","category-main"],"_links":{"self":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/6054","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=6054"}],"version-history":[{"count":6,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/6054\/revisions"}],"predecessor-version":[{"id":6075,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/6054\/revisions\/6075"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=6054"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=6054"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=6054"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}