My aim is to fill an array of strings with each word in a dictionary file (/usr/share/dict/words). I'm able to iterate through each line in the file using an ifstream to determine the number of elements I will need in my string array. That being said, I am unclear how to instantiate and then fill my array of strings? I believe there is some casting I am missing, but nevertheless my code is below. Note the error occurs on line 40:
error: cannot convert 'std::string' to 'char*' for argument '1' to 'char* strncpy(char*, const char*, size_t)'
#include <iostream>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <fstream>
#include <sstream>
using namespace std;
const int MAX_WORD_LENGTH = 40;
int main(int argc, char *argv[]){
char c;
int numLines = 0;
string line;
ifstream dictionary("/usr/share/dict/words");
if (dictionary==0){ //exits program if dictionary file wasn't opened correctly
fprintf(stderr, "failed to open /usr/share/dict/words\n");
exit(1);
}
while (dictionary.get(c)){ //counts number of lines and stores them in int numLines
if (c=='\n'){
numLines++;
}
}
//char *dictArray[numLines][MAX_WORD_LENGTH];
string dictArray[numLines];
printf("%d\n", numLines); //debugging
for(int i = 0; i < numLines; i++){ //loop to fill string array with each word in dictionary file
getline(dictionary, line);
strncpy(dictArray[i], line.c_str(), 40);
}
}
dictArray[i] = line;