Is there a way to convert from const char * to char * without casting away the const? I was warned that it could be dangerous and cause crashes (which I would agree with, since when I ran my program with const_cast, it caused a segmentation fault). Alternatively, is there a way to convert from string to char * (not convert from string to const char *, as c_str() does)? Any suggestions would be greatly appreciated.
// Read the wordlist file
ifstream wordlist("./samplewordlist.txt");
Dictionary htl;
if(wordlist)
{
cout << "The wordlist file was opened." << endl;
string line;
wordlist.ignore(7, '\n'); // ignore the first entry (the number of words in the wordlist)
while(getline(wordlist, line, '\n'))
{
cout << "Line = " << line << endl;
// convert from string to char *
const char * constConvline = line.c_str();
// convert from const char * to char *
char * convline;
convline = const_cast<char *>(constConvline);
htl.add(convline);
cout << "added " << convline << endl;
}
}
else if(!wordlist)
{
cerr << "The wordlist could not be read." << endl;
exit(2);
}