title: Exporting 2d array into new file using C
body: <p>I have been working on an assignment that requires me to read a file containing a list of names into a 2D array, and then to write the array to a new file. I am able to read the file into an array, and I am able to create a new file and export the names, but unfortunately it is not writing the names onto new lines and instead only writing the names over one another.</p>
  <p>For example, I have the following names in names.txt:</p>
  <blockquote>
  <p>Sean
  Joe
  Steve
  Taka
  Kathy</p>
  </blockquote>
  <p>However, in the new file it is only showing Kathy. How would I go about making sure that each name in the array is array is printed on a new line?</p>
  <p>Here is the code that I have thus far:</p>
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;string.h&gt;
  #include &lt;stdlib.h&gt;
  
  int main(void)
  {
      FILE* fp;
      FILE* fp2;
      char nameArray[20][120], str[100];
      int i = 0, j = 0;
      char name[20];
  
      printf(&quot;Please enter a file name: &quot;);
      scanf(&quot;%s&quot;, &amp;name);
  
      fp = fopen(name, &quot;r&quot;);
  
  
      while (fscanf(fp, &quot;%s&quot;, str) != EOF)
      {
          strcpy(nameArray[i], str);
          i++;
      }
  
      for (i = 0; i &lt; 5; i++)
      {
          fp2 = fopen(&quot;newNames.txt&quot;, &quot;wb&quot;);
          fwrite(nameArray[i], sizeof(char), 5, fp2);
      }
      size_t n;
      for (n = 0; n &lt; 5; n++) {
          printf(&quot;%s\n&quot;, nameArray[n]);
      }
      fclose(fp);
  }
  </code></pre>