Pass File As Command Line Argument

泄露秘密 提交于 2019-12-31 04:33:05

问题


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:


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!



来源:https://stackoverflow.com/questions/22822393/pass-file-as-command-line-argument

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!