bool new_database=true;
int dbno(1);
ostringstream title;
string sTitle;
title << "data" << dbno << ".database" << endl;
sTitle=title.str();
fstream test(sTitle.c_str());
if(!test.is_open())
{new_database=true;}
else
{new_database=false;}
test.close();
ofstream out((title.str()).c_str());
if(out.is_open()){cout << "File is open" << endl;}
if(new_database==true)
{
out << "#" << endl;
out << subject << endl;
out << "#" << endl;
}
out << "#" << endl;
out << id << endl;
for(int i=0;i<years;++i)
{
out << "Year " << i+1 << endl;
for(int j=0;j<(int)courses[i].size();++j)
{
out << courses[i][j].first << " "<< courses[i][i].second << "%" << endl;
}
}
title.str("");
out.close();
Essentially all I want to happen is the program checks if data1.database exists by trying to open it with the fstream and then creates it using the ofstream. However the program isn't creating the .database file even though I've passed a character array to the ofstream parameter list.
As far as I can tell, you never assign a string to title. So when you pass title in the constructor of out, it fails to open the file as title isn't holding a string.
If you've made changes to your code, it would be helpful if you update it.
however it doesn't even work if I pass title.str() to sTitle first.
That's because title was never assigned a character sequence. Then, on line 6, sTitle takes the character sequence of title. However, at this point, title isn't holding a character sequence and therefore, sTitle equals nothing.
On line 9, you attempt to open a file with sTitle, which is holding nothing.