title: Am trying to read files from a folder and treat them and output them in a text file but it doesn't output anything
body: <p>my treatment of the files include checking whether they are characters or not, to lower capital letters to small letters, stem the words to their root, and calculate TF and IDF and the weight of the terms too and as a result I try to output the resulted text in a normal text document</p>
  <p>This the stem library I used <a href="https://drive.google.com/file/d/1ZOCZ3yJqiVC3GP2WzDZ5j0oJV-YMIHwL/view?usp=sharing" rel="nofollow noreferrer">https://drive.google.com/file/d/1ZOCZ3yJqiVC3GP2WzDZ5j0oJV-YMIHwL/view?usp=sharing</a></p>
  <p>(the files I used are from 20newsgroup like 5 of them as a test)</p>
  <p>but when it builds it it shows nothing on the command window and text document</p>
  <p>I would like to know where is the problem as I tried to debug it but with no success of finding the error</p>
  <p>suggestions to improve the code are most welcome too</p>
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;sys/types.h&gt;
  #include &lt;dirent.h&gt; ///to access files folder
  #include &quot;stem.h&quot;   ///stemming library
  
  typedef struct
  {
      char text[100];
      int nbrofwords;
      struct string *next;
  } String;
  
  char *stopwords[39] = {&quot;I&quot;, &quot;a&quot;, &quot;about&quot;, &quot;an&quot;, &quot;are&quot;, &quot;as&quot;, &quot;at&quot;, &quot;be&quot;, &quot;by&quot;, &quot;do&quot;, &quot;in&quot;, &quot;for&quot;, &quot;from&quot;, &quot;how&quot;, &quot;they&quot;, &quot;have&quot;, &quot;is&quot;, &quot;it&quot;, &quot;all&quot;, &quot;of&quot;, &quot;on&quot;, &quot;or&quot;, &quot;that&quot;, &quot;the&quot;, &quot;this&quot;, &quot;to&quot;, &quot;was&quot;, &quot;what&quot;, &quot;when&quot;, &quot;where&quot;, &quot;who&quot;, &quot;will&quot;, &quot;with&quot;, &quot;and&quot;, &quot;the&quot;, &quot;com&quot;, &quot;www&quot;, &quot;org&quot;, &quot;net&quot;};
  char *smallletter[26] = {&quot;a&quot;, &quot;b&quot;, &quot;c&quot;, &quot;d&quot;, &quot;e&quot;, &quot;f&quot;, &quot;j&quot;, &quot;h&quot;, &quot;i&quot;, &quot;g&quot;, &quot;k&quot;, &quot;l&quot;, &quot;m&quot;, &quot;n&quot;, &quot;o&quot;, &quot;p&quot;, &quot;q&quot;, &quot;r&quot;, &quot;s&quot;, &quot;t&quot;, &quot;u&quot;, &quot;v&quot;, &quot;w&quot;, &quot;x&quot;, &quot;y&quot;, &quot;z&quot;};
  char *Capitalletter[26] = {&quot;A&quot;, &quot;B&quot;, &quot;C&quot;, &quot;D&quot;, &quot;E&quot;, &quot;F&quot;, &quot;J&quot;, &quot;H&quot;, &quot;I&quot;, &quot;G&quot;, &quot;K&quot;, &quot;L&quot;, &quot;M&quot;, &quot;N&quot;, &quot;O&quot;, &quot;P&quot;, &quot;Q&quot;, &quot;R&quot;, &quot;S&quot;, &quot;T&quot;, &quot;U&quot;, &quot;V&quot;, &quot;W&quot;, &quot;X&quot;, &quot;Y&quot;, &quot;Z&quot;};
  FILE *filef;
  String *words;
  long filenb;
  
  void addterms(char word[100])
  {
      int test = 0;
      String *p, *new;
      p = words;
      new = malloc(sizeof(String));
      strcpy(new-&gt;text, word);
      new-&gt;nbrofwords = 1;
      new-&gt;next = NULL;
      if (!words)
          words = new;
      else
      {
          while (p-&gt;next != NULL)
          {
              if (strcmp(p-&gt;text, word) == 0)
              {
                  test = 1;
                  p-&gt;nbrofwords += 1;
              }
              p = p-&gt;next;
          }
          if (test == 0)
              p-&gt;next = new;
      }
  }
  
  /// test if it's a character or not
  int isCharacter(char c)
  {
      int i;
      for (i = 0; i &lt; 26; i++)
      {
          if (c == smallletter[i] || c == Capitalletter[i])
          {
              return 1;
          }
      }
      return 0;
  }
  
  /// test if it's a stop word or not
  int findstopwords(char *text)
  {
      int i;
      for (i = 0; i &lt; 39; i++)
      {
          if (strcmp(text, stopwords[i]) == 0)
              return 1;
      }
      return 0;
  }
  
  /// empty arrays to read other files and words
  void emptyarray(char array[100])
  {
      int i;
      for (i = 0; i &lt; 100; i++)
          array[i] = '\0';
  }
  
  /// return words to their roots and adds them to the outputted file
  void addtofile(FILE *file_s)
  {
      char c;
      int i = 0;
      int j, k;
      char text[100];
      c = fgetc(file_s);
      do
      {
          if (isCharacter(c) == 1) /// lower Capital letters to small letters
          {
              if (c &lt; 97)
                  c += 32;
              text[i] = c;
              i++;
          }
          else
          {
              text[i] = '\0';
              if (findstopwords(text) == 0 &amp;&amp; text[0] != '\0')
              {
                  k = stem(text, 0, strlen(text) - 1);
                  if (k != (strlen(text) - 1))
                      text[k] = '\0';
                  if (findstopwords(text) == 0 &amp;&amp; text[0] != '\0')
                  {
                      fputs(text, filef);
                      fputc(' ', filef);
                      addterms(text);
                  }
              }
              emptyarray(text);
              i = 0;
          }
          c = fgetc(file_s);
      } while (c != EOF);
      fputc('\n', filef);
  }
  
  /// open the file to read the documents
  void openFile(char *filename)
  {
      char text[10000] = &quot;files/&quot;;
      FILE *f;
      int i;
      j = strlen(text);
      for (i = 0; i &lt; strlen(filename); i++)
          text[i + 11] = filename[i];
      text[strlen(filename) + 11] = '\0';
      f = fopen(text, &quot;r+&quot;);
      if (f)
      {
          addtofile(f);
      }
      filenb++;
      fclose(f);
  }
  
  /// measure the weight and tf and idf to find the
  float MeasureWeight(int nbwordf, int nbofW, int nbF)
  {
      float weight_measured;
      float TF, DF, IDF, nbword_infiles, nb_words_tot, nbfw_term, nbof_files;
      nbword_infiles = nbwordf;           /// number of a term in file
      nb_words_tot = nbofW;               /// number of terms in file
      nbfw_term = nbF;                    /// number of files with the term
      nbof_files = filenb;                /// number of files
      TF = nbword_infiles / nb_words_tot; /// TermFrequency=number of times term appear in file/doc divided by number of terms in file/doc
      DF = nbfw_term;                     /// number of files with term
      IDF = nbof_files / DF;              /// InverseDocFrequency(calculate the importance of a term) = number of files/docs divided by number of files/docs with the term
      weight_measured = TF * IDF;
      return weight_measured;
  }
  
  void Display(char *word, int nb)
  {
      int frequent[2][filenb];
      int nbF = 0;
      int i, k, j = 0;
      for (i = 0; i &lt; filenb; i++)
      {
          frequent[0][i] = 0;
          frequent[1][i] = 0;
      }
      FILE *f = fopen(&quot;C:\Users\AMANIB\Desktop\ri\IR\output.txt&quot;, &quot;r+&quot;);
      char c;
      char text[100];
      int count = 0;
      do
      {
          c = fgetc(f);
          if (isCharacter(c) == 1)
          {
              text[i] = c;
              i++;
          }
          else
          {
              text[i] = '\0';
              if (strcmp(word, text) == 0)
                  count++;
              i = 0;
              emptyarray(text);
              if (c == '\n')
              {
                  frequent[0][j] = count;
                  frequent[1][j] = j;
                  j++;
                  count = 0;
              }
          }
      } while (c != EOF);
      for (i = 0; i &lt; filenb; i++)
      {
          if (frequent[0][i] != 0)
          {
              nbF++;
          }
      }
      float weight;
      printf(&quot;%s &quot;, word);
      for (i = 0; i &lt; filenb; i++)
      {
          if (frequent[0][i] != 0)
          {
              weight = MeasureWeight(frequent[0][i], nb, nbF);
              printf(&quot;\n \n filenb%d = %d times | weight = %f \n \n \n&quot;, frequent[1][i] + 1, frequent[0][i], weight);
          }
      }
  }
  
  int main()
  {
      int i, j, k;
      char c;
      String *p;
      char word[50];
      filenb = 0;
      filef = fopen(&quot;C:\Users\AMANIB\Desktop\ri\IR\output.txt&quot;, &quot;r+&quot;);
      struct dirent *read;
      DIR *files;
      files = opendir(&quot;files&quot;);
      char *text;
      FILE *f;
      while ((read = readdir(files)))
      {
          if ((strcmp(read-&gt;doc_name, &quot;.&quot;) == 0) || (strcmp(read-&gt;doc_name, &quot;..&quot;) == 0))
              continue;
          openFile(read-&gt;doc_name);
      }
  
      closedir(files);
      fclose(filef);
      p = words;
      while (p)
      {
          Display(p-&gt;text, p-&gt;nbrofwords);
          p = p-&gt;next;
      }
      return 0;
  }
  </code></pre>