<p>Printing the <ahref="http://www.dailyfreecode.com/Code/word-list-reads-text-file-makes-2050.aspx"target="_blank">words contained in a file</a> named <code>file.txt</code> in alphabetical order and also their frequencies in to another file "wordcount.txt".
<divclass="center"><iframescrolling="no"frameborder="0"src="../../d4/d3c/words__alphabetical_8c__incl.svg"width="636"height="124"><p><b>This browser is not able to show SVG: try Firefox, Chrome, Safari, or Opera instead.</b></p></iframe>
<trclass="memdesc:"><tdclass="mdescLeft"> </td><tdclass="mdescRight"><aclass="el"href="../../db/d8b/struct_node.html"title="Node, the basic data structure of the tree.">Node</a>, the basic data structure of the tree. <ahref="../../db/d8b/struct_node.html#details">More...</a><br/></td></tr>
<trclass="memdesc:a5157a5b4e164f934602fa78413504029"><tdclass="mdescLeft"> </td><tdclass="mdescRight">Ends program due to an error. <br/></td></tr>
<trclass="memdesc:a840902ada0e956eb01b529be178748cc"><tdclass="mdescLeft"> </td><tdclass="mdescRight">Frees memory when program is terminating. <br/></td></tr>
<trclass="memdesc:af1f9b6eb320d872ba0766dc8bd070075"><tdclass="mdescLeft"> </td><tdclass="mdescRight">Closes the file after reading or writing. <br/></td></tr>
<trclass="memdesc:a1e5ad8401396df36ab585d79f3309fd9"><tdclass="mdescLeft"> </td><tdclass="mdescRight">Reserves memory for new node. <br/></td></tr>
<trclass="memdesc:a22516c70583c9fa78e4edffb063836cc"><tdclass="mdescLeft"> </td><tdclass="mdescRight">Writes contents of tree to another file alphabetically. <br/></td></tr>
<trclass="memdesc:a0ac11529daf94e98f7114a6e938679a9"><tdclass="mdescLeft"> </td><tdclass="mdescRight">Adds word (node) to the correct position in tree. <br/></td></tr>
<trclass="memdesc:a151b2c78ae821d0495faf936a1c2530f"><tdclass="mdescLeft"> </td><tdclass="mdescRight">Reads words from file to tree. <br/></td></tr>
<divclass="textblock"><p>Printing the <ahref="http://www.dailyfreecode.com/Code/word-list-reads-text-file-makes-2050.aspx"target="_blank">words contained in a file</a> named <code>file.txt</code> in alphabetical order and also their frequencies in to another file "wordcount.txt". </p>
<p>Given a file (<code>file.txt</code>) containing words (like a publication or a novel), where words are separated by a space, newline, or underscore. This program prints (writes or outputs) to another file (<code>wordcount.txt</code>), the individual words contained in 'file.txt' with their frequencies (number of occurrences) each on a newline and in alphabetical order. This program uses the binary tree data structure to accomplish this task. </p><dlclass="section author"><dt>Author</dt><dd><ahref="https://github.com/RandyKdev"target="_blank">Randy Kwalar</a></dd></dl>
<divclass="line"><spanclass="lineno"> 149</span><aclass="code hl_function"href="../../d3/d78/words__alphabetical_8c.html#a1e5ad8401396df36ab585d79f3309fd9">allocateMemoryForNode</a>(); <spanclass="comment">// allocates memory for new node</span></div>
<divclass="line"><spanclass="lineno"> 151</span> currentNode-><aclass="code hl_variable"href="../../db/d8b/struct_node.html#abe1c200da58f5276c6fd74f640a29a02">frequency</a> = 1; <spanclass="comment">// initializes the word frequency to 1</span></div>
<divclass="line"><spanclass="lineno"> 152</span> currentNode-><aclass="code hl_variable"href="../../db/d8b/struct_node.html#ad0976834843c7618677d22a10c495b36">left</a> = NULL; <spanclass="comment">// sets left node to `NULL`</span></div>
<divclass="line"><spanclass="lineno"> 153</span> currentNode-><aclass="code hl_variable"href="../../db/d8b/struct_node.html#af99e7102380da88d7c079fa264230cf4">right</a> = NULL; <spanclass="comment">// sets right node to `NULL`</span></div>
<divclass="line"><spanclass="lineno"> 154</span><spanclass="keywordflow">return</span> currentNode; <spanclass="comment">// returns pointer to newly created node</span></div>
<divclass="line"><spanclass="lineno"> 161</span> currentNode-><aclass="code hl_variable"href="../../db/d8b/struct_node.html#af99e7102380da88d7c079fa264230cf4">right</a>); <spanclass="comment">// adds `word` to right sub tree if `word` is</span></div>
<divclass="line"><spanclass="lineno"> 162</span><spanclass="comment">// alphabetically greater than `currentNode->word`</span></div>
<divclass="line"><spanclass="lineno"> 166</span> currentNode-><aclass="code hl_variable"href="../../db/d8b/struct_node.html#ad0976834843c7618677d22a10c495b36">left</a>); <spanclass="comment">// adds `word` to left sub tree if `word` is</span></div>
<divclass="line"><spanclass="lineno"> 167</span><spanclass="comment">// alphabetically less than `currentNode->word`</span></div>
<divclass="line"><spanclass="lineno"> 170</span> currentNode-><aclass="code hl_variable"href="../../db/d8b/struct_node.html#abe1c200da58f5276c6fd74f640a29a02">frequency</a>++; <spanclass="comment">// increments `currentNode` frequency if `word` is the same as `currentNode->word`</span></div>
<divclass="line"><spanclass="lineno"> 173</span><spanclass="keywordflow">return</span> currentNode; <spanclass="comment">// returns pointer to current node</span></div>
<divclass="ttc"id="astruct_node_html"><divclass="ttname"><ahref="../../db/d8b/struct_node.html">Node</a></div><divclass="ttdoc">Node, the basic data structure of the tree.</div><divclass="ttdef"><b>Definition:</b> threaded_binary_trees.c:27</div></div>
<divclass="ttc"id="astruct_node_html_abe1c200da58f5276c6fd74f640a29a02"><divclass="ttname"><ahref="../../db/d8b/struct_node.html#abe1c200da58f5276c6fd74f640a29a02">Node::frequency</a></div><divclass="ttdeci">uint64_t frequency</div><divclass="ttdoc">number of occurrences of the word</div><divclass="ttdef"><b>Definition:</b> words_alphabetical.c:31</div></div>
<divclass="ttc"id="astruct_node_html_ad0976834843c7618677d22a10c495b36"><divclass="ttname"><ahref="../../db/d8b/struct_node.html#ad0976834843c7618677d22a10c495b36">Node::left</a></div><divclass="ttdeci">struct Node * left</div><divclass="ttdoc">pointer to the left child node</div><divclass="ttdef"><b>Definition:</b> words_alphabetical.c:32</div></div>
<divclass="ttc"id="astruct_node_html_af99e7102380da88d7c079fa264230cf4"><divclass="ttname"><ahref="../../db/d8b/struct_node.html#af99e7102380da88d7c079fa264230cf4">Node::right</a></div><divclass="ttdeci">struct Node * right</div><divclass="ttdoc">pointer to the right child node</div><divclass="ttdef"><b>Definition:</b> words_alphabetical.c:33</div></div>
<divclass="ttc"id="astruct_node_html_afa6ccc6414d3e0bcacb715dcdf764bca"><divclass="ttname"><ahref="../../db/d8b/struct_node.html#afa6ccc6414d3e0bcacb715dcdf764bca">Node::word</a></div><divclass="ttdeci">char * word</div><divclass="ttdoc">the word (value) of the node</div><divclass="ttdef"><b>Definition:</b> words_alphabetical.c:30</div></div>
<divclass="ttc"id="awords__alphabetical_8c_html_a0ac11529daf94e98f7114a6e938679a9"><divclass="ttname"><ahref="../../d3/d78/words__alphabetical_8c.html#a0ac11529daf94e98f7114a6e938679a9">addWordToTree</a></div><divclass="ttdeci">struct Node * addWordToTree(char *word, struct Node *currentNode)</div><divclass="ttdoc">Adds word (node) to the correct position in tree.</div><divclass="ttdef"><b>Definition:</b> words_alphabetical.c:144</div></div>
<divclass="ttc"id="awords__alphabetical_8c_html_a1e5ad8401396df36ab585d79f3309fd9"><divclass="ttname"><ahref="../../d3/d78/words__alphabetical_8c.html#a1e5ad8401396df36ab585d79f3309fd9">allocateMemoryForNode</a></div><divclass="ttdeci">struct Node * allocateMemoryForNode()</div><divclass="ttdoc">Reserves memory for new node.</div><divclass="ttdef"><b>Definition:</b> words_alphabetical.c:102</div></div>
<divclass="ttc"id="awords__alphabetical_8c_html_a9e3cab68cf6557024f0073f674a307a1"><divclass="ttname"><ahref="../../d3/d78/words__alphabetical_8c.html#a9e3cab68cf6557024f0073f674a307a1">getPointerToWord</a></div><divclass="ttdeci">char * getPointerToWord(char *word)</div><divclass="ttdoc">Stores word in memory.</div><divclass="ttdef"><b>Definition:</b> words_alphabetical.c:70</div></div>
<divclass="center"><iframescrolling="no"frameborder="0"src="../../d3/d78/words__alphabetical_8c_a0ac11529daf94e98f7114a6e938679a9_cgraph.svg"width="524"height="86"><p><b>This browser is not able to show SVG: try Firefox, Chrome, Safari, or Opera instead.</b></p></iframe>
<divclass="line"><spanclass="lineno"> 111</span><spanclass="stringliteral">"\nA problem occurred while reserving memory for the structure\n"</span>);</div>
<divclass="ttc"id="amalloc__dbg_8h_html_a725f50ecaf1959d96de79b36b4788fee"><divclass="ttname"><ahref="../../d2/ddd/malloc__dbg_8h.html#a725f50ecaf1959d96de79b36b4788fee">malloc</a></div><divclass="ttdeci">#define malloc(bytes)</div><divclass="ttdoc">This macro replace the standard malloc function with malloc_dbg.</div><divclass="ttdef"><b>Definition:</b> malloc_dbg.h:18</div></div>
<divclass="ttc"id="astructnode_html"><divclass="ttname"><ahref="../../d5/da1/structnode.html">node</a></div><divclass="ttdoc">Node, the basic data structure in the tree.</div><divclass="ttdef"><b>Definition:</b> binary_search_tree.c:15</div></div>
<divclass="ttc"id="awords__alphabetical_8c_html_a5157a5b4e164f934602fa78413504029"><divclass="ttname"><ahref="../../d3/d78/words__alphabetical_8c.html#a5157a5b4e164f934602fa78413504029">endProgramAbruptly</a></div><divclass="ttdeci">void endProgramAbruptly(char *errorMessage)</div><divclass="ttdoc">Ends program due to an error.</div><divclass="ttdef"><b>Definition:</b> words_alphabetical.c:41</div></div>
<divclass="center"><iframescrolling="no"frameborder="0"src="../../d3/d78/words__alphabetical_8c_a1e5ad8401396df36ab585d79f3309fd9_cgraph.svg"width="363"height="36"><p><b>This browser is not able to show SVG: try Firefox, Chrome, Safari, or Opera instead.</b></p></iframe>
<divclass="line"><spanclass="lineno"> 93</span><aclass="code hl_function"href="../../d3/d78/words__alphabetical_8c.html#a5157a5b4e164f934602fa78413504029">endProgramAbruptly</a>(<spanclass="stringliteral">"\nA Problem Occurred while closing a file\n"</span>);</div>
<divclass="center"><iframescrolling="no"frameborder="0"src="../../d3/d78/words__alphabetical_8c_af1f9b6eb320d872ba0766dc8bd070075_cgraph.svg"width="274"height="36"><p><b>This browser is not able to show SVG: try Firefox, Chrome, Safari, or Opera instead.</b></p></iframe>
<divclass="line"><spanclass="lineno"> 58</span><aclass="code hl_define"href="../../d2/ddd/malloc__dbg_8h.html#a9cc854374299a1dd933bf62029761768">free</a>(<aclass="code hl_struct"href="../../d5/da1/structnode.html">node</a>->word); <spanclass="comment">// freeing node->word because memory was allocated</span></div>
<divclass="line"><spanclass="lineno"> 59</span><spanclass="comment">// using malloc</span></div>
<divclass="line"><spanclass="lineno"> 60</span><aclass="code hl_define"href="../../d2/ddd/malloc__dbg_8h.html#a9cc854374299a1dd933bf62029761768">free</a>(<aclass="code hl_struct"href="../../d5/da1/structnode.html">node</a>); <spanclass="comment">// freeing node because memory was allocated using malloc</span></div>
<divclass="ttc"id="amalloc__dbg_8h_html_a9cc854374299a1dd933bf62029761768"><divclass="ttname"><ahref="../../d2/ddd/malloc__dbg_8h.html#a9cc854374299a1dd933bf62029761768">free</a></div><divclass="ttdeci">#define free(ptr)</div><divclass="ttdoc">This macro replace the standard free function with free_dbg.</div><divclass="ttdef"><b>Definition:</b> malloc_dbg.h:26</div></div>
<divclass="ttc"id="awords__alphabetical_8c_html_a840902ada0e956eb01b529be178748cc"><divclass="ttname"><ahref="../../d3/d78/words__alphabetical_8c.html#a840902ada0e956eb01b529be178748cc">freeTreeMemory</a></div><divclass="ttdeci">void freeTreeMemory(struct Node *node)</div><divclass="ttdoc">Frees memory when program is terminating.</div><divclass="ttdef"><b>Definition:</b> words_alphabetical.c:52</div></div>
<divclass="center"><iframescrolling="no"frameborder="0"src="../../d3/d78/words__alphabetical_8c_a840902ada0e956eb01b529be178748cc_cgraph.svg"width="128"height="60"><p><b>This browser is not able to show SVG: try Firefox, Chrome, Safari, or Opera instead.</b></p></iframe>
<divclass="line"><spanclass="lineno"> 81</span><spanclass="stringliteral">"\nA problem occurred while reserving memory for the word\n"</span>);</div>
<divclass="center"><iframescrolling="no"frameborder="0"src="../../d3/d78/words__alphabetical_8c_a9e3cab68cf6557024f0073f674a307a1_cgraph.svg"width="324"height="36"><p><b>This browser is not able to show SVG: try Firefox, Chrome, Safari, or Opera instead.</b></p></iframe>
<divclass="center"><divclass="zoom"><iframescrolling="no"frameborder="0"src="../../d3/d78/words__alphabetical_8c_ae66f6b31b5ad750f1fe042a706a4e3d4_cgraph.svg"width="100%"height="472"><p><b>This browser is not able to show SVG: try Firefox, Chrome, Safari, or Opera instead.</b></p></iframe></div>
<divclass="line"><spanclass="lineno"> 189</span><spanclass="keywordtype">char</span> inputChar; <spanclass="comment">///< temp storage of characters</span></div>
<divclass="line"><spanclass="lineno"> 190</span><spanclass="keywordtype">bool</span> isPrevCharAlpha = <spanclass="keyword">false</span>; <spanclass="comment">///< bool to mark the end of a word</span></div>
<divclass="line"><spanclass="lineno"> 191</span> uint8_t pos = 0; <spanclass="comment">///< position in inputString to place the inputChar</span></div>
<divclass="line"><spanclass="lineno"> 205</span><spanclass="comment">// checks if character is ' or - and if it is preceded by a letter eg</span></div>
<divclass="line"><spanclass="lineno"> 217</span><spanclass="comment">// if last character is not letter and is not ' then replace by \0</span></div>
<divclass="line"><spanclass="lineno"> 226</span><spanclass="comment">// this is to catch the case for the EOF being immediately after the last</span></div>
<divclass="line"><spanclass="lineno"> 227</span><spanclass="comment">// letter or '</span></div>
<divclass="center"><divclass="zoom"><iframescrolling="no"frameborder="0"src="../../d3/d78/words__alphabetical_8c_a151b2c78ae821d0495faf936a1c2530f_cgraph.svg"width="100%"height="364"><p><b>This browser is not able to show SVG: try Firefox, Chrome, Safari, or Opera instead.</b></p></iframe></div>
<divclass="line"><spanclass="lineno"> 252</span><spanclass="stringliteral">"hey_this, is a. test input \n to a_file"</span>); <spanclass="comment">// writes test data to</span></div>
<divclass="line"><spanclass="lineno"> 253</span><spanclass="comment">// test file</span></div>
<divclass="line"><spanclass="lineno"> 261</span><spanclass="comment">// Tests to check if words were added to correct position in tree and also</span></div>
<divclass="line"><spanclass="lineno"> 262</span><spanclass="comment">// if their frequencies were added correctly</span></div>
<divclass="line"><spanclass="lineno"> 271</span><aclass="code hl_function"href="../../d3/d78/words__alphabetical_8c.html#af1f9b6eb320d872ba0766dc8bd070075">closeFile</a>(file); <spanclass="comment">// closes test file</span></div>
<divclass="line"><spanclass="lineno"> 272</span> remove(<spanclass="stringliteral">"file.txt"</span>); <spanclass="comment">// deletes test file from storage</span></div>
<divclass="line"><spanclass="lineno"> 276</span><spanclass="stringliteral">"WORD"</span>); <spanclass="comment">// prints the heading to `wordcount.txt`</span></div>
<divclass="line"><spanclass="lineno"> 293</span> int16_t inputChar; <spanclass="comment">// holds the current character in `wordcount.txt`</span></div>
<divclass="line"><spanclass="lineno"> 294</span> uint64_t i = 0; <spanclass="comment">// holds the current index in `correctString`</span></div>
<divclass="line"><spanclass="lineno"> 296</span><spanclass="comment">// Checks if the content in `wordcount.txt` is as expected (the same as in</span></div>
<divclass="line"><spanclass="lineno"> 305</span><aclass="code hl_function"href="../../d3/d78/words__alphabetical_8c.html#a840902ada0e956eb01b529be178748cc">freeTreeMemory</a>(root); <spanclass="comment">// frees memory taken up by the tree</span></div>
<divclass="ttc"id="awords__alphabetical_8c_html_a151b2c78ae821d0495faf936a1c2530f"><divclass="ttname"><ahref="../../d3/d78/words__alphabetical_8c.html#a151b2c78ae821d0495faf936a1c2530f">readWordsInFileToTree</a></div><divclass="ttdeci">struct Node * readWordsInFileToTree(FILE *file, struct Node *root)</div><divclass="ttdoc">Reads words from file to tree.</div><divclass="ttdef"><b>Definition:</b> words_alphabetical.c:182</div></div>
<divclass="ttc"id="awords__alphabetical_8c_html_a22516c70583c9fa78e4edffb063836cc"><divclass="ttname"><ahref="../../d3/d78/words__alphabetical_8c.html#a22516c70583c9fa78e4edffb063836cc">writeContentOfTreeToFile</a></div><divclass="ttdeci">void writeContentOfTreeToFile(struct Node *node, FILE *file)</div><divclass="ttdoc">Writes contents of tree to another file alphabetically.</div><divclass="ttdef"><b>Definition:</b> words_alphabetical.c:121</div></div>
<divclass="ttc"id="awords__alphabetical_8c_html_af1f9b6eb320d872ba0766dc8bd070075"><divclass="ttname"><ahref="../../d3/d78/words__alphabetical_8c.html#af1f9b6eb320d872ba0766dc8bd070075">closeFile</a></div><divclass="ttdeci">void closeFile(FILE *file)</div><divclass="ttdoc">Closes the file after reading or writing.</div><divclass="ttdef"><b>Definition:</b> words_alphabetical.c:90</div></div>
<divclass="center"><divclass="zoom"><iframescrolling="no"frameborder="0"src="../../d3/d78/words__alphabetical_8c_aa8dca7b867074164d5f45b0f3851269d_cgraph.svg"width="100%"height="472"><p><b>This browser is not able to show SVG: try Firefox, Chrome, Safari, or Opera instead.</b></p></iframe></div>
<divclass="line"><spanclass="lineno"> 123</span><spanclass="keyword">static</span> uint64_t i = 1; <spanclass="comment">///< for word numbering in the write file</span></div>
<divclass="line"><spanclass="lineno"> 124</span><spanclass="keywordflow">if</span> (<aclass="code hl_struct"href="../../d5/da1/structnode.html">node</a> != NULL) <spanclass="comment">// checks if the node is valid</span></div>
<divclass="line"><spanclass="lineno"> 130</span><aclass="code hl_struct"href="../../d5/da1/structnode.html">node</a>->word); <spanclass="comment">// prints the word number, word frequency and word</span></div>
<divclass="line"><spanclass="lineno"> 131</span><spanclass="comment">// in tabular format to the file</span></div>
<divclass="center"><iframescrolling="no"frameborder="0"src="../../d3/d78/words__alphabetical_8c_a22516c70583c9fa78e4edffb063836cc_cgraph.svg"width="182"height="60"><p><b>This browser is not able to show SVG: try Firefox, Chrome, Safari, or Opera instead.</b></p></iframe>
<liclass="footer">Generated by <ahref="https://www.doxygen.org/index.html"><imgclass="footer"src="../../doxygen.svg"width="104"height="31"alt="doxygen"/></a> 1.9.6 </li>