Skip to content

Instantly share code, notes, and snippets.

@JSkally
Created April 21, 2013 21:48
Show Gist options
  • Save JSkally/5431205 to your computer and use it in GitHub Desktop.
Save JSkally/5431205 to your computer and use it in GitHub Desktop.
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
void loadFile();
void processFile(int);
char txtfile[512];
int recordsize=22;
char id[9];
char inFileName[12];
char outFileName[12];
void viewContents();
void saveFile();
FILE *open_file(char*, char*);
int fileLoaded=0;
int numrecords=0;
int fileEncrypted=0;
int currentrec=0;
int main ()
{
char option = '0';
id[0]= '7';
id[1]='6';
id[2]='4';
id[3]='5';
id[4]='8';
id[5]='2';
id[6]='5';
id[7]='1';
id[8]='2';
id[9]=0;
while (option !='X')
{
system("cls");
printf("File Accessed:");
if (fileLoaded==1)
printf("%s\n",inFileName);
else
printf("None\n");
printf("[L]oad a file\n");
if (fileLoaded==1)
{
printf("[V]iew contents\n");
if (fileEncrypted==0)
printf("[E]ncrypt file\n");
else
printf("[D]ecrypt file\n");
printf("[S]ave file\n");
}
printf("E[X]it\n");
option =getchar();
switch(option)
{
case 'L':case'l':{loadFile();
break;
}
case 'V':case'v':{viewContents();
break;
}
case 'E':case'e':{processFile(1);
break;
}
case 'D':case'd':{processFile(0);
break;
}
case 'S':case's':{saveFile();
break;
}
}
}
/*end of while statement*/
printf("End of program\n");
} /*end main*/
void loadFile()
{
int ch=0;
int filepos = 0;
FILE *in;
printf("Please input file name:");
scanf("%s",inFileName);
/*
char ext[3];
for(int=0;i<2;i++)
{
ext[i]=inFileName[9+i];
}
if ext=='enc'
*/
numrecords =1;
in = open_file (inFileName, "r" );
while((ch=fgetc(in))!=EOF)
{ txtfile[filepos]=ch;
if (ch=='\n')
numrecords++;
filepos++;
}
fclose ( in );
fileLoaded = 1;
printf("File has been loaded\n");
printf("Hit the enter key to continue\n");
while (!(kbhit()));
}
/*end of loadfile*/
FILE *open_file ( char *file, char *mode )
{
FILE *fp = fopen ( file, mode );
if ( fp == NULL ) {
perror ( "Unable to open file" );
exit ( EXIT_FAILURE );
}
return fp;
}
void viewContents()
{
int total=0,num;
char strint[3];
/* for(currentrec=0;currentrec<=numrecords;currentrec++)*/
currentrec = 0;
printf("===========================================\n");
printf("\tFile Contents\t\t\n");
printf("===========================================\n");
while (currentrec < numrecords)
{ printf("%.*s\n",recordsize, txtfile+(currentrec*(recordsize+1)));
strint[0]= txtfile[20+(currentrec*(recordsize+1))];
strint[1]=txtfile[21+(currentrec*(recordsize+1))];
strint[2]=0;
num=atoi(strint);
total+=num;
currentrec=currentrec+1;
}
if (fileEncrypted==0)
{
printf("File Totals\t:");
printf("%d\n",total);
}
/*print contents, 22 characters at a time*/
/*for each row, convert last two digits to integer*/
/*print footer at the end*/
printf("Hit the enter key to continue\n");
while (!(kbhit()));
}
void processFile(int mode)
{
int i,shiftval;
currentrec = 0;
if(mode==1)
{
while (currentrec < numrecords)
{
for ( i= 0; i <= recordsize-1;i++)
{
shiftval = id[i % 9]-48;
txtfile[i + currentrec*(recordsize+1)]= txtfile[i + currentrec*(recordsize+1)] - shiftval;
}
currentrec++;
}
fileEncrypted= 1;
printf("File contents have been encrypted\n");
}
else
{
if(mode==0)
{
while (currentrec < numrecords)
{
for ( i= 0; i <= recordsize-1;i++)
{
shiftval = id[i % 9]-48;
txtfile[i + currentrec*(recordsize+1)]= txtfile[i + currentrec*(recordsize+1)] + shiftval;
}
currentrec++;
}
fileEncrypted= 0;
printf("File contents have been decrypted\n");
}
}
printf("Hit the enter key to continue\n");
while (!(kbhit()));
}
void saveFile()
{
FILE *fptr;
printf("Enter code that will save the file\n");
fptr = open_file(inFileName,"w");
if(fptr==NULL)
{
printf("File could not be opened\n");
}
else
{
fprintf(fptr,txtfile);
fclose(fptr);
}
printf("Hit the enter key to continue\n");
while (!(kbhit()));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment