Thread: Problems Running a Caesar Cipher

  1. #1
    Registered User
    Join Date
    Jun 2014
    Posts
    8

    Problems Running a Caesar Cipher

    Hello forums. I was making some adjustments to a Caesar Cipher to allow for it to read a .txt file. However, after having saved the file in numerous places, including the directory where Code:Blocks is saved, I was still not able to get it to work. Can someone please take a look at my code and tell me if the code actually works?

    Code:
    #include<stdio.h>
    #include<conio.h>
    #include<string.h>
    
    
    void main()
    {
        int shift, i;
        char code[30];
    
    
        FILE*fp;
    
    
        fp=fopen("caesarText","r");
    
    
        if(fp ==NULL){ printf("Error opening input file \"caesarText\"!\n");
        return 0;
       }
        else{
        printf("Enter how much you would like to shift the text: /n");
        scanf("%d", &shift);
            {
                for(i=0;i<strlen(code);i++)
                {       if(code[i]==' '){}
                        else
                        {
                            if (code[i]>='x')
                            {
                            code[i]=code[i]-26;
                            }
                            code[i]=code[i]+shift;
                        }
                }
    
    
        }
        printf("Your encrypted text is: %s", code);
        getch();}
    }

  2. #2
    Registered User
    Join Date
    Mar 2012
    Location
    the c - side
    Posts
    373
    If by not getting it to work you mean you are getting the "Error opening input file" message indicating that the file can't be found:

    With Codeblocks I normally chuck any files in the project folder for the specific project I'm working on and they are found no problem. Depending on your system you may need the .txt filetype appended to the filename though.

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > fp=fopen("caesarText","r");
    The first thing you should probably do is go into windows explorer and turn off that dreadful feature "hide extensions of known file types".
    Showing or hiding file name extensions - Windows Help

    Your file will almost certainly have a .txt extension, and you'll need that in your fopen() parameter as well.
    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.

  4. #4
    Registered User
    Join Date
    Jun 2014
    Posts
    8
    I have the file saved in C drive ,program files x86, same place that code blocks in saved in. It is still not working.

  5. #5
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    Quote Originally Posted by Ayu12 View Post
    I have the file saved in C drive ,program files x86, same place that code blocks in saved in. It is still not working.
    You likely need it where your EXE is saved instead of where the CB exe is!
    The other likely location is where the your CB project is saved.

    Tim S.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  6. #6
    Registered User
    Join Date
    Jun 2014
    Posts
    8
    Can I ask a favor and have someone run this program with a text file to see if it actually works. The text file just has to be a semtence.

  7. #7
    Registered User
    Join Date
    Jun 2014
    Posts
    8
    Do you happen to know where I might have saved my EXE ? Sorry I am new to computer programming. I really appreciate the help.

  8. #8
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    While your problem can most likely be fixed by taking the advice in post #3 there are several steps you can take to determine where your text file should be located and also give you a better idea of why the file opening failed.

    Probably the easiest solution would be to open a file for writing that has an unique file name because by default opening a file for writing has fewer chances of failure. If the file opens correctly then you will need to use your operating system's find functionality to locate exactly where this uinque file is located. You then place your file in that directory. Normally when running the program in Code::Blocks the default directory is the directory that contains the project files.

    And when you check that the file properly opened you can use perror() to possibly tell you why the file failed to open.

    Jim

  9. #9
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Look for where your project is saved. This path can be found in your project settings configuration.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  10. #10
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    While your problem can most likely be fixed by taking the advice in post #3 there are several steps you can take to determine where your text file should be located and also give you a better idea of why the file opening failed.

    Probably the easiest solution would be to open a file for writing that has an unique file name because by default opening a file for writing has fewer chances of failure. If the file opens correctly then you will need to use your operating system's find functionality to locate exactly where this uinque file is located. You then place your file in that directory. Normally when running the program in Code::Blocks the default directory is the directory that contains the project files.

    And when you check that the file properly opened you can use perror() to possibly tell you why the file failed to open.

    Jim

  11. #11
    Registered User
    Join Date
    Mar 2012
    Location
    the c - side
    Posts
    373
    Ok , i've noticed that even after the need for the .txt extension in your filename is sorted, the program is also missing an important line. The code below is still pretty "basic" and will only allow you to read the first line of the file but will at least give you the chance to experiment a bit.

    Code:
    #include<stdio.h>
    #include<string.h>
    
    int main(void)
    {
        int shift, i;
        char code[30];
        FILE*fp;
    
        fp=fopen("caesarText.txt","r");
    
        if(fp ==NULL)
        {
            printf("Error opening input file \"caesarText\"!\n");
        }
        else
        {
            printf("Enter how much you would like to shift the text: ");
            scanf("%d", &shift);
            fgets(code,30,fp);          // important line missing in original code
    
            for(i=0;i < strlen(code);i++)
            {
                if(code[i]==' ')
                {
    
                }
                else
                {
                    if (code[i]>='x')
                    {
                        code[i]=code[i]-26;
                    }
                    code[i]=code[i]+shift;
                }
            }
    
            printf("Your encrypted text is: %s", code);
    
        }
        getchar();
        return 0;
    }

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, 04: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. Caesar Cipher
    By blacknapalm in forum C Programming
    Replies: 8
    Last Post: 11-13-2008, 12:11 AM
  5. caesar cipher help.
    By stormfront in forum C Programming
    Replies: 36
    Last Post: 11-22-2005, 08:45 PM