Feb 03 2009
File Manipulation
/*************************************************************************/
/* Description : This program performs several file system operations */
/* like displaying contents of file, renaming, deleting and*/
/* copying of files. */
/*************************************************************************/
#include <iostream>
#include <fstream>
using namespace std;
bool fileValid(char filename[]);
bool fexists(char filename[]);
void displayFile(char *filename);
void renameFile(char *filename, char *newName);
void copyFile(char *filename, char *newName);
void deleteFile(char *filename);
/*************************************************************************/
/* Purpose : Displays the menu and asks for the user input */
/* Parameters : none */
/* Return value : none */
/*************************************************************************/
void main() {
int input = 0;
char filename[256], filename2[256];
do
{
cout << “\n\n\n”;
cout << “[1] Display File” << endl;
cout << “[2] Rename File” << endl;
cout << “[3] Copy File” << endl;
cout << “[4] Delete File” << endl;
cout << “[0] Exit” << endl << endl << endl;
cout << “Command >> “;
cin >> input;
switch (input)
{
case 1:
cout << “[ VIEW ] Enter filename: “;
cin >> filename;
displayFile(filename); // displays the contents of the file
break;
case 2:
cout << “[ RENAME ] Enter filename: “;
cin >> filename;
cout << “[ RENAME ] Enter new filename: “;
cin >> filename2;
renameFile(filename, filename2); // renames the file
break;
case 3:
cout << “[ COPY ] Enter filename: “;
cin >> filename;
cout << “[ COPY ] Enter new filename: “;
cin >> filename2;
copyFile(filename, filename2); // copies the file
break;
case 4:
cout << “[ DELETE ] Enter filename: “;
cin >> filename;
deleteFile(filename); // removes the file
break;
default:
system(”exit”);
}
} while (input < 5 && input > 0);
}
/*************************************************************************/
/* Purpose : Checks if the filename is valid (.txt) */
/* Parameters : filename */
/* Return value : boolean, true if file is valid, false if not */
/*************************************************************************/
bool fileValid(char filename[])
{
if( strcmp(”.txt”, strrchr(filename, ‘.’)) == 0)
return true;
else
return false;
}
/*************************************************************************/
/* Purpose : Checks if the file exists. */
/* Parameters : filename */
/* Return value : boolean, true if file exists, false if not */
/*************************************************************************/
bool fexists(char filename[])
{
ifstream ifile(filename);
return ifile;
}
/*************************************************************************/
/* Purpose : Displays the contents of a file. */
/* Parameters : filename */
/* Return value : none */
/*************************************************************************/
void displayFile(char *filename)
{
char command[256];
if( !fileValid(filename) )
cout << “File type not supported.” << endl << endl;
else if ( !fexists(filename) )
cout << “File does not exist.” << endl << endl;
else {
cout << “\n\n”;
strcpy(command, “type “);
strcat(command, filename);
system(command);
}
}
/*************************************************************************/
/* Purpose : Renames a file. */
/* Parameters : filename - the filename to be renamed */
/* newName - the new filename */
/* Return value : none */
/*************************************************************************/
void renameFile(char *filename, char *newName)
{
char command[256];
if( !fileValid(filename) )
cout << “File type not supported.” << endl << endl;
else if ( !fexists(filename) )
cout << “File does not exist.” << endl << endl;
else if ( !fexists(newName) )
cout << “File already exists.” << endl << endl;
else {
strcpy(command, “ren “);
strcat(command, filename);
strcat(command, ” “);
strcat(command, newName);
system(command);
}
}
/*************************************************************************/
/* Purpose : Copies a file to a desired filename */
/* Parameters : filename - the filename to be copied */
/* newName - the target filename */
/* Return value : none */
/*************************************************************************/
void copyFile(char *filename, char *newName)
{
char command[256];
if( !fileValid(filename) )
cout << “File type not supported.” << endl << endl;
else if ( !fexists(filename) )
cout << “File does not exist.” << endl << endl;
else {
strcpy(command, “copy “);
strcat(command, filename);
strcat(command, ” “);
strcat(command, newName);
system(command);
}
}
/*************************************************************************/
/* Purpose : Removes a file from the file system */
/* Parameters : filename - the filename to be deleted */
/* Return value : none */
/*************************************************************************/
void deleteFile(char *filename)
{
char command[256];
if( !fileValid(filename) )
cout << “File type not supported.” << endl << endl;
else if ( !fexists(filename) )
cout << “File does not exist.” << endl << endl;
else {
strcpy(command, “del “);
strcat(command, filename);
system(command);
}
}


