-
Deletion program help
I'm writing a program that deletes a file specified by the user, and one specified in a data file (nothing fancy, just a text file with the .dat extension). The problems I'm having is that it crashes when the user types a directory followed by a file (C:\1.txt) and it doesn't delete it. Also, after it reads the data file, it tries to delete the file twice, so the second time it gets hit with a 404 error. Lastly, if the directory has a space in it, the program goes into an endless loop. I've only encountered it from the data file because the custom input doesn't work. I honestly have no clue about what is wrong. I haven't tried programming in a couple of months, so I'm that has a lot to do with it. Oh, my OS is Windows XP, compiler Dev-C++. Sorry for the rambling, so here is the code:
Code:
#include <iostream>
#include <fstream>
#include <conio.h>
#include <string>
using namespace std;
int manu();
int defu();
int main()
{
int choice;
cout << "1. Delete variable file" << endl
<< "2. Delete pre-determined file" << endl
<< "Enter your choice: ";
cin >> choice;
switch ( choice )
{
case 1:
manu();
break;
case 2:
defu();
break;
default:
cout << endl << "ERROR: Invalid Input!" << endl;
cin.ignore(); // Voids the key being pressed so it doesn't ........ anything up
getch(); // Waits for user to press a key
break;
}
return 0;
}
int manu()
{
ifstream inp;
string filename, command;
char buffer[132];
while ( 1 )
{
cout << endl << "Enter filename to delete: ";
cin.getline ( buffer, 132 );
filename = buffer;
if ( filename.empty() ) break; // exit if empty string
inp.open ( filename.c_str() );
if ( inp.good() ) // file exists
{
inp.close();
command = "del ";
command += filename.c_str();
cout << "Delete " << filename.c_str() << endl;
system ( command.c_str() );
cin.ignore();
getch();
}
else
{
cout << "ERROR: 404, File Not Found!" << endl;
cin.ignore();
getch();
return 1;
}
inp.clear(); // clear ios error flags
}
cin.ignore(); // These are here because it crashes otherwise
getch(); // Same for this one, and all the ones below
return 0;
}
int defu()
{
ifstream inp;
string filename, command;
char str[2048];
while ( 1 )
{
fstream file_op ( "del.dat", ios::in ); // opens del.dat
while ( file_op >> str )
cout << str << endl;
filename = str;
file_op.close();
if ( filename.empty() ) break; // exit if empty string
inp.open ( filename.c_str() );
if ( inp.good() ) // file exists
{
inp.close();
command = "del ";
command += filename.c_str();
cout << "Delete " << filename.c_str() << endl;
system ( command.c_str() );
cout << "Press any key to continue...";
cin.ignore();
getch();
}
else
{
cout << "ERROR: 404, File Not Found!" << endl;
cin.ignore();
getch();
return 1;
}
inp.clear(); // clear ios error flags
}
cin.ignore();
getch();
return 0;
}
If this is too much of a page stretcher, I can always upload a text file.
-
Code:
cout << "1. Delete variable file"<< endl
<< "2. Delete pre-determined file" << endl
<< "Enter your choice: ";
cin >> choice;
cin.ignore() //add ignore here
-
Oh, ......... Thanks for that, damn dumb mistake, but how would I actual fix those major problems? I'm throw ball after ball when I try solutions...
-
Code:
#include <iostream>
#include <fstream>
#include <string>
#include <limits>
using namespace std;
int manu();
int defu();
void flush_stdin();
int main()
{
int choice;
cout << "1. Delete variable file"<< '\n'
<< "2. Delete pre-determined file" << '\n'
<< "Enter your choice: " << flush;
cin >> choice;
flush_stdin();
switch ( choice ) {
case 1:
manu();
break;
case 2:
defu();
break;
default:
cerr << "ERROR: Invalid Input!" << endl;
break;
}
return 0;
}
inline void flush_stdin()
{
cin.clear();
cin.ignore( numeric_limits<streamsize>::max(), '\n' );
}
int manu()
{
ifstream inp;
string filename, command;
for (;;) {
cout << "Enter filename to delete: " << flush;
getline ( cin, filename );
if ( filename.empty() ) {
cerr << "No File Name inputed." << endl;
break;// exit if empty string
}
inp.open ( filename.c_str() );
if ( inp ) { // file exists
inp.close();
command = "del " + filename;
cout << "Delete " << filename << endl;
system ( command.c_str() );
} else {
cerr << "ERROR: 404, File Not Found!" << endl;
}
inp.clear();// clear ios error flags
}
return 0;
}
int defu()
{
ifstream inp;
string filename, command;
ifstream file_op ( "del.dat" );// opens del.dat
while ( getline( file_op, filename ) ) {
if ( filename.empty() ) {
continue;
}
inp.open( filename.c_str() );
if ( inp ) { // file exists
inp.close();
command = "del " + filename;
cout << "Delete " << filename << endl;
system ( command.c_str() );
} else {
cerr << "ERROR: 404, File Not Found!" << endl;
}
inp.clear();// clear ios error flags
}
return 0;
}
-
And what did you do in the code Antigloss??? I, and probably others dont want to look through the code and compare to actually see what you did. Why not highlight it.
To me just looks like you changed the formatting...which isn't useful.