[英]Goto line number , and edit a string there in file using C++
I am trying to make a function which gets two parameters one Line_number and 2nd a string. 我正在尝试使一个函数获取两个参数,一个是Line_number,第二个是字符串。 This function will open a File Pre-Populated and skip till Line "Line_number" is reached , get that line and post some other line instead.But it does not work.
此功能将打开一个预填充的文件,并跳过直到达到“ Line_number”行,获取该行并发布其他行。但是,此功能不起作用。 I have seen other solutions but cant find right solution.
我看过其他解决方案,但找不到正确的解决方案。 Kindly help me with it.
请帮助我。
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);
}
In main: 在主要方面:
Inoutput_file.open("example.txt");
BackPatch(3,"Print this to Line 3 instead");
Inoutput_file.close();
I would Also Like to Mention here that I have opened same File with different instance of "ifstream" to work for rest of my program. 我还要在此提及,我已经打开了具有“ ifstream”不同实例的同一个文件,以供我的程序其余部分使用。
fstream Inoutput_file; fstream Inoutput_file; void BackPatch(int GotoLineNo, string LineNo) {
void BackPatch(int GotoLineNo,字符串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
}
} }
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.