我有一個功能,可以在一個文件中同時交換兩個字符,但是如果我嘗試多次使用該函數,則前一個交換將會從文本文件和原始文本中刪除現在回來了,因此第二個變化看起來是我的第一個變化。我該如何解決這個問題?保持文本文件不被擦除,而是保持寫入的能力? C++
void swapping_letters()
{
ifstream inFile("decrypted.txt");
ofstream outFile("swap.txt");
char a;
char b;
vector<char> fileChars;
if (inFile.is_open())
{
cout<<"What is the letter you want to replace?"<<endl;
cin>>a;
cout<<"What is the letter you want to replace it with?"<<endl;
cin>>b;
while (inFile.good())
{
char c;
inFile.get(c);
fileChars.push_back(c);
}
replace(fileChars.begin(),fileChars.end(),a,b);
}
else
{
cout<<"Please run the decrypt."<<endl;
}
for(int i = 0; i < fileChars.size(); i++)
{
outFile<<fileChars[i];
}
}
能看到烏爾着落,但我現在運行於低的時候,我的編譯器不喜歡我在做什麼,呃,那麼只是把代碼放在程序中而不是有一個函數,我猜可以把所有我需要的字母一次換掉,而不是每次寫入文件,謝謝! – Dom