0

this is a program that is giving me many headaches, but I am tackling it!

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.

That part is already finished. There's only one last thing that I need to do.

The file message.txt has more than one line. For example:

hello I like programming
this is a test
to see if this program
can read and encrypt many lines

I want the program to read all those lines and then encrypt them and save them in encryptMessage.txt, such as this (let's suppose the key is 3):

khoor L olnh surjudpplqj
wklv lv d whvw
wr vhh wklv surjudp
fdq uhdg dqg hgfu|sw pdq| olqhv

However, I do not know how to make it work. I know I need to use a loop and !feof function or something like that. But I sincerely do not know how to implement, do you have any idea? Thank you very much!

#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  ((message == NULL) || (encryptMessage == NULL) || (key == 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> ");
    fgets(sentence, 99, message);


   // 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);
    fclose(key);
    fclose(message);

    return 0;
}
Eddy Mogollón
  • 15
  • 1
  • 1
  • 4
  • You simply want to read/write the file? Google is your friend. 1: http://stackoverflow.com/questions/3501338/c-read-file-line-by-line, 2: http://www.cprogramming.com/tutorial/cfileio.html – Andrew Dunai Dec 11 '14 at 18:04
  • Read/write the first land is easy. My confusion is in getting to read all the lines and write all the lines as I showed in my post. – Eddy Mogollón Dec 11 '14 at 18:20
  • But what is the problem? You can simply do "`read`, `encrypt` and write to output file" for each source line. – Andrew Dunai Dec 11 '14 at 18:22
  • How do I do that? Is there any chance that you can modify my source code and put it as an example? The thing is that the message.txt file can be modified to have more or less lines and have a different message. So I want the program to read all the lines, and write all of them. My program so far only reads the first line and modifies the first line. – Eddy Mogollón Dec 11 '14 at 18:29

1 Answers1

0

Here's how you can read entire file:

while(fgets(sentence, MAXSIZE - 1, message)) {
    // do something with sentence
}

Here is modified source code: http://pastebin.com/KxAe9KcS

Andrew Dunai
  • 3,061
  • 19
  • 27