Here is the section that asks the user to enter the name of the read file and the write file. Notice that the read file and the write file are in two seperate sections.
Code:
// Definition of function filterIP
void filterIP(bool &veri1, fstream &source, fstream &output, AddIP &aIP, DNS &dns)
{
char opt;
do
{
char *SFile = NULL;
SFile = new char[15];
veri1 = true;
cout << "\nEnter source file: ";
cin >> SFile;
cin.ignore();
try
{
aIP.getSFile(source, SFile);
}
catch(AddIP::NoFile)
{
cout << "\nERROR: File \"" << SFile << "\" not found!\n"
<< "\nPress (C)ontinue or (E)xit: ";
cin >> opt;
cin.ignore();
while (toupper(opt) != 'C' && toupper(opt) != 'E')
{
cout << "\nInvalid choice. Press (C)ontinue or (E)xit: ";
cin >> opt;
cin.ignore();
}
if (toupper(opt) == 'E')
exit(0);
else
veri1 = false;
}
delete [] SFile;
}while(!veri1);
do
{
char *OFile = NULL;
OFile = new char[15];
veri1 = true;
cout << "\nEnter output file: ";
cin >> OFile;
cin.ignore();
try
{
aIP.getOFile(output, OFile);
}
catch(AddIP::NoFile)
{
cout << "\nERROR: File \"" << OFile << "\" not found!\n"
<< "Do you want to create an output file? (Y/N): ";
cin >> opt;
cin.ignore();
while (toupper(opt) != 'N' || toupper(opt) != 'Y')
{
cout << "\nInvalid choice. Press (C)ontinue or (E)xit: ";
cin >> opt;
cin.ignore();
}
if (toupper(opt) == 'Y')
{
output.open(OFile, ios::out | ios::app);
break;
}
else
veri1 = false;
}
delete [] OFile;
}while(!veri1);
veri1 = false;
cout << "\nScanning source file...\n"
<< "Writing IP to output file...\n\n";
aIP.seekIP(source, output);
cout << "\nProgram has reached the end of source file. Press any key to"
<< " continue";
cin.get();
system("cls");
}
Here are the sections that opens the read file and write file:
Code:
void AddIP::getSFile(fstream &source, char *sFile)
{
source.open(sFile, ios::in);
if (source.fail())
throw NoFile();
}
void AddIP::getOFile(fstream &output, char *oFile)
{
output.open(oFile, ios::out | ios::app);
if (output.fail())
throw NoFile();
}
Do you see anything that could cause the repetive "file does not exists" problem even if the user enter a file name that does exist? Remember, the program works perfect when: 1)program first starts AND 2)user enters correct file name
Otherwise, the program either says that the files not exist. I have tried cout the file name right before "source.open" and "output.open." The stored value are valid. Something is keeping it from openning. Maybe "source.fail()" and "output.fail()" do not work right.
Kuphryn