Re: Reading a file from a specified range
From: Chris \( Val \) (chrisval_at_bigpond.com.au)
Date: 10/26/04
- Next message: Richard Herring: "Re: Getting the type of a variable?"
- Previous message: Adrian: "Re: a+b+c is not equal to a+(b+c)"
- In reply to: mkarja: "Re: Reading a file from a specified range"
- Next in thread: mkarja: "Re: Reading a file from a specified range"
- Reply: mkarja: "Re: Reading a file from a specified range"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: Tue, 26 Oct 2004 18:04:37 +1000
"mkarja" <mmmc_reptail@hotmail.com> wrote in message
news:75a5303b.0410250056.594a3426@posting.google.com...
|>
| > Thanks a million. This works just as I needed.
| > Three hip, hip, hurraahs to you :)
| >
| > ----
| > mkarja
|
| Or at least I thought it worked as I needed.
| I was under the impression that the log file would be pretty
| much the same allways. It turned out it isn't. I had looked at
| several log files and they all were the same, except the command
| and error messages, so I thought they all were basically the
| same. The command that produces the error may be, like I said
| earlier, three lines above the first error message or it may be
| four rows above it. It might even be five or two or whatever
| rows above it, I don't know.
| So the example given by Chris won't work correctly if the row is
| more than three rows above the error message. I've modified it so
| that I can get the command even if it's four rows above the error
| but this way if the command is perhaps two rows above the error
| then there will be some unnecessary rows in the result if I go
| up four rows.
| Is it possible to do the example that Chris gave so that when
| the /*** is found it would search backwards looking for some
| character instead of going backwards some defined number of rows.
| In this case the character would be < character, because the row
| that the command is starts with the < character.
|
| Thanks for your help so far and if you can help me with this I
| think I'll be allright for a while, until I run into another
| wall and need some help :)
You know, programmers are supposed to actually program :-)
Even if you don't have any code, at minimum you could have
posted an small example of the log file, with dummy data in
the place of the real data, if it's sensitive, just like I
have done here. It's not hard is it ?
Combine the following, with what I have already shown you:
-- TestFile.txt --
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXX@OUT OF BOUNDS@XXXXXXXXX
XXXXXXX@One@XXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXX@Two@XXXXX
XXXXXXXXXXXXXX@MIDDLE XXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXX TEST LINE XXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXX SECTION@ XXXXXXXXXXX
XXXXX@Hello@XXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXX@World@XXXXXXXXXX
XXXXXXXXXX@OUT OF BOUNDS@XXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
# include <iostream>
# include <fstream>
# include <ostream>
# include <string>
# include <cstddef>
int main()
{
std::ifstream InFile( "TestFile.txt",
std::ios_base::binary );
std::string Buffer;
std::ifstream::pos_type OneLine( 36 );
while( std::getline( InFile, Buffer ) )
{
if( Buffer.find( "TEST LINE" ) != std::string::npos )
break;
}
InFile.seekg( -OneLine*5, std::ios_base::cur );
std::size_t NrLines( 0 );
while( InFile.ignore( INT_MAX, '@' ) &&
std::getline( InFile, Buffer, '@' ) && ++NrLines < 6 )
std::cout << "Line (" << NrLines << ") "
<< Buffer << std::endl;
return 0;
}
-- OUTPUT --
Line (1) One
Line (2) Two
Line (3) MIDDLE XXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXX TEST LINE XXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXX SECTION
Line (4) Hello
Line (5) World
This is only a guide, because you have failed
to provide enough information for anyone in the
group to give you any conclusive answer or help.
In addition to what you now know, add to your
toolkit the functions such as 'std::istream::get()'
and 'std::istream::peek()' to fine tune your code,
and narrow down the unwanted characters.
Since you cannot actually read backwards from the
stream, you'll need to 'seek' back to some desired
location as we have done previously, and then read
some lump of it into an buffer, concatenating as you
go, until you get what you need for later processing.
You could then place that buffer into an 'std::stringstream',
which would allow you to search just about any which way you
like, forwards and backwards, to come up with a *possible*
further reduction in junk characters.
You could also throw another 'find()' member function
in the second loop, but I'll leave that you you :-)
Is this closer to what you're after ?
Cheers.
Chris Val
- Next message: Richard Herring: "Re: Getting the type of a variable?"
- Previous message: Adrian: "Re: a+b+c is not equal to a+(b+c)"
- In reply to: mkarja: "Re: Reading a file from a specified range"
- Next in thread: mkarja: "Re: Reading a file from a specified range"
- Reply: mkarja: "Re: Reading a file from a specified range"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|