-1

I'm working on Shift cipher, I am having problems with encryption. It has no errors or trouble compiling but after I run it the output file is empty. i think reading the file but not encrypted out.txt file is empty. i didn't solve it. Thank you.

int main
{

    file_in = fopen("/Users/mathmoiselle/Desktop/lucky.txt", "r");

    if( file_in == NULL )
    {
        perror("Error while opening the file.\n");
        exit(EXIT_FAILURE);
    }

    file_out = fopen("/Users/mathmoiselle/Desktop/out.txt","r");

    return 0;
}
jekyll
  • 51
  • 1
  • 10
  • 2
    You need to open `file_out` with write privileges (i.e. `file_out = fopen("/Users/mathmoiselle/Desktop/out.txt","w");` – Aidenhjj Nov 09 '16 at 17:04
  • Thanks reply, ok i changed that line " file_out = fopen("/Users/mathmoiselle/Desktop/out.txt","w"); " still my file is empty. – jekyll Nov 09 '16 at 17:07
  • One problem is that you need to rewind the file pointer for `file_in` after the first time you run through it (i.e. `rewind(file_in)`) – Aidenhjj Nov 09 '16 at 17:13

1 Answers1

1

Following on from my comments. You need to rewind the file pointer for file_in and also your includes were poorly formatted at the top. Not sure whether this makes a difference (beginner myself, but certainly stuck out when I read it):

#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>

int encode (int, int);

int encode(int ch, int key) {
    if (islower(ch)) {
        ch = (ch-'a' + key) % 26 + 'a';
        ch += (ch < 'a') ? 26 : 0;
    }
    else if (isupper(ch)) {
        ch = (ch-'A' + key) % 26 + 'A';
        ch += (ch < 'A') ? 26 : 0;
    }
    return ch;
}
int main (void)
{
    FILE *file_in;
    FILE *file_out;
    char ch;
    char text[300];
    int key;

    // gets(text);  // Removed in question
    file_in = fopen("shift_cipher.c", "r");


    if( file_in == NULL )
    {
        perror("Error while opening the file.\n");
        exit(EXIT_FAILURE);
    }
    printf("\n The contents of the file are : \n");

    while( ( ch = fgetc(file_in) ) != EOF )
    {
        printf("%c",ch);
    }

    rewind(file_in);

    // while (fgets(line, MAXLINE, f1)) {
    //     printf("%s", line);
    // }

    // gets(text);  // Removed in question
    file_out = fopen("out.txt","w");

    printf("\n Enter the alphabetic offset key you would like to use:");
    scanf("%d", &key);

    while( ( ch = fgetc(file_in) ) != EOF )
    {
        printf("%c", ch);
        ch=encode(ch, key);
        fprintf(file_out, "%c", ch);
    }
    printf("file has been encoded");
    fclose(file_out);
    fclose(file_in);

    return 0;
}
Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
Aidenhjj
  • 1,249
  • 1
  • 14
  • 27
  • 1
    awwww, ok now i understand. Thank you so much. (i am newbie at C :) ) And btw i deleted some lines, `gets(text);` and `char text[300];` that's not necessary i think. Thank you again. – jekyll Nov 09 '16 at 17:25
  • and i have just one question too, can i add generate random key? – jekyll Nov 09 '16 at 17:28
  • Look at http://stackoverflow.com/questions/822323/how-to-generate-a-random-number-in-c, then use the random number generated, times it by the number of letters, and minus `char 'a'`. – Aidenhjj Nov 09 '16 at 17:30
  • but i used % for modulo. Actually i wanted user enter the key but i wondered how is generate random key. i will try this lines: `srand(time(NULL)); int r = rand();` as you were saying. – jekyll Nov 09 '16 at 17:40
  • just do `int r = (rand() % 26)` – Aidenhjj Nov 09 '16 at 17:42
  • The only thing then is the 'key' is generated inside the program and lost afterwards. So you'll have to crack the code (find what random value the computer assigned to 'key') in whatever decoder you're implementing. – Aidenhjj Nov 09 '16 at 17:46
  • i want randomly choosing a number from a text file again, i.e i'll create key.txt on my desktop again, which include 0123456789 Hmm i will try your code. – jekyll Nov 09 '16 at 17:46
  • inverse is simply, i decoded ciphertext (reading from file out.txt), but if we know the key :) – jekyll Nov 09 '16 at 17:47
  • Cannot up-vote an answer that suggests using depreciate/dropped `gets()`. Consider re-write. – chux - Reinstate Monica Nov 09 '16 at 19:22
  • Aidenhjj i did it. Our code are read plaintext from the lucky.txt file and generated random key. (`srand( (unsigned int) time(NULL) );` `key = rand() % 26; ` ..etc.) and it wrote ciphertext to output.txt and generated key to key.txt file. For decryption, read the key from key.txt and ciphertext from output1.txt file and wrote the plaintext to output2.txt file. it works :) i did it. with your help thank you ^^ – jekyll Nov 09 '16 at 20:38
  • @Aidenhjj, i need your help dude, i am dealing with affine cipher but encryption doesnt work correctly. actually the original affine encryption function is r = ((alpha * p) + beta ) % 26; thats ok but how can i modified inside putc function >> `putc( ((alpha * p) + beta - 65) % 26 + 65 , cipher);` i checked the keys in txt file but it doesnt correctly encrypted. (char p;) – jekyll Nov 16 '16 at 21:59