1

My program is supposed to read an encrypted file from the command-line, but I don't know how to pass command-line arguments. These are the instructions:

*A shift cipher is a very basic cryptographic algorithm in which encryption is performed by substituting each character in the plaintext with the character that's a fixed number of characters (i.e. the shift value) later in the alphabet. For example, if our shift value is 2, the plaintext cabbage becomes ecddcig.

It's easy to see that shift ciphers are so weak because there are only 26 possible ways to shift (and one of those 26 is the same as not shifting at all). Your program should read at the command line the name of a file that has been encrypted with a shift cipher. It will decrypt the file using all of the possible shift values and then deciding which of the shift values is correct. The shift value that the program decides is correct is the one which, when applied, results in the highest percentage of the file's words appearing in the dictionary. *

I've written functions to shift the characters in a string by n, a function to determine whether a given word appears in the dictionary, and a function to split a string into tokens.

4
  • You know the arguments to the main function? The argc and argv arguments I'm sure you've seen before? Those contains the arguments passed on the command line. I suggest you experiment with those, printing out the values of argc, as well as loop from zero to argc and print out the corresponding entry in the argv array. Commented Apr 2, 2014 at 20:36
  • So if I wanted to pass it a file would I just include the file name on the command line when I am compiling the .c file? Commented Apr 2, 2014 at 20:53
  • No, you pass the file-name as argument to the created executable. Like ./my_program /some/path/to/my/file. This file-name will then be in argv[1] as a string ("/some/path/to/my/file"). Remember to check that argc is at least 2 first though. Commented Apr 2, 2014 at 21:00
  • Then if I want to manipulate the file in the program I refer to it by using argv[1] ? Commented Apr 2, 2014 at 21:10

1 Answer 1

2

In C, you can access command line arguments with argc and argv in the main function. Something like this:

int main(int argc, char *argv[]) 
{
    for (int i = 1; i < argc; i++) {
        printf("%s\n", argv[i]);
    }
}

Note that I'm starting with the second item in the argv list, as the first one is always the name of the executable itself. When called with ./program file.txt file2.txt it would print

file.txt
file2.txt

Hope that helps!

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

Comments

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.