[英]replacing string based on user input c++
我想接收來自用戶的輸入並在文件中搜索該輸入。 當我找到包含該特定單詞的行時,我想打印它並獲取另一個輸入以根據第二個用戶輸入和第三個用戶輸入更改該行的一部分。 (我正在編寫一個醫院管理應用程序,這是患者和編輯他們的文檔的項目的一部分)。 我完成了 90% 的項目,但我不知道如何替換它。 查看以下代碼:
#include <iostream>
#include <stream>
#include <string.h>
#include <string>
using namespace std;
int main(){
string srch;
string line;
fstream Myfile;
string word, replacement, name;
int counter;
Myfile.open("Patientlist.txt", ios::in|ios::out);
cout << "\nEnter your Name: ";
cin.ignore();
getline(cin, srch);
if(Myfile.is_open())
{
while(getline(Myfile, line)){
if (line.find(srch) != string::npos){
cout << "\nYour details are: \n" << line << endl << "What do you want to change? *type it's word and then type the replacement!*" << endl;
cin >> word >> replacement;
}
// i want to change in here
}
}else
{
cout << "\nSearch Failed... Patient not found!" << endl;
}
Myfile.close();
}
例如,我的文件包含這一行( David, ha, 2002 )並且用戶想要將 2002 更改為 2003
嘗試這個
#include <iostream>
#include <fstream>
#include <string.h>
#include <string>
using namespace std;
int main(){
string srch;
string line, line2;
fstream Myfile;
string word, replacement, name;
int counter;
Myfile.open("Patientlist.txt", ios::in);
cout << "\nEnter your Name: ";
cin.ignore();
getline(cin, srch);
if(Myfile.is_open())
{
while(getline(Myfile, line)){
if (line.find(srch) != string::npos){
cout << "\nYour details are: \n" << line << endl << "What do you want to change? *type it's word and then type the replacement!*" << endl;
cin >> word >> replacement;
int index = line.find(word);
if (index != string::npos){
Myfile.close();
Myfile.open("Patientlist.txt", ios::out);
line.replace(index, word.length(), replacement);
Myfile.write(line.data(), line.size());
Myfile.close();
}
}
// i want to change in here
}
}else
{
cout << "\nSearch Failed... Patient not found!" << endl;
}
}
首先 header 應該是<fstream>
而不是<stream>
我在文件處理方面也沒有太多經驗,但我讀到你不能同時讀寫,所以在重新打開文件進行寫入之前我已經關閉了。
然后,可以更新您的line
,然后將其寫入文件,而不是更新文件中的文本。
一種可能的方法:將文件讀入“line”變量后關閉文件,然后:
std::replace(0, line.length(), "2002", "2003")
然后覆蓋舊文件。 請注意,std::replace 與 string::replace 不同!
您不能直接替換文件中的字符串。 你必須:
理想情況下,重命名部分應該一步完成。 例如,您不希望最終沒有文件,因為原始文件已被刪除,但臨時文件由於某些錯誤未重命名 - 請參閱您的操作系統文檔。
這是一個想法:
#include <iostream>
#include <fstream>
#include <string>
#include <cstdio>
using namespace std;
void replace(string& s, const string& old_str, const string& new_str)
{
for (size_t off = 0, found_idx = s.find(old_str, off); found_idx != string::npos; found_idx = s.find(old_str, off), off += new_str.length())
s.replace(found_idx, old_str.length(), new_str);
}
int main()
{
const char* in_fn = "c:/temp/in.txt";
const char* bak_fn = "c:/temp/in.bak";
const char* tmp_fn = "c:/temp/tmp.txt";
const char* out_fn = "c:/temp/out.txt";
string old_str{ "2002" };
string new_str{ "2003" };
// read, rename, write
{
ifstream in{ in_fn };
if (!in)
return -1; // could not open
ofstream tmp{ tmp_fn };
if (!tmp)
return -2; // could not open
string line;
while (getline(in, line))
{
replace(line, old_str, new_str);
tmp << line << endl;
}
} // in & tmp are closed here
// this should be done in one step
{
remove(bak_fn);
rename(in_fn, bak_fn);
remove(out_fn);
rename(tmp_fn, in_fn);
}
return 0;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.