-1

In this program, I've used the simple substitution cipher on a text file named "message" in the same folder as the program. However when I run it, the message doesn't change from "abcdefg". I've looked at the code numerous times but I don't see the problem, maybe I'm just a amateur in C. Anything to help is much appreciated. Thanks!

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>    
#include <stdlib.h>   

const int MAXSIZE = 50;
void Encrypt(FILE *File, char file[MAXSIZE], int i)  
{
    File = fopen("message.txt", "r+");    

    for (i = 0; i < 6; i++)
    {

        if (file[i] == 'a')
        {
            file[i] = 'F';
            fputc(file[i], File);
        }

        if (file[i] == 'b')

        {
            file[i] = 'G';
            fputc(file[i], File);
        }

        if (file[i] == 'c')
        {
            file[i] = 'Z';
            fputc(file[i], File);
        }

        if (file[i] == 'd')
        {
            file[i] = 'V';
            fputc(file[i], File);
        }


        if (file[i] == 'e')
        {
            file[i] = 'A';
            fputc(file[i], File);
        }


        if (file[i] == 'f')
        {
            file[i] = 'B';
            fputc(file[i], File);
        }


        if (file[i] == 'g')
        {
            file[i] = 'G';
            fputc(file[i], File);
        }


        if (file[i] == 'h')
        {
            file[i] = 'X';
            fputc(file[i], File);
        }

        if (file[i] == 'i')
        {
            file[i] = 'E';
            fputc(file[i], File);
        }

        if (file[i] == 'j')
        {
            file[i] = 'J';
            fputc(file[i], File);
        }

        if (file[i] == 'k')
        {
            file[i] = 'I';
            fputc(file[i], File);
        }

        if (file[i] == 'l')
        {
            file[i] = 'K';
            fputc(file[i], File);
        }

        if (file[i] == 'm')
        {
            file[i] = 'O';
            fputc(file[i], File);
        }


        if (file[i] == 'n')
        {
            file[i] = 'V';
            fputc(file[i], File);
        }

        if (file[i] == 'o')
        {
            file[i] = 'U';
            fputc(file[i], File);
        }

        if (file[i] == 'q')
        {
            file[i] = 'Y';
            fputc(file[i], File);
        }

        if (file[i] == 'r')
        {
            file[i] = 'N';
            fputc(file[i], File);
        }


        if (file[i] == 's')
        {
            file[i] = 'P';
            fputc(file[i], File);
        }


        if (file[i] == 't')
        {
            file[i] = 'R';
            fputc(file[i], File);
        }


        if (file[i] == 'u')
        {
            file[i] = 'C';
            fputc(file[i], File);
        }


        if (file[i] == 'v')
        {
            file[i] = 'Q';
            fputc(file[i], File);
        }


        if (file[i] == 'w')
        {
            file[i] = 'T';
            fputc(file[i], File);
        }


        if (file[i] == 'x')
        {
            file[i] = 'D';
            fputc(file[i], File);
        }


        if (file[i] == 'y')
        {
            file[i] = 'S';
            fputc(file[i], File);
        }


        if (file[i] == 'z')
        {
            file[i] = 'H';
            fputc(file[i], File);
        }
    }

    printf("Check the file for encrypted message!\n\n");

    fclose(File);
    return;
}

int main()
{
    int i = 0;
    char file[MAXSIZE];
    FILE *File = 0;

    Encrypt(File, file, i);   

    system("pause");
    return 0;
}
J...S
  • 5,079
  • 1
  • 20
  • 35
  • 1
    Your input buffer is empty (not initialized). From where do you want to get the clear text? – yacc Sep 13 '17 at 16:31

2 Answers2

0

Your input buffer is never initialized. Add this after file open:

fgets(file, MAXSIZE, File);
/* todo: check for error */

If you want to overwrite the message, use rewind(File). And write a newline when you're done.

yacc
  • 2,915
  • 4
  • 19
  • 33
0

In function Encrypt(), after opening the file you haven't actually read data from the file into the string file.

You could use fgets() to read from the open file into the string file like

fgets(file, sizeof(file), File);

It should be noted that fgets() will read in the trailing \n to the string.

After reading the line the file pointer would've advanced and you need to get it back to its original position before writing.

You could save the current position in a variable, read from file and set the file pointer back using the saved value with fseek().

long currentPos=ftell(File);
fgets(file, MAXSIZE, File);
fseek(File, currentPos, 0);

Also, instead of the multiple ifs, it would be better to else ifs after the first if. In this way, the ifs after the matched if needn't be examined.

Or you could do away with all those annoying ifs and do something like

char original[]="abcdefghijklmnoqrstuvwxyz";
char translation[]="FGZVABGXEJIKOVUYNPRCQTDSH";
for(j=0; translation[j]!='\0'; ++j)
{
    if(file[i]==original[j])
    {
        file[i]=translation[j];
        fputc(file[i], File);
    }
}

And the use of system("pause") is not considered a good practice. See here.

J...S
  • 5,079
  • 1
  • 20
  • 35