转到行号,并使用 C++ 在文件中编辑一个字符串
Goto line number , and edit a string there in file using C++
我正在尝试创建一个函数,它获取两个参数,一个 Line_number 和第二个字符串。此函数将打开一个文件预填充并跳到行 "Line_number" 到达,获取该行和 post 一些其他行 instead.But 它不起作用。我见过其他解决方案,但找不到正确的解决方案。请帮助我。
fstream Inoutput_file;
void BackPatch(int GotoLineNo, string LineNo)
{
for (int i = 1; i <= GotoLineNo; i++)
{
char* str = new char[20];
Inoutput_file.getline(str, 20, '\n');
if (i == LineNo-1)
{
Inoutput_file.seekp(Inoutput_file.tellg());
Inoutput_file.getline(str, 20, '\n');
cout << str << endl; //prints the correct line
Inoutput_file << "Goto " + str <<endl; //donot work
cout << "Goto " + str<<endl; // Prints correct line
}
}
Inoutput_file.seekp(0);
Inoutput_file.seekg(0);
}
主要内容:
Inoutput_file.open("example.txt");
BackPatch(3,"Print this to Line 3 instead");
Inoutput_file.close();
我还想在这里提一下,我已经用不同的 "ifstream" 实例打开了相同的文件,以便为我的程序的其余部分工作。
fstream Inoutput_file;
void BackPatch(int GotoLineNo, string LineNo)
{
for (int i = 1; i <= GotoLineNo; i++)
{
char* str = new char[20];
Inoutput_file.getline(str, 20, '\n');
if (i == LineNo-1)
{
Inoutput_file.seekp(Inoutput_file.tellg());
Inoutput_file.getline(str, 20, '\n');
cout << str << endl; //prints the correct line
Inoutput_file << "Goto " + str <<endl; //Now it will work fine
cout << "Goto " + str<<endl; // Prints correct line
}
}
我正在尝试创建一个函数,它获取两个参数,一个 Line_number 和第二个字符串。此函数将打开一个文件预填充并跳到行 "Line_number" 到达,获取该行和 post 一些其他行 instead.But 它不起作用。我见过其他解决方案,但找不到正确的解决方案。请帮助我。
fstream Inoutput_file;
void BackPatch(int GotoLineNo, string LineNo)
{
for (int i = 1; i <= GotoLineNo; i++)
{
char* str = new char[20];
Inoutput_file.getline(str, 20, '\n');
if (i == LineNo-1)
{
Inoutput_file.seekp(Inoutput_file.tellg());
Inoutput_file.getline(str, 20, '\n');
cout << str << endl; //prints the correct line
Inoutput_file << "Goto " + str <<endl; //donot work
cout << "Goto " + str<<endl; // Prints correct line
}
}
Inoutput_file.seekp(0);
Inoutput_file.seekg(0);
}
主要内容:
Inoutput_file.open("example.txt");
BackPatch(3,"Print this to Line 3 instead");
Inoutput_file.close();
我还想在这里提一下,我已经用不同的 "ifstream" 实例打开了相同的文件,以便为我的程序的其余部分工作。
fstream Inoutput_file; void BackPatch(int GotoLineNo, string LineNo) {
for (int i = 1; i <= GotoLineNo; i++)
{
char* str = new char[20];
Inoutput_file.getline(str, 20, '\n');
if (i == LineNo-1)
{
Inoutput_file.seekp(Inoutput_file.tellg());
Inoutput_file.getline(str, 20, '\n');
cout << str << endl; //prints the correct line
Inoutput_file << "Goto " + str <<endl; //Now it will work fine
cout << "Goto " + str<<endl; // Prints correct line
}
}