首页 > 解决方案 > 无法理解 IO 目录和管道命令

问题描述

我试图通过根据数字移动字母并将其写入 f2.txt 来加密 f1.txt 文件。但我不确定 f1.txt 的内容是否进入 ./cf

$ 猫 f1.txt | ./cf -e 3 > f2.txt

int main(int argc, char ** argv){
char alpha[26] = {'A','B','C','D','E','F','G','H','I','J','K','L', // alphabet arr
'M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z'};
//ifstream input(argv[0]);
string line = "";
if(input.fail()){
    cout << "error opening file "<<endl;
    exit(1);
}
string type = argv[1];
if(type == "-e"){
    while(getline(input,line)){
        char plain [line.length()]; // array to store plain text
        strcpy(plain, line.c_str()); //putting input into char array
        string cipher_text = ""; // variable to store plain text
        int shift = atoi(argv[2]);
        for(int x = 0; x < line.length(); x++){ //for loop runs through char array
                                                                                        //and add plain letter based on shift
            if((char(plain[x]) >= 65) && (char(plain[x]) <= 90)){;
                cipher_text += cipher_letter(alpha,shift,string(1,plain[x]));
            }else
                cipher_text += plain[x];
        }
            cout << cipher_text << endl;
    }
}else if(type == "-d"){
    while(getline(input,line)){
        char cipher [line.length()]; // array to store plain text
        strcpy(cipher, line.c_str()); //putting input into char array
        string plain_text = ""; // variable to store plain text
        int shift = atoi(argv[2]);
        for(int x = 0; x < line.length(); x++){ //for loop runs through char array
                                                                                        //and add plain letter based on shift
            if((char(cipher[x]) >= 65) && (char(cipher[x]) <= 90)){
                plain_text += plain_letter(alpha,-shift,string(1,cipher[x]));
            }else
                plain_text += cipher[x];
        }
                cout<< plain_text << endl;
    }
}
input.close();

}

标签: c++11unix

解决方案


推荐阅读