-3

我编写了一个程序来读取和打印用户选择的文本文件。我现在希望程序加密文件中的文本,并让用户能够选择字母移位。有人可以告诉我怎么做吗?这是我到目前为止所拥有的:

#include <stdio.h>

int main(void) {
    FILE *file_in;
    char filename[20];
    char ch;
    int shift;
    // file_in is the name given to the stream. filename will be the file that the user chooses. ch is the characters in the file.


    printf("What file do you want to open?: ");
    gets(filename);
    file_in=fopen(filename, "r");
    //ask the user to enter the name of a file

    if(file_in==NULL)
        printf("Error! File did not open.\n");
    //print message if the file can not be found

    while((ch=fgetc(file_in)) != EOF) 
        putchar(ch);
        //prints the results on the screen and shows the end of the file
    fclose(file_in);
    //closes stream
}`enter code here`
4

1 回答 1

1

对于简单的字母移位,将移位存储为 anchar并像从用户输入中读取文件名一样读取它。替换putchar(ch);putchar(ch+shift);加密和ch-shift解密或 vv。ch+shift如果总和大于 a 的最大值,则会静默溢出char,从而保证每个字母ch都有一个“加密”对应。

于 2013-01-30T14:11:29.793 回答