Read from file to string
<p>Well, firstly, I know I'm using stdio.h to read a file with C++, but please disregard it, I need to do it this way.</p>

<p>I have this file:</p>

<pre><code>5
peter
josh
edward
mary
lois
</code></pre>

<p>And I need to read the first number (easy):</p>

<pre><code>int np;

FILE *myfile = fopen("myfile.txt", "r");
fscanf(myfile, "%d", &amp;np);
</code></pre>

<p>And then I need to read the following np names:</p>

<pre><code>string people[np];
for (int i = 0; i &lt; np; i++) {
  fscanf(myfile, "%s", &amp;people[i]);
  fscanf(myfile, "\n");
}
</code></pre>

<p>However, I'm getting a SEGFAULT. When I used gdb, I got this:</p>

<pre><code>Program received signal SIGSEGV, Segmentation fault.
0x00007ffff7b6e603 in std::basic_ostream&lt;char, std::char_traits&lt;char&gt; &gt;&amp; std::operator&lt;&lt; &lt;char, std::char_traits&lt;char&gt;, std::allocator&lt;char&gt; &gt;(std::basic_ostream&lt;char, std::char_traits&lt;char&gt; &gt;&amp;, std::basic_string&lt;char, std::char_traits&lt;char&gt;, std::allocator&lt;char&gt; &gt; const&amp;) () from /usr/lib/libstdc++.so.6
</code></pre>

<p>In my opinion, I think it's because I have an array of strings, and I'm read char*, how can I save the lines as strings using stdio.h file reading commands?</p>
