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.



LinkBack URL
About LinkBacks


