Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Your recent update states that the file is really a database, in which you are looking for a word. This is very important.</p> <p>If you have enough memory to hold the whole database, you should do just that (read the whole database and arrange it for efficient searching), so you should probably <em>not</em> ask about searching in a file.</p> <p>Good database designs involve data structures like <a href="http://en.wikipedia.org/wiki/Trie" rel="nofollow">trie</a> and <a href="http://en.wikipedia.org/wiki/Hash_table" rel="nofollow">hash table</a>. But for a start, you could use the most basic improvement of the database - holding the words in alphabetical order (use the somewhat tricky <a href="http://en.cppreference.com/w/c/algorithm/qsort" rel="nofollow">qsort</a> function to achieve that).</p> <pre><code>struct Database { size_t count; struct Entry // not sure about C syntax here; I usually code in C++; sorry { char *word; char *explanation; } *entries; }; char *find_explanation_of_word(struct Database* db, char *word) { for (size_t i = 0; i &lt; db-&gt;count; i++) { int result = strcmp(db-&gt;entries[i].word, word); if (result == 0) return db-&gt;entries[i].explanation; else if (result &gt; 0) break; // if the database is sorted, this means word is not found } return NULL; // not found } </code></pre> <p>If your database is too big to hold in memory, you should use a trie that holds just the beginnings of the words in the database; for each beginning of a word, have a file offset at which to start scanning the file.</p> <pre><code>char* find_explanation_in_file(FILE *f, long offset, char *word) { fseek(f, offset, SEEK_SET); char line[100]; // 100 should be greater than max line in file while (line, sizeof(line), f) { char *word_in_file = strtok(line, " "); char *explanation = strtok(NULL, ""); int result = strcmp(word_in_file, word); if (result == 0) return explanation; else if (result &gt; 0) break; } return NULL; // not found } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload