在文件中向后搜索字符串。 [英] Search for a string backwards in a file.

查看:71
本文介绍了在文件中向后搜索字符串。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嘿伙计们,


我在文件中向后搜索一个字符串(比如说ABC)。

首先我寻求结束。

然后我试着做一张支票 -


做{

file.clear();

file.get(c);

file.seekg(-2,std :: ios :: cur);

} while(c!=' 'A''&& c.readback()!='''B''&& c.readback!='''C'')

//回读,hypotheticl func,这里的问题)

是否有一些像偷看的功能/技巧可以反向读取

一次一个字符?

或者有没有更好的方法来解决这个问题。


谢谢。

Hey folks,

I am searching for a string (say "ABC") backwards in a file.
First I seek to the end.
Then I try to make a check like -

do {
file.clear ();
file.get(c);
file.seekg(-2, std::ios::cur);
} while (c != ''A'' && c.readback() != ''B'' && c.readback != ''C'')
// readback, hypotheticl func, problem here)
Is there some function/trick like peek that can instead read backwards
one character at a time?
Or is there a better way to solve this problem.

Thank you.

推荐答案



SK < SK ****** @ rediffmail.com>在消息中写道

news:83 ************************** @ posting.google.c om ...

"SK" <sk******@rediffmail.com> wrote in message
news:83**************************@posting.google.c om...
嘿伙计们,

我正在文件中向后搜索一个字符串(比如说ABC)。
首先我寻求结束。然后我尝试做一个检查 -

做{
file.clear();
file.get(c);
file.seekg( -2,std :: ios :: cur);
} while(c!=''A''&& c.readback()!='''B''&& c.readback !='''C'')
//回读,hypotheticl func,问题在这里)
是否有一些像peek一样的函数/技巧可以反而向后读取一个字符?<或者有没有更好的方法来解决这个问题。

谢谢。
Hey folks,

I am searching for a string (say "ABC") backwards in a file.
First I seek to the end.
Then I try to make a check like -

do {
file.clear ();
file.get(c);
file.seekg(-2, std::ios::cur);
} while (c != ''A'' && c.readback() != ''B'' && c.readback != ''C'')
// readback, hypotheticl func, problem here)
Is there some function/trick like peek that can instead read backwards
one character at a time?
Or is there a better way to solve this problem.

Thank you.




文件小到足以阅读整个归档到内存?如果是这样,那么将整个文件读成一个字符串,然后搜索不在文件中的字符串。

其他任何东西都会变得非常复杂,而且效率也不是很高。

文件不是为了向后阅读而设计的,我宁愿重新设计你的

文件格式,这样你就不需要向后阅读而不是实际

尝试这个。


并且没有快速的技巧可以做到这一点。


john



Is the file small enough to read the whole file into memory? If so then read
the whole file into a string and search in the string not in the file.
Anything else rapidly gets very complicated and also not terribly efficient.
Files aren''t designed to be read backwards, I would prefer to redesign your
file format so that you don''t need to read backwards than to actually
attempt this.

And no there is no quick trick to do this.

john


SK写道:

嘿伙计们,

我正在寻找一个字符串(比如说ABC)向后一个文件。
首先我寻求结束。
然后我尝试做一个像 -

做{
file.clear();
file.get(c);
file.seekg(-2,std :: ios :: cur);
} while(c!=''A''&& c.readback ()!=''B''&& c.readback!='''C'')
//回读,hypotheticl func,问题在这里)
有没有像peek这样的函数/技巧可以反而向后读取一个角色一次?


No.

或者有更好的方法可以解决这个问题。

Hey folks,

I am searching for a string (say "ABC") backwards in a file.
First I seek to the end.
Then I try to make a check like -

do {
file.clear ();
file.get(c);
file.seekg(-2, std::ios::cur);
} while (c != ''A'' && c.readback() != ''B'' && c.readback != ''C'')
// readback, hypotheticl func, problem here)
Is there some function/trick like peek that can instead read backwards
one character at a time?
No.
Or is there a better way to solve this problem.




Seek到底。


寻找一些角色

将一些字符读入缓冲区。

搜索缓冲区从最后开始。


如果找到模式 - >罚款

如果你找不到模式:在缓冲区中找回一个数字

的字符,检查缓冲区

并重复。

-

Karl Heinz Buchegger
kb *** ***@gascad.at




" Karl Heinz Buchegger" < KB ****** @ gascad.at>在消息中写道

新闻:40 *************** @ gascad.at ...

"Karl Heinz Buchegger" <kb******@gascad.at> wrote in message
news:40***************@gascad.at...
SK写道:

嘿伙计们,

我正在文件中向后搜索一个字符串(比如说ABC)。
首先我寻求结束。<然后我尝试做一个检查 -

做{
file.clear();
file.get(c);
file.seekg (-2,std :: ios :: cur);
} while(c!=''A''&& c.readback()!=''B''&& c。回读!='''C'')
//回读,hypotheticl func,问题在这里)
是否有一些像peek一样的函数/技巧可以反而向后读取一个字符?

Hey folks,

I am searching for a string (say "ABC") backwards in a file.
First I seek to the end.
Then I try to make a check like -

do {
file.clear ();
file.get(c);
file.seekg(-2, std::ios::cur);
} while (c != ''A'' && c.readback() != ''B'' && c.readback != ''C'')
// readback, hypotheticl func, problem here)
Is there some function/trick like peek that can instead read backwards
one character at a time?



没有。



No.

或者有更好的方法来解决这个问题。
Or is there a better way to solve this problem.



寻求回到最后。

找回一些字符
将一些字符读入缓冲区。
从最后搜索缓冲区。

如果你找到了模板 - >好的
如果你找不到这种模式:在缓冲区中找回一些字符,检查缓冲区
然后重复。



Seek to the end.

Seek back a number of characters
Read a number of characters into a buffer.
Search that buffer from the end.

If you find the pattern -> fine
If you don''t find the pattern: seek back a number
of characters into the buffer, check the buffer
and repeat.



并且不要忘记处理你正在寻找的字符串的情况

来跨越你的一个寻找位置。即如果你不小心,一个

一半的字符串最终在一个缓冲区中而另一半在另一个

缓冲区中,所以你永远找不到它。 />

john



And don''t forget to deal with the case where the string you are searching
for straddles one of your seek positions. I.e. if you aren''t careful, one
half of the string ends up in one buffer and the other half in another
buffer, so you never find it.

john


这篇关于在文件中向后搜索字符串。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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