473,395 Members | 1,742 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

fstream pointer arithmetic

Hi

ofter I need to get to a line in a text file just before a line with a
known string. but by the time getline ( ifs, line ) gets to the line
with the knows string, the last line as already passed.
sure I can remember the line and then while( getline .. till it flags
then recall last remembered, but I was thinking, is there a method by
which one can move getline back one line,
filestream_ pointer--;
kind of an increment?

thanks
Feb 14 '07 #1
4 2179
Gary Wessle wrote:
Hi

ofter I need to get to a line in a text file just before a line with a
known string. but by the time getline ( ifs, line ) gets to the line
with the knows string, the last line as already passed.
sure I can remember the line and then while( getline .. till it flags
then recall last remembered, but I was thinking, is there a method by
which one can move getline back one line,
filestream_ pointer--;
kind of an increment?

thanks
The only facilities for doing something like that are the tellg and
seekg member functions, which will tell you the position in the stream,
and move you to a position in the stream.

In any case, a solution based on tellg/seekg is going to be way more
complex than just remembering the previous line. And likely to be much
less efficient too since you have to reread the previous line.

Using swap, you can remember the previous line without much overhead.
Maybe something like:

template <typename Predicate>
std::string find_previous_line(std::istream & in, Predicate matches)
{
std::string line, previous_line ;
while (std::getline(in, line))
{
if (matches(line))
return previous_line ;
line.swap(previous_line) ;
}

return "" ;
}

--
Alan Johnson
Feb 14 '07 #2
On 14 feb, 06:05, Alan Johnson <a...@yahoo.comwrote:
Gary Wessle wrote:
Hi
ofter I need to get to a line in a text file just before a line with a
known string. but by the time getline ( ifs, line ) gets to the line
with the knows string, the last line as already passed.
sure I can remember the line and then while( getline .. till it flags
then recall last remembered, but I was thinking, is there a method by
which one can move getline back one line,
filestream_ pointer--;
kind of an increment?
thanks

The only facilities for doing something like that are the tellg and
seekg member functions, which will tell you the position in the stream,
and move you to a position in the stream.

In any case, a solution based on tellg/seekg is going to be way more
complex than just remembering the previous line. And likely to be much
less efficient too since you have to reread the previous line.

Using swap, you can remember the previous line without much overhead.
Maybe something like:

template <typename Predicate>
std::string find_previous_line(std::istream & in, Predicate matches)
{
std::string line, previous_line ;
while (std::getline(in, line))
{
if (matches(line))
return previous_line ;
line.swap(previous_line) ;
}

return "" ;

}

--
Alan Johnson
swap() does look like an overhead to me, since there is no reason to
put the content of previous_line into line.

line.swap(previous_line);

could better be written as

previous_line = line;

Feb 14 '07 #3
jamx wrote:
On 14 feb, 06:05, Alan Johnson <a...@yahoo.comwrote:
>Gary Wessle wrote:
Hi
ofter I need to get to a line in a text file just before a line with a
known string. but by the time getline ( ifs, line ) gets to the line
with the knows string, the last line as already passed.
sure I can remember the line and then while( getline .. till it flags
then recall last remembered, but I was thinking, is there a method by
which one can move getline back one line,
filestream_ pointer--;
kind of an increment?
thanks

The only facilities for doing something like that are the tellg and
seekg member functions, which will tell you the position in the stream,
and move you to a position in the stream.

In any case, a solution based on tellg/seekg is going to be way more
complex than just remembering the previous line. And likely to be much
less efficient too since you have to reread the previous line.

Using swap, you can remember the previous line without much overhead.
Maybe something like:

template <typename Predicate>
std::string find_previous_line(std::istream & in, Predicate matches)
{
std::string line, previous_line ;
while (std::getline(in, line))
{
if (matches(line))
return previous_line ;
line.swap(previous_line) ;
}

return "" ;

}

--
Alan Johnson

swap() does look like an overhead to me, since there is no reason to
put the content of previous_line into line.

line.swap(previous_line);

could better be written as

previous_line = line;
It depends. I could easily imagine an implementation where swap() is faster.

Feb 14 '07 #4
Rolf Magnus wrote:
jamx wrote:

>>On 14 feb, 06:05, Alan Johnson <a...@yahoo.comwrote:
>>>Gary Wessle wrote:

Hi

ofter I need to get to a line in a text file just before a line with a
known string. but by the time getline ( ifs, line ) gets to the line
with the knows string, the last line as already passed.
sure I can remember the line and then while( getline .. till it flags
then recall last remembered, but I was thinking, is there a method by
which one can move getline back one line,
filestream_ pointer--;
kind of an increment?

thanks

The only facilities for doing something like that are the tellg and
seekg member functions, which will tell you the position in the stream,
and move you to a position in the stream.

In any case, a solution based on tellg/seekg is going to be way more
complex than just remembering the previous line. And likely to be much
less efficient too since you have to reread the previous line.

Using swap, you can remember the previous line without much overhead.
Maybe something like:

template <typename Predicate>
std::string find_previous_line(std::istream & in, Predicate matches)
{
std::string line, previous_line ;
while (std::getline(in, line))
{
if (matches(line))
return previous_line ;
line.swap(previous_line) ;
}

return "" ;

}

--
Alan Johnson

swap() does look like an overhead to me, since there is no reason to
put the content of previous_line into line.

line.swap(previous_line);

could better be written as

previous_line = line;


It depends. I could easily imagine an implementation where swap() is faster.
I would expect swap to be at least as fast, but 'previous_line = line;'
expresses the intent more clearly, so I would prefer it.

john
Feb 15 '07 #5

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

Similar topics

2
by: Maya | last post by:
Apart from being a pointer, what would be the benefit of using 'std::filebuf' than using the std::fstream? As far as I can see, I would use the same methods in 'filebuf' that I would when using...
6
by: Francois Grieu | last post by:
Are these programs correct ? #include <stdio.h> unsigned char a = {1,2}; int main(void) { unsigned char j; for(j=1; j<=2; ++j) printf("%u\n", *( a+j-1 )); return 0; }
7
by: barikat | last post by:
int a; int *Ptr1, *Ptr2; Ptr1 = a; Ptr1++; Ptr2 = a; printf("Ptr1 : %p\n", Ptr1); printf("Ptr2 : %p\n\n", Ptr2);
26
by: Bill Reid | last post by:
Bear with me, as I am not a "professional" programmer, but I was working on part of program that reads parts of four text files into a buffer which I re-allocate the size as I read each file. I...
19
by: =?iso-8859-1?b?VG9t4XMg0yBoyWlsaWRoZQ==?= | last post by:
Coming originally from C++, I used to do the likes of the following, using a pointer in a conditional: void Func(int *p) { if (p) { *p++ = 7; *p++ = 8;
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
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,...
0
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...
0
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...
0
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,...

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.