title: How can I detect unterminated comment and write an error message saying "Error: line X: unterminated comment" to the standard error stream in my code?
body: <pre><code>#include &lt;stdio.h&gt;
  #include &lt;stdbool.h&gt;
  
  void m_cmnt(FILE *fp) {
      int prev;
      int ch;
      while ((ch = getc(fp)) != EOF) {
          if (prev == '*' &amp;&amp; ch == '/') {
              return;
          } else
              prev = ch;
      }
  }
  
  int main(int c, char **arr) {
      FILE *fp, *np;
      int ch, prev;
      bool String = 0;
  
      fp = fopen("test.txt", "r");
      np = fopen("temp.txt", "w");
  
      if (fp == NULL) {
          printf("Invalid/No Filename given as Argument ! \n");
          return 1;
      }
      while ((ch = getc(fp)) != EOF) {
          if (!String) {
              if (ch == '/') {
                  prev = ch;
                  ch = getc(fp);
                  switch (ch) {
                  case '*':
                      /* if(ch != 'a') putc('h', np); */
                      m_cmnt(fp);
                      putc(' ', np);
                      break;
                  default:
                      putc(prev, np);
                      putc(ch, np);
                      break;
                  }
              } else
                  putc(ch, np);
          } else
              putc(ch, np);
  
          if (ch == '\"' || ch == '\'')
              String = !String;
          prev = ch;
      }
  
      fclose(fp);
      fclose(np);
      remove(arr[1]);
      //rename("temp.txt", arr[1]);
      return 0;
  }
  </code></pre>
  
  <p>This is a simple de-commenting C program (which is C pre-processor job). I was struggling adding a feature to write an error message when detecting unterminated comment (<code>/* example</code>) to the standard error stream. The error should say something like <code>Error: line X: unterminated comment</code> where X is the line number the error occurred. I have been trying this for days now and I can't make any progress and I am highly frustrated. So please someone help me with simple and to the point answer.</p>
  
  <pre><code>test.txt 
  
  hello\nworld
  Me/*some\ncomment*/again
  </code></pre>
  
  <p>The result of test.txt after the program run should be like</p>
  
  <pre><code>hello
  world
  me
  again
  </code></pre>
  
  <p>Both of them are in separate line because \n is present in each case. But what I am getting right now is </p>
  
  <pre><code>hello\nworld
  Me again
  </code></pre>