473,761 Members | 4,739 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Search for a string backwards in a file.

SK
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.
Jul 22 '05 #1
15 3619

"SK" <sk******@redif fmail.com> wrote in message
news:83******** *************** ***@posting.goo gle.com...
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.


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
Jul 22 '05 #2
SK wrote:

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.
--
Karl Heinz Buchegger
kb******@gascad .at
Jul 22 '05 #3

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

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.


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
Jul 22 '05 #4
John Harrison wrote:

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

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.


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.


:-)
It's a tricky thing which could be solved with letting the reads overlap:

+---------------------------+
+---------------------------+

| |
Overlapping area, large enough that the pattern will fit
into it.

But there should be left something to think about for the OP.

The best thing the OP could do is: Avoid that topic at all by redesigning
the file format.

--
Karl Heinz Buchegger
kb******@gascad .at
Jul 22 '05 #5
How about reversing "ABC" and doing a plain search?

Henrik Vallgren

"SK" <sk******@redif fmail.com> skrev i meddelandet
news:83******** *************** ***@posting.goo gle.com...
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.

Jul 22 '05 #6
SK
"John Harrison" <jo************ *@hotmail.com> wrote in message news:<2j******* *****@uni-berlin.de>...
"SK" <sk******@redif fmail.com> wrote in message
news:83******** *************** ***@posting.goo gle.com...
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.
Is the file small enough to read the whole file into memory?


No, it goes in several mbs.
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.

Wish I could redesign the file format, but can't actually :-(
But what I am parsing are timestamps in a file. I need to know the
first and the last timestamp in a given file.
And no there is no quick trick to do this.


Thanks.
Jul 22 '05 #7

"SK" <sk******@redif fmail.com> wrote in message
news:83******** *************** ***@posting.goo gle.com...
"John Harrison" <jo************ *@hotmail.com> wrote in message

news:<2j******* *****@uni-berlin.de>...
"SK" <sk******@redif fmail.com> wrote in message
news:83******** *************** ***@posting.goo gle.com...
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.


Is the file small enough to read the whole file into memory?


No, it goes in several mbs.
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.


Wish I could redesign the file format, but can't actually :-(
But what I am parsing are timestamps in a file. I need to know the
first and the last timestamp in a given file.


Then Karl's suggestion is the right one, read blocks of data from the end of
a file, and search backwards though each block, repeat if you don't find
anything, and watch out for the timestamp falling half way between two
blocks. In practise this means that the blocks have to overlap sufficiently
so that the timestamp will always be contained entirely within one block.

john
Jul 22 '05 #8
"SK" <sk******@redif fmail.com> wrote in message
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.
This approach seems fine to me. Others suggested reading the file into
blocks of strings. But I'm not sure if there's a need to do this as the
default fstreams already read the file into blocks of strings (though the
standard requires them to, but they do for file streams). As for how they
normally handle reading from the end of the file, I don't know for sure, but
I think it's like this: when the user reads the last character, then read
the last N chars into memory where N is the size of a block, position the
streambuf's get pointer to the last character, return the character at the
get pointer.
file.get(c);
file.seekg(-2, std::ios::cur);


The above might be more clearly expressed with

c = file.peek();
file.seekg(-1, std::ios::cur);

Also, since you're looking for a string of length 3, you could save the last
3 chars read into variables like c1, c2, c3. Or even an array, vector, etc.
Then even if you encounter an 'A' you could ignore it if the next characters
are not 'BC'.

Finally, to get improved performance you could work directly with the
underlying streambuf. Call file.rdbuf() to get a pointer to the streambuf.
It will really be a filebuf, but pretend you don't know this. To position
the stream to the beginning or end use pubseekpos. To position the stream
relative to the current position use pubseekoff. To get the current
character as in peek use sgetc.

If this is not fast enough, you could even write your own class derived from
filebuf that implements a sbumpcminus() function that gets the current
character then decrements the get pointer. Use setg to set the get pointers
and range of the get area. But this is getting really advanced.

peek at the current character
Jul 22 '05 #9
SK wrote:
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.


You can do something like this.

std::string toFind = "ABC";
std::string reversed;
std::copy(toFin d.rbegin(), toFind.rend(),s td::front_inser ter(reversed));

std::ifstream ifs("MyFile.txt ", std::ios::in);
if(ifs)
{
std::string fileContent = std::string(
std::istreambuf _iterator<char> (ifs),
std::istreambuf _iterator<char> ());

std::string::si ze_type found_position = fileContent .rfind(reversed );
if(std::string: :npos != found_position)
std::cout<<"Req uested string found at position:
"<<found_positi ons<<std::endl;
}

(Forgive any typos...)

JLR
Jul 22 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
2214
by: Sharif T. Karim | last post by:
I am trying to do the following with my search script that looks for records in a mysql table. The following is an example of what I am trying to do. Text being searched: -- The brown fox jumped over the green fence then jumped into the web monitor. It was hurt so it jumped backwards and fell on its! -- The word we're searching for "web".
5
2183
by: Chris R. | last post by:
I'm trying to do something relatively simple - find the offset of the first - or next - occurance of a string in a file, ideally in a case- insensitive way. I've seen a few solutions that seemed to involve loading the whole file as a string, but that seems to be limiting the size of the file stream that could be handled. Is there a way to do searching with just the stream? Or do i need to start loading the file into string buffers of...
6
44105
by: Neil Patel | last post by:
I have a log file that puts the most recent record at the bottom of the file. Each line is delimited by a \r\n Does anyone know how to seek to the end of the file and start reading backwards?
6
2805
by: mandibdc | last post by:
I need to extract some elements from a very large XML file. Because of the size, I'd like to work with it on my Linux machine as a text file. Basically, I am going to have a list of specific strings I'm searching for. For each string, I need to search through the XML file, and when I find that string (in the tag <code>), copy the entire <item> XML element that the code appears in, into another text file. The XML document is comprised...
1
2723
by: Eric | last post by:
Hi: I have two files. I search pattern ":" from emails text file and save email contents into a database. Another search pattern " field is blank. Please try again.", vbExclamation + vbOKOnly Me.txtEmail.SetFocus Exit Sub End If Me.txtStatusBar.Value = "Parsing..." strEmail = Me.txtEmail.Value
4
3381
by: Dameon | last post by:
Hi All, I have a process where I'd like to search the contents of a file(in a dir) for all occurences (or the count of) of a given string. My goal is to focus more on performance, as some of the files could be upwards of 25mb in size and time is important. I don't want to take the route of loading the text of the file into a giant string and searching it, but would rather focus on a performance-minded solution. Any sugesstions for a...
4
2171
by: BenCoo | last post by:
Hello, In a Binary Search Tree I get the error : Object must be of type String if I run the form only with the "Dim bstLidnummer As New BinarySearchTree" it works fine. Thanks for any help on this, Benny
4
1760
by: Russell Mangel | last post by:
Hi, The code I have posted searches for a pattern of bytes starting from the end of Byte array *backwords*, if a match is found, return the starting index of those found bytes. Since one of the parameters is starting index you can start the search anywhere within the Byte you are searching.
0
10781
Debadatta Mishra
by: Debadatta Mishra | last post by:
Introduction In this article I will provide you an approach to manipulate an image file. This article gives you an insight into some tricks in java so that you can conceal sensitive information inside an image, hide your complete image as text ,search for a particular image inside a directory, minimize the size of the image. However this is not a new concept, there is a concept called Steganography which enables to conceal your secret...
0
9554
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9377
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10136
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9989
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9925
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
6640
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5405
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3913
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2788
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.