473,770 Members | 2,153 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
15 3623
sk******@rediff mail.com (SK) wrote:
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 -
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.


it goes in several mbs.

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.


If the file is in ascii format (ie. it is made up of lines), then
you can read forwards a line at a time (it won't take long even to
parse several megs that way).
If you really must start at the end, then start, say, 50k from the
end and find the first newline, and then read forwards from there
(assuming your file does not contain lines longer than 50k).
Jul 22 '05 #11
Karl Heinz Buchegger wrote:
<snip>
The best thing the OP could do is: Avoid that topic at all by redesigning
the file format.


This comment really has no value to the discussion because it assumes that: the
OP has the liberty to arbitrarily redesign the file format, that the
requirements for the existing file format are secondary to the (code)
implementation details, and that reading from the end of the file isn't a
suitable solution to the problem as expressed by the OP.
Jul 22 '05 #12
Julie wrote:

Karl Heinz Buchegger wrote:
<snip>
The best thing the OP could do is: Avoid that topic at all by redesigning
the file format.


This comment really has no value to the discussion because it assumes that: the
OP has the liberty to arbitrarily redesign the file format, that the
requirements for the existing file format are secondary to the (code)
implementation details, and that reading from the end of the file isn't a
suitable solution to the problem as expressed by the OP.


On the other hand it happens quite frequently, that posters post
questions about how to do something and it turns out later that
a simple change in the assignment (if possible) is the far better
solution.

--
Karl Heinz Buchegger
kb******@gascad .at
Jul 22 '05 #13
Karl Heinz Buchegger wrote:

Julie wrote:

Karl Heinz Buchegger wrote:
<snip>
The best thing the OP could do is: Avoid that topic at all by redesigning
the file format.


This comment really has no value to the discussion because it assumes that: the
OP has the liberty to arbitrarily redesign the file format, that the
requirements for the existing file format are secondary to the (code)
implementation details, and that reading from the end of the file isn't a
suitable solution to the problem as expressed by the OP.


On the other hand it happens quite frequently, that posters post
questions about how to do something and it turns out later that
a simple change in the assignment (if possible) is the far better
solution.


Perhaps you should phrase your original comment as a question, rather than an
assertion.
Jul 22 '05 #14
why just reverse the string and search it in the normal way?:)

"John Harrison" <jo************ *@hotmail.com> дÈëÓʼþ
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? 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 #15
Hardy wrote:

why just reverse the string and search it in the normal way?:)


Because it isn't very efficient to start searching a 10 MB file
at the beginning, when all you are interested in, is the *last*
occourence of the search string and you know that this
search string is located somewhere in the last 100 Bytes or so.
--
Karl Heinz Buchegger
kb******@gascad .at
Jul 22 '05 #16

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
2184
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
44109
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
3383
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
9591
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
9425
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
10228
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
9869
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8883
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6676
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
5312
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
3970
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
2816
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.