在此函數中,我需要替換輸入文件中的所有字符,例如輸入另一個字符,例如a
。一個i
。我已經給了它兩個鏡頭,但是因爲我是新手,現在想我的大腦甚至不能提供任何建議?從文件中讀取並交換C++中的某個字符
void swapping_letter()
{
ifstream inFile("decrypted.txt");
char a;
char b;
string line;
if (inFile.is_open())
{
while (!inFile.eof())
{
getline(inFile,line);
}
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;
replace(line.begin(),line.end(),a,b);
inFile<<line
inFile.close();
}
else
{
cout<<"Please run the decrypt."<<endl;
}
}
或:
void swapping_letter()
{
ifstream inFile("decrypted.txt");
char a;
char b;
if (inFile.is_open())
{
const char EOL = '\n';
const char SPACE = ' ';
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;
vector<char> fileChars;
while (inFile.good())
{
char c;
inFile.get(c);
if (c != EOL && c != SPACE)
{
fileChars.push_back(c);
}
replace(fileChars.begin(),fileChars.end(),a,b);
for(int i = 0; i < fileChars.size(); i++)
{
inFile<<fileChars[i];
}
}
}
else
{
cout<<"Please run the decrypt."<<endl;
}
}
我沒有這樣做? – Dom
你的權利,我可以替換字符,但是當我試着寫代碼糟透了,生病看看它謝謝! – Dom