Look at the function HandleEncrypt()... The look even closer at the while loop that is there... What am i doing wrong here. I am trying to open a file then read the contents of the file to Contents[] so that i can later change the contents. Then i am printing the contents to the screen... The program compiles fine but that one line that i have remarked is causing me problems... I think im using m array worng..
Code:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
struct Encrypt{
char *Password;
int RandomVal;
}EnCryptoGraph;
struct Decrypt{
char *Password;
int RandomVal;
}DeCryptoGraph;
int OpenFile(char *Path, char *RW);
int RandomNum(void);
void HandleMenuChoice(void);
void HandleEncrypt(void);
void HandleDecrypt(void);
char *FilePath, Contents[] = "Testing";
FILE *fp;
int main(void){
srand( (unsigned)time( NULL ) ); /****** SEEDING rand() FOR CRYPTIC CODE ******/
printf("***** ***** * * ***** ******* ***** ******* ***** * * *******\n");
printf("* * * * * * * * * * * * * * *\n");
printf("* **** * ***** * * * * **** * *\n");
printf("* * * * * * * * * * * * *\n");
printf("***** * * * * * ***** * ***** * * *\n");
/* ***********Main Menu********** */
printf("\n1) Encrypt\n");
printf("2) Decrypt\n");
printf("\nEnter a Number: ");
HandleMenuChoice();
return 0;
}
void HandleMenuChoice(void){
int ch;
while (1){
ch = getchar();
if(ch == 49){
HandleEncrypt();
break;
}
else{
if (ch == 50){
HandleDecrypt();
break;
}else if(ch != '\0') break;
}
printf("\nInvalid Entry\n Please Enter a Number(1-2):");
}
}
/* *********Handle Encryption********* */
void HandleEncrypt(void){
char c;
int cnt;
printf("You Pressed 1\n");
printf("%d\n",RandomNum());
printf("Type the Path and File Name of your .txt file:\n");
scanf("%s", &FilePath);
printf("%s\n",&FilePath);
OpenFile("c:\\Testing.txt","r+");
printf("c is %d",(c = fgetc(fp)));
while ( ( c = fgetc(fp)) != '\0' ){
Contents[cnt] = c; /* Problem Here */
putchar(c);
cnt++;
}
}
/* *********Handle Decryption********* */
void HandleDecrypt(void){
printf("You Pressed 2\n");
printf("%d\n",RandomNum());
}
/* ******Generate Random Number For Encryption Code****** */
int RandomNum(){
int num;
num = rand()%9;
return num;
}
/* ***** Open Binary File ***** */
int OpenFile(char *Path, char *RW){
if ( (fp = fopen(Path, RW)) == NULL)
{
fprintf(stderr, "Error opening file.");
}else printf("File Open\n");
return 0;
}