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.
I have this file:
5
peter
josh
edward
mary
lois
And I need to read the first number (easy):
int np;
FILE *myfile = fopen("myfile.txt", "r");
fscanf(myfile, "%d", &np);
And then I need to read the following np names:
string people[np];
for (int i = 0; i < np; i++) {
fscanf(myfile, "%s", &people[i]);
fscanf(myfile, "\n");
}
However, I'm getting a SEGFAULT. When I used gdb, I got this:
Program received signal SIGSEGV, Segmentation fault.
0x00007ffff7b6e603 in std::basic_ostream<char, std::char_traits<char> >& std::operator<< <char, std::char_traits<char>, std::allocator<char> >(std::basic_ostream<char, std::char_traits<char> >&, std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&) () from /usr/lib/libstdc++.so.6
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?