Pass File As Command Line Argument

2019-02-28 20:35发布

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.

1条回答
男人必须洒脱
2楼-- · 2019-02-28 21:18

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!

查看更多
登录 后发表回答