dimanche 20 janvier 2019

having trouble understanding IO directory and pipe line command

im trying to encrypt the f1.txt file by shifting the letter based off the number and write it to f2.txt. but im not sure if the contents of f1.txt go into ./cf

$ cat 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();

}

Aucun commentaire:

Enregistrer un commentaire