// Input the plaintext in the array
	char d;
	string temp;
	ifstream myfile1 ("Plaintext.txt");
	 if (myfile1.is_open())
	  {	 do 
                     {
			for ( j=0; j<BC; j++) // BC=4
			for ( i=0; i < 4; i++)
			{	myfile1.get(d);
				a[i][j] = d; // plaintext				
		      }	
                         Encrypt(a, rk);	

                     /*
                            strcpy(temp,a);
                            // Code to write to the file       
                     */
// Now i have to save the encrypted text in a file but with all the text in the plaintext file i.e //the file has more than 16 bytes.. One method to do this is save the 16 bytes encrypted //text as a string but saving consecutive 16 bytes till end of file.. 	
		 
            }while(!myfile1.eof());

			myfile1.close();			
	  }
	  else 
		cout << "Unable to open file";

Can anyone help me how to change the code so that it can copy the encrypted text consecutively till the end of the file..

You need to clarify the problem a bit. Is it your intent to overwrite the original plaintext in the file with the encrypted text? In that case, you'll have to open the file as an "fstream" with both in and out modes. You will have to keep track of where you are in the file and move the pointer back and forth.

Why not simply write to a different (temp) file, then when all the text is encrypted, overwrite the original? Or store the encrypted data in a temp array, writing it to the source file when done?

Lastly, strcpy( temp, a); is not likely to work, unless you can ensure there's a zero in the byte that follows the array.

No in fact i need to encrypt the whole file: plaintext.txt and then save it in a file named ciphertext.txt
Ya i want to store the encrypted data in a temp array & then write it to the source file: ciphertext.txt when done.
Please help me out how we can store the encrypted text i.e the text is being encrypted 16 bytes @ a time each until end of file and then i have to copy the whole ciphertext in a temporary array and then save it in the file ciphertext.txt when done...

If you're saving the encrypted text to a different file, then just open up the output file and as you finish encryping a block, write it out. I'm not seeing the problem. After you encrypt one 16 byte block, do you do any further processing to it?

No there is no further processing to the 1 encrypted 16 bytes block of data..
But when the next 16 bytes are encrypted and saved in the file: ciphertext.txt it is going to overwrite the 1st 16 encrypted data..
So how can we write the whole encrypted data to the file ciphertext.txt...

ifstream input( "plain.txt" );
ofstream output( "cipher.txt" );
int r, c;

//test for successful file openings.

//read in a block, storing to the 4x4 array, encipher it

//this fills in the line 16-19 area of your first posting
for (r = 0; r < 4; r++ )
   for(c = 0; c < 4; c++ )
       output << ar[r][c];
//end output block.  next output will follow what was just writen

As long as you do not close the output file, you will continue to add on to the end of it. Just as, while you read from the input, you keep reading from successive locations.

Ok thanks vmanes that work out...
In fact I had closed the file after 1 encrypted 16 bytes block of data.. thats whys the previous 16 bytes being encrypted and saved in the file was being replaced by the last 16 bytes of data..
Thanks again..

Hello the program is able to encrypt the data in the file plaintext.txt till eof but now the last 16 bytes is being encrypted twice..and saved in the file ciphertext.txt
E.g:-

plaintext.txt:- aaaaaaaaaaaaaaaabbbbbbbbbbbbbbbb
Text.txt:- aaaaaaaaaaaaaaaabbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb

text.txt = file to save the decrypted data

The input of the plaintext that is being copied in the array a is taking the last 16 bytes of data twice..
Please help me out.. how to solve the problem..

char d;
	ifstream myfile1 ("Plaintext.txt");
	ofstream myfile3 ("Ciphertext.txt");
	 	
		if (myfile1.is_open())
		{
			while(!myfile1.eof())
			{	
				for ( j=0; j<BC; j++)
				for ( i=0; i < 4; i++)
				{	
					myfile1.get(d);
					a[i][j] = d; // plaintext				
					cout<<a[i][j]; // to chk how many plaintext is copied
				 }	

				Encrypt(a, rk);

				if (myfile3.is_open())
				{
					for(j=0; j< BC; j ++)
					for ( i=0; i<4; i++)
					  myfile3<<a[i][j];				
				}
				else 
					 cout << "Unable to open file";	
			}					
			
			myfile3.close();
		}	 
		else 
		  cout << "Unable to open file";
		myfile1.close();

What you've got is a classic case of improper loop control using eof( ).

Remember that eof( ) only tells you you've reached end of data when you've attempted to read past the end. It's like walking towards a cliff, at night, with a blindfold on. You don't know you've reached it until you take one step to far (and fall over the edge!).

Consider this smaller sample:

int num;
int total = 0;
ifstream fin ("nums.txt");

while( !fin.eof( ) )
{
    fin >> num;
    cout << num << " ";
    total += num;
}
cout << total << endl;

If the file is dead empty, you still enter the loop because you don't know that. Input gets nothing, you output some random value, add that junk to total. Not good.

If there is data, you read numbers, adding, until the very last. Input stops at the first non-interger looking thing on the input stream, so we get the last number and halt. Display it, add it, go back to top of loop. End of file has not yet been set, so we enter the loop again. The input now attempts to read past end of file, nothing there. EOF gets set, and nothing new is stored to num (it holds the previously read value.) Display the old value, add it in again, then the loop control will halt the process.
This is essentially what's happening in your program.

First, you are working on the premise that data will always exist in 16 character chunks. If this is a guaranteed condition, how about this minor change:

myfile1.get(d);  //priming read

while(!myfile1.eof())
{	
    for ( j=0; j<BC; j++)
        for ( i=0; i < 4; i++)
        {	
            a[i][j] = d; // plaintext				
            cout<<a[i][j]; // to chk how many plaintext is copied
            myfile1.get(d);  //reads first element of next block
        }	

    Encrypt(a, rk);

    if (myfile3.is_open())
    {
        for(j=0; j< BC; j ++)
            for ( i=0; i<4; i++)
                myfile3<<a[i][j];				
    }
    else 
        cout << "Unable to open file";	
}

This reads one time outside the loop - so if there is no data, you never enter the loop. The read in the inner loop is moved to the bottom, so what you end up with is attempting to read the first char of the next 16-char block. If you've just finished the last block, this then will go past the end, set EOF, and your main loop will exit correctly.

Someone is bound to say using .eof() is not a good way to control loops. In this case of character by character input, it's not soooo bad. When you get to dealing with string input, it can be troublesome. Many suggest the better approach is to either put the input statement as the condition in your loop (the input operation "returns" a non-0 or 0 value indicating successful input or failure, respectivley) or to use the .fail( ) method of the input object (which, as with .eof() requires a priming read and read at end of loop.) Either of these methods correctly handle the case of input file ending without a final newline.

There ends today's lesson. It's Saturday, I gotta go play.
Val

Thanks vmanes again for the help and the explainations you have stated..
It was great to seek help from you... mostly the cliff example you have stated...wow..
Anyways you have solved my problem...great..
God bless you..man..

This question has already been answered. Start a new discussion instead.