0

Glad to ask you again!

I want to create a program that basically reads a file called message.txt which would have some texts with a message, let's say: ''Hello I am a program'' and then encrypts that message and puts it in a file called encryptMessage.txt, additionally it will save the key used for the user in the file key.txt. Now this is what I have done so far. I don't know how to make the program read the file message.txt, show it into the screen and then encrypt it into the file. Any piece of advice? Thank you!

I was planning to use fscanf, but I can't use it because it's a line, not just a single string.

Please, if possible write the code yourself so I can compare it to what I have written so far. I always appreciate your feedback, thanks!

#include <stdio.h>
#include <ctype.h>
#define MAXSIZE 100

int main(void)
{
    FILE *message;
    FILE *encryptMessage;
    FILE *key;

    message = fopen("message.txt", "r");
    encryptMessage = fopen("encryptMessage.txt", "w");
    key = fopen("key.txt", "w");

    if  ((encryptMessage == NULL) || (encryptMessage == NULL) || (encryptMessage == NULL))
    {
            printf("Error reading file!!!\n");
            return 1;
    }

    int userKey;
    char sentence[MAXSIZE];
    char q[MAXSIZE];
    int i = 0;

    printf("Input the text that you want to encrypt:\n> "); // These two lines are a test to see if I was able to encrypt the message, but this is not necessary. It should directly read the file called message.txt.
    fgets(sentence, 99, stdin);

   // printf("\nThe string that you wrote is:\n%s\n\n", sentence);

    printf("Input the key:\n");
    scanf("%d", &userKey);
    fprintf(key, "%d", userKey);

    //printf("\nThe key that you selected is: %d\n\n", userKey);

    for(i = 0; sentence[i] != '\0'; ++i)
    {
        if( ( isupper(sentence[i]) ) || ( islower(sentence[i]) ) )
        {
            q[i] = sentence[i] + (char)userKey;
        }
        else
        {
            q[i] = (sentence[i]);
        }
    }

    q[i] = '\0';
    printf("%s", q);
    fprintf(encryptMessage, "%s", q);

    fclose(encryptMessage);

    return 0;
}
1
  • To answer the "how to read a full line" question: see this question & answer. Commented Dec 11, 2014 at 16:34

1 Answer 1

1

To read a line from message.txt you need to use fgets function.

fgets(sentence, 99, stdin);

The above fgets(which you have in your code) reads from the stdin which is normally the keyboard. To make it read from the text file,use

fgets(sentence, MAX_SIZE, message);

Note the change in the second argument too. If you want to display whatever was scanned,uncomment the below line which you have in your code

//printf("\nThe string that you wrote is:\n%s\n\n", sentence);

Don't forget to close(using fclose) all the FILE pointers which you had opened(using fopen) after its use.

Sign up to request clarification or add additional context in comments.

1 Comment

This was so easy, I figured it out before you told me. I just tried changing it and it worked, surprisingly hahaha. Thank you very much

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.