C++ 搜索和替换循环运行次数过多
我的申请有这个问题。当我的应用程序运行8-13次而不是1次时 我的程序的主要目的是从带有字符串stremp2的filein中搜索一行,将其加载到临时字符串中,并用字符串stremp2+\t\t+test替换它以进行文件输出 我现在生成的输出文件是: 我要生成的文件c:/hospitaldata/PatientDatabaseTEST.txtC++ 搜索和替换循环运行次数过多,c++,loops,while-loop,text-files,nested-loops,C++,Loops,While Loop,Text Files,Nested Loops,我的申请有这个问题。当我的应用程序运行8-13次而不是1次时 我的程序的主要目的是从带有字符串stremp2的filein中搜索一行,将其加载到临时字符串中,并用字符串stremp2+\t\t+test替换它以进行文件输出 我现在生成的输出文件是: 我要生成的文件c:/hospitaldata/PatientDatabaseTEST.txt 如果要循环一次,则必须在If条件中设置一个中断 while (filein >> strTemp) { if (strT
如果要循环一次,则必须在If条件中设置一个中断
while (filein >> strTemp)
{
if (strTemp == strReplace){
strTemp = strNew;
break; // breaking the loop
//found = true;
}
strTemp += "\n";
fileout << strTemp;
//if(found) break;
}
循环的每个周期都会写入输出文件,而不仅仅是在找到匹配项时。这应该起作用:
string strReplace = strTemp2;
string strNew = strTemp2+"\t\tTest";
ifstream filein("c:/hospitaldata/PatientDatabase.txt"); //File to read from
ofstream fileout("c:/hospitaldata/PatientDatabaseTEST.txt"); //Temporary file
if (!filein || !fileout)
{
cout << "Error opening files!" << endl;
return 1;
}
string strTemp;
filein.getline(strTemp);
fileout << strTemp << std::endl;
//bool found = false;
while (filein >> strTemp)
{
if (strTemp == strReplace){
strTemp = strNew;
strTemp += "\n";
fileout << strTemp;
//found = true;
}
//if(found) break;
}
读台词,而不是单词:
#include <iostream>
#include <sstream>
using namespace std;
void markTest(istream & input, ostream & output, const string & pattern) {
string strTemp;
while (std::getline(input, strTemp))
{
if (strTemp == pattern){
strTemp += "\t\tTest";
}
output << strTemp << "\n";
if (!output) {
break;
}
}
}
int main() {
istringstream input(
"CPR Patient nr Værelse nr Seng nr Medicin\n"
"140143211 256 6 5");
markTest(input, cout, "140143211 256 6 5");
return 0;
}
循环结束后,只需写入输出文件一次?strTemp2的值是多少?每次从输入中读取一个字时,输出的数据可能重复。12字,12行。strTemp2有以下值:140143211256 6 5来自问题中所需的输出。我认为OP也希望输出第一行。是的,我希望,这将是重要的。我编辑了我的代码,以包括事先添加第一行。请使用编辑的代码重试。它现在使用getline读取第一行,应该可以了。呜呜,很抱歉。名字应该在第一行。我现在没有访问编译器的权限,所以我不能亲自测试代码。如果我是对的,输出现在是预定义的。可能不是。数字140143211256 6 5是可更改的。@Andrew,markTest的输出肯定不是预定义的。它接受任何类型的作为参数,您可以自由地提供filestream作为参数。您还可以添加第四个参数以附加到找到的字符串。
CPR Patient nr Værelse nr Seng nr Medicin
140143211 256 6 5 Test
while (filein >> strTemp)
{
if (strTemp == strReplace){
strTemp = strNew;
break; // breaking the loop
//found = true;
}
strTemp += "\n";
fileout << strTemp;
//if(found) break;
}
string strReplace = strTemp2;
string strNew = strTemp2+"\t\tTest";
ifstream filein("c:/hospitaldata/PatientDatabase.txt"); //File to read from
ofstream fileout("c:/hospitaldata/PatientDatabaseTEST.txt"); //Temporary file
if (!filein || !fileout)
{
cout << "Error opening files!" << endl;
return 1;
}
string strTemp;
filein.getline(strTemp);
fileout << strTemp << std::endl;
//bool found = false;
while (filein >> strTemp)
{
if (strTemp == strReplace){
strTemp = strNew;
strTemp += "\n";
fileout << strTemp;
//found = true;
}
//if(found) break;
}
#include <iostream>
#include <sstream>
using namespace std;
void markTest(istream & input, ostream & output, const string & pattern) {
string strTemp;
while (std::getline(input, strTemp))
{
if (strTemp == pattern){
strTemp += "\t\tTest";
}
output << strTemp << "\n";
if (!output) {
break;
}
}
}
int main() {
istringstream input(
"CPR Patient nr Værelse nr Seng nr Medicin\n"
"140143211 256 6 5");
markTest(input, cout, "140143211 256 6 5");
return 0;
}