Hello, I'm currently doing a project that requires the user to run a program with a source file name and destination file name as parameters. The program then encrypts the text from the source file and puts it into the destination file using a simple substitution cipher. The program reads in a character from the source, puts it into a method that returns the encrypted character, and then is supposed to write the encrypted character into the file. When I try to write the character into the file, I get a segfault. I've tried just about everything to try and fix this. I have a feeling it's right under my nose. Anyways, here's my routine that encrypts everything:
Code:
void encrypt_file(CIPHER * cipher_key, FILE * src_file, FILE * dest_file){
char encrypted_char;
while((encrypted_char = get_encrypted_char(cipher_key, fgetc(src_file))) != EOF){
//printf("%c", encrypted_char);
fputc(encrypted_char, dest_file);
}
fclose(src_file);
fclose(dest_file);
}
and here's the get_encrypted_char method:
Code:
char get_encrypted_char(CIPHER * cipher_key, char char_to_encrypt){
int i;
char encrypted_char;
encrypted_char = char_to_encrypt;
for (i = 0; i < CIPHER_LENGTH; i++){
if(char_to_encrypt == cipher_key->plaintext[i]){
encrypted_char = cipher_key->ciphertext[i];
}
}
return encrypted_char;
}
the CIPHER is a struct that encapsulates a plaintext character array along with a ciphertext character array such that their indices match. Any insight on this would be greatly appreciated.