Jump to content

Check out our Community Blogs

Register and join over 40,000 other developers!

Recent Status Updates

View All Updates

- - - - -

Problem with fstream variables.

encryption stream

  • Please log in to reply
1 reply to this topic

#1 KeilanS


    CC Addict

  • Just Joined
  • PipPipPipPipPip
  • 127 posts

Posted 16 November 2008 - 09:58 PM

In the below function, after infile.eof() becomes true, it stays true. So the embedded while loop only goes once.

I thought that using infile.seekg(0, ios::beg); would set infile.eof() to false against, but it isn't working that way.

Any help would be appreciated.

//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; //An 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         
      cout << newCode;
      infile.seekg (0, ios::beg);
      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       
         cout << i << " " << found << endl;
         if (found != string::npos){
            return i;} //Return key value if word is found                      

  • 0

#2 dcs


    CC Devotee

  • Just Joined
  • PipPipPipPipPipPip
  • 730 posts

Posted 17 November 2008 - 09:09 AM

Dinkumware, Ltd. - Compleat Libraries Reference

ifstream::open(), close(), and EOF - comp.lang.c++ | Google Groups
  • 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