逐行尝试搜索并替换文件中的一行 [英] trying seekp line by line and replace a line in a file

查看:88
本文介绍了逐行尝试搜索并替换文件中的一行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我的问题是我似乎无法弄清楚如何'设置tellp()'到特定行的开头然后用新的替换它。我也刚刚意识到我还必须弄清楚如何避免写下任何一行。我应该能够解决这个问题。这就是我到目前为止所尝试的



So my problem is that I can't seem to figure out how to 'set tellp()' to the beginning of a particular line and then replace it with a new one. I've also just realised that I'm going to have to also figure out how to avoid writing over any of the next line too.. I should probably be able to figure that out though. This is what I've tried so far

std::fstream indiLine;
indiLine.open ("indiTest.mdl",std::ios::in | std::ios::out); //| std::ios::app);
if (indiLine.is_open())
{
   std::string inOut;
   int line_num = 0;
   //find line
   while (getline(indiLine,inOut))
   {
         if (line_num-1 == vNum) //stop at a particular line of text denoted by vNum
         {  std::cout << inOut << std::endl ;
            break; }
         line_num++;
   }
   indiLine.seekp(indiLine.tellg());
   indiLine << "R";
   indiLine.close();
}
else{ debugOut("Save Failure On Open",500); }





和我测试的特定线路的输出:





and the output for the particular lines I've tested:

引用:

在子程序之前;

X200Y200Z200E8D5

X-200Y200Z200E1D5 子程序后
;

X200Y200Z20RE8D5

X-200Y200ZR00E1D5

before subroutine;
X200Y200Z200E8D5
X-200Y200Z200E1D5
after subroutine;
X200Y200Z20RE8D5
X-200Y200ZR00E1D5



这些是第8行和第9行的' vNum'是5,然后是6,所以它们应该是第6和第7行。或者更确切地说,那些是我追求的,在这些特殊情况下。

所以tellg()给了我错误的位置..如果它是在行的最后我可以做的类似


and those are the 8th and 9th lines when 'vNum' is at 5 and then 6 so they should be the 6th and 7th lines. Or rather those are the ones I'm after, in these particular instances.
so tellg() is giving me the wrong position.. If it was at the end of the line I could have just done something like

indiLine.seekp(indiLine.tellg-inOut.length());



但它是不是这样,我真的不知道该怎么做。另外,我似乎不太可能从'tellg()'返回的类型中扣除'int'。

任何关于如何解决这个问题的提示真的很棒,如果我从完全错误的角度去做,请让我知道你的想法,甚至更好。谢谢你们,你们都很棒。 :)


but it's not so I don't really know what to do. Also it seems likely to me that I won't be able to just deduct an 'int' from the type that 'tellg()' returns.
Any hints on what to do to solve it this way would be really great and if I'm going at it from the completely wrong angle let me know what you have in mind, even better. Thanks guys, you're all awesome. :)

推荐答案

最直接且相对安全的行编辑文件的方法是:



打开源文件

打开目标文件

从源文件依次读取每一行:

- 如果它不是正确的行那么将该行写入目标文件

- 如果它是您要丢弃的行并将新行复制到目标文件



删除源文件

重命名目标文件



这样做的好处是,如果出现严重错误,不会丢失任何数据!如果它变成梨状,一个例外就会让你走出循环,一切都会被整齐地关闭和收拾整齐。它可能有点慢,但我个人更有可能在任何一天快速纠正。
The most direct and relatively safe way of line editing a file is to:

Open your source file
Open a destination file
Read each line in turn from the source file:
- if it's not the correct line then write the line to the destination file
- if it's the line you want discard it and copy the new line to the destination file

Delete the source file
Rename the destination file

This has the advantage of not losing you any data if something goes horribly wrong! If it goes pear shaped an exception carries you out of the loop, everything gets closed and tidied up neatly. It can be a bit slow but personally I'll take a greater chance of being correct over fast any day.


这篇关于逐行尝试搜索并替换文件中的一行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆