Szyfrowanie tekstu - nieoczekiwane dodawanie liter do tekstu

0

Witam! Pisząc szyfrator tekstu, mam problem <znów> ;) Przy próbie zaszyfrowania obojętnie jaką metodą na końcu szyfrowanego tekstu zawsze zostają dodane jakieś litery zbędne. Zauważyłem, że to nawet nie problem szyfrowania. Przy samym zapisie tekstu do pliku następuje podwojenie ostatniego znaku w wyrazie. Kod:

int EncryptPass() //szyfrowanie
{
    char letter;
    stringstream stream;
    string password;
    fstream filei("password.txt", fstream::in);

    system("CLS");
    while(!filei.eof())
    {
        filei >> letter;

        //tu algorytm Cezara:
        //letter -= 97;
        //if ((letter >= 0) && (letter <= 26)) letter = (letter + 3) % 26;
        //letter += 97;

        stream << letter;
    }
    filei.close();

    fstream fileo("password.txt", fstream::out | fstream::trunc);

    stream >> password;
    fileo << password;
    fileo.close();
    cout << "Zaszyfrowano!";
    cin.ignore();
    cin.get();
}

int DecryptPass() //odszyfrowanie
{
    char letter;
    stringstream stream;
    string password;
    fstream filei("password.txt", fstream::in);

    system("CLS");
    while(!filei.eof())
    {
        filei >> letter;

        //tu algorytm Cezara:
        //letter -= 97;
        //if((letter >= 0) && (letter <= 26)) letter = (letter + 26 - 3) % 26;
        //letter += 97;

        stream << letter;
    }
    filei.close();

    fstream fileo("password.txt", fstream::out | fstream::trunc);

    stream >> password;
    fileo << password;
    fileo.close();
    cout << "Odszyfrowano!";
    cin.ignore();
    cin.get();
}

Jak widać, odczytuje znak po znaku dopóki nie będzie końca pliku i wczytuje do stringstream, a potem z ss do string i do pliku. Proszę o wskazanie błedy, który powoduje dodawanie niepożądanych liter.

0
#include <iostream>
#include <fstream>
#include <string>
using namespace std;

void EncryptPass() //szyfrowanie
  {
   char letter;
   string password;
   fstream file("password.txt",ios::in); 
   while(file>>letter)
     {
        //tu algorytm Cezara:
        //letter -= 97;
        //if ((letter >= 0) && (letter <= 26)) letter = (letter + 3) % 26;
        //letter += 97;
      password+=letter;
     }
   file.close();
   file.clear();
   file.open("password.txt",ios::out|ios::trunc);
   file<<password;
   file.close();
  }

int main()
  {
   EncryptPass();
   cout<<"Zaszyfrowano!";
   cin.sync();
   cin.get();
   return 0;
  }

1 użytkowników online, w tym zalogowanych: 0, gości: 1