I'm trying to move a dictionary into a group of bite-sized files based off of the length of the strings in each file (I'm ignoring strings of length 1 for obvious reasons). Since I don't know what the longest word is (and I'm not going to look for it), and I don't want redundant files, I decided to use a vector of output file streams that I would put all the words into, but I can't get the file to open.
I took a look at it and found the failbit is 1, but the badbit is 0, so apparently it's a logic error on opening the stream (http://www.cplusplus.com/reference/ios/ios/rdstate/). I looked online and decided to try using pointers, but that didn't help either, so now I'm asking what the problem might be, because I can't think of any reason why it isn't working.
void createDictionary()
{
//declare variables
string dictionaryName; //name of dictionary file
ifstream dictionaryIn; //file of original dictionary
vector<shared_ptr<ofstream>> dictionaryOut; //files where dictionary will be put based off of word length
string word; //input from the dictionary file
vector<int> lengths; //vector containing available lengths and their locations
unsignedlonglong counter = 0;
//get input
cout << "Enter full name of dictionary to use (include the extension): ";
getline(cin, dictionaryName);
//open dictionary
constchar *file = dictionaryName.c_str();
dictionaryIn.open(file);
if (!dictionaryIn.is_open())
{
cout << dictionaryName << " could not be opened. Please make sure your file name is correct and try again.\nReturning to main menu.\n";
return;
}
//set up dictionary
cout << dictionaryName << " opened successfully.\nPlease wait while I set up the dictionary for quick searching for anagrams.\n";
while (getline(dictionaryIn, word))
{
//loop variables
int length = word.length(); //length of the word (used to separate words in the dictionary based off of length)
int loc = locate(length, lengths); //location of the file containing words of length "length"
//single-length words do not have anagrams
if (length == 1)
continue;
if (loc == -1)
{
counter++;
//get file name
stringstream name;
name << length << ".txt";
constchar *dictionaryFile = name.str().c_str();
//add file stream to vector
//ofstream currentLength(dictionaryFile);
dictionaryOut.push_back(make_shared<ofstream>(dictionaryFile));
lengths.push_back(length);
//get location and check that file is open
loc = dictionaryOut.size() - 1;
//loc = locate(length, lengths);
//dictionaryOut[loc].open(dictionaryFile);
if (!dictionaryOut[loc]->is_open())
{
cout << dictionaryOut[loc]->fail() << dictionaryOut[loc]->bad() << endl;
cout << "Stopped on file " << counter << endl;
cout << "There was an error in setting up your dictionary. Please try again.\nReturning to main menu.\n";
return;
}
}
*dictionaryOut[loc].get() << word << endl;
}
for (int x = 0; x < dictionaryOut.size(); x++)
{
dictionaryOut[x]->close();
}
cout << "Dictionary set up successfully.\nReturning to main menu.\n";
}
So, I found the answer, and it was something weird that I wasn't used to. Back when I first started in C++, you could only open a file with a constchar*, so if you wanted to open it based off of a string you didn't know beforehand, you had to convert your string. A friend of mine said to try using a string directly instead of converting it, and it worked for some reason. Why? I don't know, but that's programming, I guess.