Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
- - - - -

Passing an ifstream variable to a function?

encryption stream

  • Please log in to reply
2 replies to this topic

#1 KeilanS

KeilanS

    CC Addict

  • Just Joined
  • PipPipPipPipPip
  • 127 posts

Posted 16 November 2008 - 03:47 PM

Hey all, I am writing a code used to decode text documents using caesar encryption. The program is given an encrypted text file and a code word used in that file in plain english. From there it must find the encryption key and translate the file.

It's getting hung up at this function call:
cout << "The Caesar cipher used is "
        << findKey(infile, word);

Which links to this function:
int findKey (ifstream infile, string word){
 string buffer; //Define a string to hold each line                           
   string newCode; //Define a string to hold the converted code                 
   int found; //A int to test if the code was found                             

   for (int i = 1; i < 26; i++){ //Test input file with all keys                
      newCode = caesar(word,i);
      //Find the code word converted with each key and store as newCode         

      while( !infile.eof() ){ //Ensure file has not been read past end          
         getline (infile, buffer);
         found = buffer.find(newCode); //See if the word is in the buffer       
         if (found != string::npos){
            return i;} //Return key value if word is found                      

      }
   }
}

The error it is giving me is in the file ios_base.h, and says:
`std::ios_base::ios_base(const std::ios_base&)' is private

What am I doing wrong?

Here is the complete code if needed:
#include <iostream>
#include <string> //For the string variable                                     
#include <fstream> //To read from and to files                                  
#include <cctype> //For isupper and islower                                     
#include <cstdlib> //For exit command                                           
using namespace std;

string caesar (string, int);
int findKey (ifstream, string);

int main()
{
   //Variables declared                                                         
   string filename; //Variable for filename                                     
   string word; //Variable for the known word                                   
   ifstream infile; //Declare infile variable                                   

   cout << "Please enter the filename of the encoded message: ";
   cin >> filename;
   cout << "Please enter the known word: ";
   cin >> word;
   //The above code gets the filename and known word                            

   infile.open(filename.c_str()); //Open the inputted file                      

   if (infile.fail()){
      cout <<"File failed to open" << endl;
      exit(1); //Exits the program if file isn't found                          
   }

   cout << "The Caesar cipher used is "
        << findKey(infile, word);
   //The above code finds and outputs the key using the findKey function        

   infile.close(); //Closes input file                                          
   return 0;
}

//**************************************************************************    
//Function: caesar                                                              
//Assumption(s): A string and an int are inputted                               
//Action(s): Encrypts the string with a key of the int                          
//**************************************************************************    

string caesar (string s, int k){
   string encrypt = ""; //Declare the return variable                           
   for (int i = 0; i < s.length(); i++){ //Look at every character              
      if (isupper(s.at(i))){
         encrypt += (s.at(i)+k)%90;}
      //In the case of an uppercase letter, we need to use %90 to               
      //ensure the new letter is not greater than Z (which has value 90)        
      else if (islower(s.at(i))){
         encrypt += (s.at(i)+k)%122;}
      //In the case of a lowercase letter, we use %122 to                       
      //ensure the new letter is not greater than z (which has value 122)       
   }
   return encrypt;
}

//**************************************************************************    
//Function: findKey                                                             
//Assumption(s): An input file and a string are inputted                        
//Action(s): Outputs the encryption key using a known word                      
//**************************************************************************    

int findKey (ifstream infile, string word){
 string buffer; //Define a string to hold each line                           
   string newCode; //Define a string to hold the converted code                 
   int found; //A int to test if the code was found                             

   for (int i = 1; i < 26; i++){ //Test input file with all keys                
      newCode = caesar(word,i);
      //Find the code word converted with each key and store as newCode         

      while( !infile.eof() ){ //Ensure file has not been read past end          
         getline (infile, buffer);
         found = buffer.find(newCode); //See if the word is in the buffer       
         if (found != string::npos){
            return i;} //Return key value if word is found                      

      }
   }
}

  • 0

#2 dcs

dcs

    CC Devotee

  • Just Joined
  • PipPipPipPipPipPip
  • 730 posts

Posted 16 November 2008 - 04:00 PM

Pass the stream by reference -- you don't want to make a copy of the stream.
  • 1

#3 KeilanS

KeilanS

    CC Addict

  • Just Joined
  • PipPipPipPipPip
  • 127 posts

Posted 16 November 2008 - 04:11 PM

That seems to have fixed it. Thanks!

+rep
  • 0





Also tagged with one or more of these keywords: encryption, stream

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download