Hi,
I'm trying to figure out how to read some range of rows from a
file.
Is it possible to search the file with some criteria and then
when the search string is found read 3 rows before and after
that row from where the search string was found ?
Of course the row where the string was found should also be read.
I have a log file in txt format. I need to read the file if
there's any errors. There's two lines that has error information
if something goes wrong. I need to get the errors and the command
that caused the error which is about three rows above the first
error line. I have to use the error as a search string because
the command changes everytime so I can't use the command as a
search string.
The error lines are as follows:
/*** SYNTAX ERROR ***/
/*** INVALID PARAMETER NAME ***/
I'm using /*** as a search string.
I could use seekg if I just needed to read the file forward from
the point where the search string was found, but I can't figure
how I could do what I'm trying to do here.
I hope you understand what I'm trying to do here. If not, I'll
try to explain more.
----
mkarja 11 2611
"mkarja" <mm**********@h otmail.com> wrote in message
news:75******** *************** ***@posting.goo gle.com... Hi,
I'm trying to figure out how to read some range of rows from a file. Is it possible to search the file with some criteria and then when the search string is found read 3 rows before and after that row from where the search string was found ? Of course the row where the string was found should also be read.
The problem is the reading backwards bit. Instead of trying to do this you
should keep the last three lines read in memory somewhere so that when you
do find the string you are looking for you have all the information you
need.
Something like this
string last_three_line s[3];
string curr_line;
while (getline(file, curr_line))
{
if (current line is error line)
{
...
}
last_three_line s[0] = last_three_line s[1];
last_three_line s[1] = last_three_line s[2];
last_three_line s[2] = curr_line;
}
john
"mkarja" <mm**********@h otmail.com> wrote in message
news:75******** *************** ***@posting.goo gle.com...
| Hi,
|
| I'm trying to figure out how to read some range of rows from a
| file.
| Is it possible to search the file with some criteria and then
| when the search string is found read 3 rows before and after
| that row from where the search string was found ?
| Of course the row where the string was found should also be read.
|
| I have a log file in txt format. I need to read the file if
| there's any errors. There's two lines that has error information
| if something goes wrong. I need to get the errors and the command
| that caused the error which is about three rows above the first
| error line. I have to use the error as a search string because
| the command changes everytime so I can't use the command as a
| search string.
|
| The error lines are as follows:
| /*** SYNTAX ERROR ***/
| /*** INVALID PARAMETER NAME ***/
|
| I'm using /*** as a search string.
| I could use seekg if I just needed to read the file forward from
| the point where the search string was found, but I can't figure
| how I could do what I'm trying to do here.
|
| I hope you understand what I'm trying to do here. If not, I'll
| try to explain more.
You can *seek* backwards, in the same way that
you can seek *forward* to obtain an reasonable
result:
// Approx line size in the file...
std::ifstream:: pos_type OneLine( 100 );
// Seek backwards from the current position
// in the stream...
InFile.seekg( -OneLine, std::ios_base:: cur );
....then:
while( std::getline( InFile, Buffer ) )
// ...
Cheers.
Chris Val
"Chris \( Val \)" <ch******@bigpo nd.com.au> wrote in message news:<2t******* ******@uni-berlin.de>... "mkarja" <mm**********@h otmail.com> wrote in message news:75******** *************** ***@posting.goo gle.com...
| I have a log file in txt format. I need to read the file if | there's any errors. There's two lines that has error information | if something goes wrong. I need to get the errors and the command | that caused the error which is about three rows above the first | error line. I have to use the error as a search string because | the command changes everytime so I can't use the command as a | search string.
[snip] You can *seek* backwards, in the same way that you can seek *forward* to obtain an reasonable result:
// Approx line size in the file... std::ifstream:: pos_type OneLine( 100 );
// Seek backwards from the current position // in the stream... InFile.seekg( -OneLine, std::ios_base:: cur );
...then:
while( std::getline( InFile, Buffer ) ) // ...
The typical solution is to read lines into a circular buffer with the
minimum capacity necessary. This seems to be 5 in your case: 2 lines
of interest, plus the 3 previous lines.
/david
"David Rubin" <da********@war pmail.net> wrote in message
news:82******** *************** *@posting.googl e.com...
| "Chris \( Val \)" <ch******@bigpo nd.com.au> wrote in message
news:<2t******* ******@uni-berlin.de>...
| > "mkarja" <mm**********@h otmail.com> wrote in message
| > news:75******** *************** ***@posting.goo gle.com...
|
| > | I have a log file in txt format. I need to read the file if
| > | there's any errors. There's two lines that has error information
| > | if something goes wrong. I need to get the errors and the command
| > | that caused the error which is about three rows above the first
| > | error line. I have to use the error as a search string because
| > | the command changes everytime so I can't use the command as a
| > | search string.
|
| [snip]
| > You can *seek* backwards, in the same way that
| > you can seek *forward* to obtain an reasonable
| > result:
| >
| > // Approx line size in the file...
| > std::ifstream:: pos_type OneLine( 100 );
| >
| > // Seek backwards from the current position
| > // in the stream...
| > InFile.seekg( -OneLine, std::ios_base:: cur );
| >
| > ...then:
| >
| > while( std::getline( InFile, Buffer ) )
| > // ...
|
| The typical solution is to read lines into a circular buffer with the
| minimum capacity necessary. This seems to be 5 in your case: 2 lines
| of interest, plus the 3 previous lines.
Yes, of course :-)
I thought it was pretty clear from the above
that one would therefore write something like:
std::ifstream:: pos_type Previous( OneLine * 3 );
InFile.seekg( -Previous, std::ios_base:: cur );
// Read '5' lines into an buffer, or deal with
// each one individually...
Cheers.
Chris Val
> Yes, of course :-) I thought it was pretty clear from the above that one would therefore write something like:
std::ifstream:: pos_type Previous( OneLine * 3 ); InFile.seekg( -Previous, std::ios_base:: cur );
// Read '5' lines into an buffer, or deal with // each one individually...
Cheers. Chris Val
I've tried your examples but I can't get them to work.
VC++ 6 won't compile them.
Here's my original code that gets only the two error lines.
/*** SYNTAX ERROR ***/
/*** INVALID PARAMETER NAME ***/
============ code start =============== ======
ifstream infile("paramet ers.txt");
if (!infile) return (1);
bool done=false;
char str[1000];
std::string commandResponse ;
while (!done && infile.good()){
infile.getline( str,100-1);
if (strstr(str,"/***")){
commandResponse += str;
infile.seekg(1, ios::cur);
infile.getline( str,100-1);
commandResponse += str;
commandResponse += "\n";
}
}
infile.close();
============ code ends =============== ======
Could you be so kind to help me out getting your code
inserted so that it would work.
I've included almost everything I can think of that would
help, but no use.
#include <iostream.h>
#include <fstream.h>
#include <string>
#include <vector>
I've tried including iostream & fstream without the .h too but
it didn't help. The compiler just won't compile. It either says
that there's too few parameters or cannot convert paramater 1
or 2 to something...
I've tried to dabble with this for a while now and I really need
to get it working. It would be nice if I could just fiddle with
this code as long as it takes for me to get it working, but I'm
in a bit of a hurry so I'll take any help I can get.
Thanks.
----
mkarja
"Chris \( Val \)" <ch******@bigpo nd.com.au> wrote in message news:<2t******* ******@uni-berlin.de>... "David Rubin" <da********@war pmail.net> wrote in message news:82******** *************** *@posting.googl e.com... | "Chris \( Val \)" <ch******@bigpo nd.com.au> wrote in message news:<2t******* ******@uni-berlin.de>... | > "mkarja" <mm**********@h otmail.com> wrote in message | > news:75******** *************** ***@posting.goo gle.com...
| > | I have a log file in txt format. I need to read the file if | > | there's any errors. There's two lines that has error information | > | if something goes wrong. I need to get the errors and the command | > | that caused the error which is about three rows above the first | > | error line. I have to use the error as a search string because | > | the command changes everytime so I can't use the command as a | > | search string.
[snip] | The typical solution is to read lines into a circular buffer with the | minimum capacity necessary. This seems to be 5 in your case: 2 lines | of interest, plus the 3 previous lines.
Yes, of course :-)
I thought it was pretty clear from the above that one would therefore write something like:
std::ifstream:: pos_type Previous( OneLine * 3 ); InFile.seekg( -Previous, std::ios_base:: cur );
The problem is here [pasted from your previous post]
| > // Approx line size in the file... | > std::ifstream:: pos_type OneLine( 100 );
This is a rather brittle solution.
/david
"mkarja" <mm**********@h otmail.com> wrote in message
news:75******** *************** ***@posting.goo gle.com...
|> Yes, of course :-)
| >
| > I thought it was pretty clear from the above
| > that one would therefore write something like:
| >
| > std::ifstream:: pos_type Previous( OneLine * 3 );
| > InFile.seekg( -Previous, std::ios_base:: cur );
| >
| > // Read '5' lines into an buffer, or deal with
| > // each one individually...
| >
| > Cheers.
| > Chris Val
|
| I've tried your examples but I can't get them to work.
| VC++ 6 won't compile them.
| Here's my original code that gets only the two error lines.
|
| /*** SYNTAX ERROR ***/
| /*** INVALID PARAMETER NAME ***/
[snip]
I don't have VC++ installed at the moment, but even then,
without the appropriate error messages, we would only be
guessing at the problem.
Please post the exact messages emitted by the compiler.
Fwiw, did you use an appropriate namespace qualification ?
Cheers.
Chris Val
> [snip] Ok, try this:
Given the following data file:
XXXXXXXXXXXXXXX XXXXXXXXXXXXXXX XXXX XXXXXXXXXXXXXXX XXXXXXXXXXXXXXX XXXX XXXXXXXXXXXXX BEFORE XXXXXXXXXXXXX XXXXXXXXXXXXXXX XXXXXXXXXXXXXXX XXXX XXXXXXXXXXX TEST LINE XXXXXXXXXXXX XXXXXXXXXXXXXXX XXXXXXXXXXXXXXX XXXX XXXXXXXXXXXXX AFTER XXXXXXXXXXXXXX XXXXXXXXXXXXXXX XXXXXXXXXXXXXXX XXXX XXXXXXXXXXXXXXX XXXXXXXXXXXXXXX XXXX XXXXXXXXXXXXXXX XXXXXXXXXXXXXXX XXXX
# include <iostream> # include <fstream> # include <ostream> # include <string> # include <cstddef>
int main() { std::ifstream InFile( "100000.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::np os ) break; }
InFile.seekg( -OneLine*3, std::ios_base:: cur );
std::size_t NrLines( 6 ); while( std::getline( InFile, Buffer ) && --NrLines > 0 ) std::cout << Buffer << std::endl;
return 0; }
-- PRODUCES THE FOLOWING OUTPUT --
XXXXXXXXXXXXX BEFORE XXXXXXXXXXXXX XXXXXXXXXXXXXXX XXXXXXXXXXXXXXX XXXX XXXXXXXXXXX TEST LINE XXXXXXXXXXXX XXXXXXXXXXXXXXX XXXXXXXXXXXXXXX XXXX XXXXXXXXXXXXX AFTER XXXXXXXXXXXXXX
HTH. Chris Val
Thanks a million. This works just as I needed.
Three hip, hip, hurraahs to you :)
----
mkarja
> 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 :)
----
mkarja This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Kevin T. Ryan |
last post by:
Hi All -
I'm not sure, but I'm wondering if this is a bug, or maybe (more
likely) I'm misunderstanding something...see below:
>>> f = open('testfile', 'w')
>>> f.write('kevin\n')
>>> f.write('dan\n')
>>> f.write('pat\n')
>>> f.close()
|
by: Will McGugan |
last post by:
Hi,
I'm writing an app that downloads images. It rejects images that are
under a certain size - whithout downloading them completely. I've
implemented this using PIL, by downloading the first K and trying to
create a PIL image with it. PIL raises an exception because the file is
incomplete, but the image object is initialised with the image
dimensions, which is what I need. It actualy works well enough, but I'm
concerened about...
|
by: Phil Slater |
last post by:
I'm trying to process a collection of text files, reading word by word. The
program run hangs whenever it encounters a word with an accented letter
(like rôle or passé) - ie something that's not a "char" with an ASCII code
in 0..127
I've searched the ANSI C++ standard, the internet and various text books,
but can't see how to workaround this one. I've tried wchar_t and wstring
without success.
But rather than spending lots of time on...
|
by: mail2atulmehta |
last post by:
Hi,
I do not know if this is right place for this, but i need some help. I
have a text file, whose values are seprated by a delimiter. I want to
open this file in excel, ( not import it) . I have written the driver
prg in c#. The code opens the file, but it is not writting the values
from text file into excel file. I can not figure out the problem here.
This is my code:
StreamReader dataFileReader = null;
FileInfo file = null;...
|
by: John Dann |
last post by:
I'm trying to read some binary data from a file created by another
program. I know the binary file format but can't change or control the
format. The binary data is organised such that it should populate a
series of structures of specified variable composition.
I have the structures created OK, but actually reading the files is
giving me an error. Can I ask a simple question to start with:
I'm trying to read the file using the...
| |
by: fool |
last post by:
Dear group,
I am a beginner in php and I was little bit experience in C language.
I
want to read a file's content and delete the first line of the file and
display those lines which has got deleted.
The content of the external file , which is in .cvs format is:
-------------------------------------------------------------------
|
by: laredotornado |
last post by:
Hi,
I'm using PHP 4.4.4 on Apache 2 on Fedora Core 5. PHP was installed
using Apache's apxs and the php library was installed to
/usr/local/php. However, when I set my "error_reporting" setting to be
"E_ALL", notices are still not getting reported. The perms on my file
are 664, with owner root and group root. The php.ini file is located
at /usr/local/lib/php/php.ini. Any ideas why the setting does not seem
to be having an effect? ...
|
by: Tyler |
last post by:
Hello All:
After trying to find an open source alternative to Matlab (or IDL), I
am currently getting acquainted with Python and, in particular SciPy,
NumPy, and Matplotlib. While I await the delivery of Travis Oliphant's
NumPy manual, I have a quick question (hopefully) regarding how to read
in Fortran written data.
The data files are not binary, but ASCII text files with no formatting
and mixed data types (strings, integers,...
|
by: Man4ish |
last post by:
I have one pblm for reading a file randomly for searching the value with in given range.
e.g.
offset allele id
19 G/T 2066803
20 C/T 2066804
13 A/G 2066805
12 A/G 2066927
In the above file i have to search all the records which are in range (10,15). If i read whole file sequentially it will consume lot of time since data file is around 1GB. So I am planning...
|
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,...
|
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...
| |
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,...
|
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...
|
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...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
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
| |
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...
| |