File I/O not working correctly
my WriteEntries function is not working... this snippet of code creates the file properly, but when i want to write the strings to the file... it does not work... anyone know why?
Code:
/*********************/
/**** MCreateFile ****/
/*********************/
void MCreateFile()
{
Phonebook pbEntry;
string filename;
string directory = "Data/";
string dirfile;
CLEARSCREEN;
cout<<"\n\t\t\tPlease enter the filename: ";
cin>>filename;
filename.append(".txt");
dirfile=directory+filename;
ifstream testFile;
testFile.open(dirfile.c_str(), ios::in);
fstream File(dirfile.c_str(), ios::out);
if(testFile.good())
{
testFile.close();
int samefile;
cout<<"\n "<<setfill('=')<<setw(70)<<"=";
cout<<"\n | There is a file at the current save location with the same name... |";
cout<<"\n "<<setfill('=')<<setw(70)<<"="<<endl;
cout<<"\n\t\t\t <1>Overwrite";
cout<<"\n\t\t\t <2>Rename Current File";
cout<<"\n\t\t\t <3>Quit";
cout<<"\n\t\t\t ->: ";
cin>>samefile;
switch(samefile)
{
case 1:
File.open(dirfile.c_str(), ios::out|ios::trunc);
File.close();
break;
case 2:
filename.clear();
cout<<"\n\t\t\tNew File Name: ";
cin>>filename;
filename.append(".txt");
break;
case 3:
return;
break;
default:
cout<<"\n\t\t\tNot a valid entry!";
cout<<"\n\t\t\t ->: ";
}
}
dirfile=directory+filename;
File.open(dirfile.c_str(), ios::out);
if(File.bad())
{
cout<<"\t\t\tFile could not be created!";
return;
}
else
{
cout<<"\n\t\t\t \'"<<filename<<"\' was created!!"<<endl<<endl;
cin.ignore();
cin.get();
CLEARSCREEN;
}
int entrymenu;
bool done=false;
pbEntry.GetEntries(File);
while(done==false)
{
cout<<setfill('-')<<setw(80)<<"-";
cout<<"\t\t\t\t "<<filename<<endl;
cout<<setfill('-')<<setw(80)<<"-";
pbEntry.DisplayEntries();
cout<<"\t\t\t\t-(1)Add Entry"<<endl;
cout<<"\t\t\t\t-(2)Delete Entry"<<endl;
cout<<"\t\t\t\t-(3)Edit Entry"<<endl;
cout<<"\t\t\t\t-(4)Save"<<endl;
cout<<"\t\t\t\t-(5)Quit"<<endl;
cout<<"\t\t\t\t->: ";
cin>>entrymenu;
switch(entrymenu)
{
case 1:
pbEntry.Add();
break;
case 2:
pbEntry.Delete();
break;
case 3:
pbEntry.Edit();
break;
case 4:
pbEntry.WriteEntries(File);
CLEARSCREEN;
break;
case 5:
done=true;
File.close();
return;
break;
default:
cout<<"\t\t\tNot Valid!";
CLEARSCREEN;
}
}
}
Code:
/*********************************/
/**** Phonebook::WriteEntries ****/
/*********************************/
void Phonebook::WriteEntries(fstream &fFile)
{
cout<<"DEBUG2"; // Seeing if the program ever makes it to this function... which it does...
for(unsigned int i=0;i<vEntries.size();i++)
{
fFile<<"\n\t\t\tName\t\t\t:"<<vEntries[i].name;
fFile<<"\n\t\t\tCell Phone Number\t:"<<vEntries[i].cnumber;
fFile<<"\n\t\t\tHome Phone Number\t:"<<vEntries[i].hnumber;
fFile<<"\n\t\t\tAddress\t\t:"<<vEntries[i].address<<endl<<endl;
}
}