Hi,
the code below compiles and runs. The program reports an error incase a user provides a path name of a .doc file. the program only accepts a notepad file(.txt) in drive C. if the .txt file is in another folder, eg., c:\my documents\dominic.txt, the program reports "Invalid filename c:\my documents\dominic.txt". My aim is to have this code accept all files (irrespective of format) in my PC's disk. Kindly teach me how to achieve this.
Code:
#include <iostream>
#include <string.h>
#include <time.h>
#include <conio.h>
#include <fstream>
#define DATA 0x378 //Data Register base address
#define STATUS DATA+1 // Status Register address
#define CONTROL DATA+2 //Control Register address
using namespace std;
//prototyping
void Interface (ifstream &fin, char (*Secure)(char c));
char EncDec (char c);
//main (): manage file stream extraction
int main ()
{
const int MaxFileNameSize=256;
// prompt and extract the name of the file
cout<<"Enter the name of the file:"<<flush;
char FileName[MaxFileNameSize]; //string FileName;
//validate the file name provided
cin>>FileName;
//ifstream fin(Filename.c_str());
//converts the string type to a char array type for the function
ifstream fin(FileName);
if (fin)
{
//we have a valid file, so determine the action
cout<<"The file contents to be made secure through"
<<"bit level transformation (Y,N):"<<flush;
char reply;
cin>>reply;
//process the file according to the user request
switch (reply)
{
case 'Y':
Interface(fin, EncDec);
break;
case 'N':
cout<<"File is left in plain version";
break;
default:
{
cerr<<"Bad request"<<endl;
return 1;
}
}
}
//Process invalid filename.
else
{
cerr<<"Invalid file name:"<<FileName<<endl;
return 1;
}
return 0;
}
//Interface (): Manage the interface to the stream cipher
//using the EncDec () function.
void Interface (ifstream &fin, char (*secure)(char c))
{
//Extract, modify and save one-character-at-a-time
//from the file.
char CurrentChar; //currenrly extracted character
while (fin.get(CurrentChar))
{ //modify and save the current character using
//Secure()
CurrentChar=(*secure)(CurrentChar);
ofstream file_op("C:\\jacob.txt", ios::app);
file_op<<CurrentChar;
}
cout<<"EOF"<<endl;
return;
}
//EncDec(): manage writing to and reading from the
//standard parallel port.
char EncDec (char c)
{
char s, n;
s=c;
outportb(CONTROL,inportb(CONTROL)&0xF0|0x04); /*initialise the
CONTROL port for data input*/
outportb(DATA,s);
/*write c to the DATA register*/
clock_t start_time;
start_time=clock();
while ((clock()-start_time)<0.25*CLK_TCK);//propagation delay time
n=(inportb(STATUS)&0xF0); /* read MSnibble*/
n=n|(inportb(CONTROL)&0x0F);/*Read LSnibble*/
n=n^0x84; /*toggle bit 2 and 7 */
return n;
}