C program to caesar cipher file to output, help needed

This is a discussion on C program to caesar cipher file to output, help needed within the C Programming forums, part of the General Programming Boards category; I need to write a program that asks the user for the text file, prints its contents, then asks for ...

  1. #1
    Registered User
    Join Date
    Nov 2012
    Posts
    4

    C program to caesar cipher file to output, help needed

    I need to write a program that asks the user for the text file, prints its contents, then asks for the output file, encrypts the contents of the first file with caeser cipher and writes the encryption to the output file. So far I have this code, it writes the contents of the file, asks for the output file, and offset key, it has no errors or trouble compiling but after I run it the output file is still empty!
    Code:
    #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;
    
    
        printf("Enter the name of the file you wish to see including complete pathway and file\n extension:\n");
        gets(text);
        file_in = fopen(text,"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);
        }
    
    
        printf("\n \n Enter the name of the file you wish to send the encryption\n to including complete pathway and file\n extension:\n");
        gets(text);
        file_out = fopen(text,"w");
        
        printf("\n Enter the alphabetic offset key you would like to use:");
        scanf("%d", &key);
        
        while( ( ch = fgetc(file_in) ) != EOF )
        { 
            encode(ch, key);
            fprintf(file_out, "%c", ch);
        }
        printf("file has been encoded");
        fclose(file_out);
        fclose(file_in);
        
        return 0;
    }

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,592
    > encode(ch, key);
    This returns a result, which you're ignoring.

    Oh, and stop using gets()
    http://sourceforge.net/apps/mediawik...php?title=Gets
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,802
    Code:
    char ch;
    
    ...
    
    while( ( ch = fgetc(file_in) ) != EOF )
    fgetc returns an int value which is very important when comparing against EOF.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  4. #4
    Registered User
    Join Date
    Nov 2012
    Posts
    4
    is there any chance you could write some code to replace it with? I see the errors, i'm just not sure how to fix them!

  5. #5
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Quote Originally Posted by SaraTerry View Post
    is there any chance you could write some code to replace it with? I see the errors, i'm just not sure how to fix them!
    The problem you're having is caused by not knowing how ch is being used here. The ch in main() is NOT the same as the ch variable in encode() - that is a COPY of the ch from main().

    If you want to use the value from encode (and you do), you need to "catch" it. How to catch it: ch = encocde(ch, key). Or, you could send ch as a pointer to the variable. But otherwise, you just "throw away" the value of ch, from encode().

    So you have two way you can do this, and no, we won't write your code for you. Understand that C ALWAYS copies paramaters for a function. If the variable is a pointer to a variable, then and only then, will the variable it points to be changed. Otherwise, the copy of that variable will be the only thing that is changed.

  6. #6
    Registered User
    Join Date
    Nov 2012
    Posts
    4
    so I should change it to
    ch=encode(ch, key);
    fprintf(file_out, "%c", ch);
    I've only covered pointers in files to move from line to line, so i'm not sure about the other way

  7. #7
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Quote Originally Posted by SaraTerry View Post
    so I should change it to
    ch=encode(ch, key);
    fprintf(file_out, "%c", ch);
    I've only covered pointers in files to move from line to line, so i'm not sure about the other way
    Yes, and understand that the "ch" you are having returned from encode() is a COPY of the "ch" you have in main(), and of course, has been given a different value, ( by encode() ).

  8. #8
    Registered User
    Join Date
    Nov 2012
    Posts
    4
    Thank you for your help, and I understand that now but my output file is still blank after I run the program!

  9. #9
    Registered User
    Join Date
    May 2012
    Posts
    1,066
    Quote Originally Posted by SaraTerry View Post
    Thank you for your help, and I understand that now but my output file is still blank after I run the program!
    Code:
    while( ( ch = fgetc(file_in) ) != EOF )
    {
        printf("%c",ch);
    }
    ...
    while( ( ch = fgetc(file_in) ) != EOF )
    {
    After reading the input file in your first while-loop the file position indicator is at the end of the file. Thus when you try to read a character in your second while-loop you'll get EOF.

    You have to put the file indicator back to the beginning. Look at rewind().

    Bye, Andreas

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. I need help on what I think is a Caesar cipher......
    By CaesarCipher in forum C++ Programming
    Replies: 8
    Last Post: 11-28-2011, 03:13 PM
  2. Caesar Cipher help!!
    By darshan10 in forum C Programming
    Replies: 6
    Last Post: 10-19-2011, 04:58 PM
  3. Caesar Cipher
    By dldsob in forum C++ Programming
    Replies: 7
    Last Post: 07-06-2009, 06:06 PM
  4. Another Caesar Cipher
    By Swordsman in forum C++ Programming
    Replies: 6
    Last Post: 09-07-2007, 08:56 AM
  5. caesar cipher help.
    By stormfront in forum C Programming
    Replies: 36
    Last Post: 11-22-2005, 07:45 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21