title: Read Line From File Until Newline and Store it in Char Array
body: <p>I am trying to write a program that reads one line from a file and stores it into a char array. Since I only want to read one line, I want it to stop reading at the newline. My current code doesn't achieve exactly what I want it to do. From a file that contains:</p>
  <pre><code>Hello
  u 
  </code></pre>
  <p>I want to store <code>Hello</code> in a char array. My current code only stores <code>llo</code> and some other garbage. This is what I have so far:</p>
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  
  int main()
  {
  
      char random[100];
  
      FILE *fp;
      fp = fopen(&quot;testing.txt&quot;, &quot;r&quot;);
          if(fp == NULL){
              printf(&quot;Can't open the file\n&quot;);
              exit(1);
      }
  
      char c;
  
  while((c = fgetc(fp)) != EOF){
      for(int i = 0; c != '\n'; i++){
          fscanf(fp, &quot;%c&quot;, &amp;c);
          random[i] = c;
      }
  }
  
  printf(&quot;%s\n&quot;, random);
  
      return 0;
  }
  </code></pre>