473,756 Members | 3,211 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Reading a file from a specified range

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
Jul 22 '05
11 2615

"mkarja" <mm**********@h otmail.com> wrote in message
news:75******** *************** ***@posting.goo gle.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 --
XXXXXXXXXXXXXXX XXXXXXXXXXXXXXX XXXX
XXXXXXXXXXXXXXX XXXXXXXXXXXXXXX XXXX
XXXXXXXXXX@OUT OF BOUNDS@XXXXXXXX X
XXXXXXX@One@XXX XXXXXXXXXXXXXXX XXXX
XXXXXXXXXXXXXXX XXXXXXXXX@Two@X XXXX
XXXXXXXXXXXXXX@ MIDDLE XXXXXXXXXXXX
XXXXXXXXXXXXXXX XXXXXXXXXXXXXXX XXXX
XXXXXXXXXXX TEST LINE XXXXXXXXXXXX
XXXXXXXXXXXXXXX XXXXXXXXXXXXXXX XXXX
XXXXXXXXXXXXX SECTION@ XXXXXXXXXXX
XXXXX@Hello@XXX XXXXXXXXXXXXXXX XXXX
XXXXXXXXXXXXXXX XXXXXXXXXXXXXXX XXXX
XXXXXXXXXXXXXXX XX@World@XXXXXX XXXX
XXXXXXXXXX@OUT OF BOUNDS@XXXXXXXX X
XXXXXXXXXXXXXXX XXXXXXXXXXXXXXX XXXX
XXXXXXXXXXXXXXX XXXXXXXXXXXXXXX XXXX

# include <iostream>
# include <fstream>
# include <ostream>
# include <string>
# include <cstddef>

int main()
{
std::ifstream InFile( "TestFile.t xt",
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*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
XXXXXXXXXXXXXXX XXXXXXXXXXXXXXX XXXX
XXXXXXXXXXX TEST LINE XXXXXXXXXXXX
XXXXXXXXXXXXXXX XXXXXXXXXXXXXXX XXXX
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::stringstr eam',
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
Jul 22 '05 #11
> "Chris \( Val \)" <ch******@bigpo nd.com.au> wrote in message >
news:<2u******* ******@uni-berlin.de>...

You know, programmers are supposed to actually program :-)


Heh, yeah so I've heard. I don't know where that idea is from :)
Actually I did some programming on my own (gasp) and I managed
to solve my problem. I was perhaps a bit hasty on asking for
help, but I was in a hurry to get it done. Deadline tomorrow.

Here's how I did it if anyones interested. Not sure if it's the
best way to do this, but this is how I did it anyways.

============ code start =============== ==========
std::ifstream infile( "logfile.tx t" );

std::string commandResponse ;
std::vector<std ::string> commandResponse 2;
std::string Buffer;
int i;
int j;

while( std::getline( infile, Buffer ) )
{
commandResponse 2.push_back(Buf fer);
if( Buffer.find( "/***" ) )
continue;

infile.seekg( 1, std::ios_base:: cur );
std::getline( infile, Buffer );
break;
}

int respSize = commandResponse 2.size();

for(i=respSize-1; i>0; i--) {
if ( commandResponse 2.at(i).find( "<" ) != std::string::np os )
{
for(j=i; j<respSize; j++) {
commandResponse += commandResponse 2.at(j);
}
break;
}
}

commandResponse += Buffer;

Buffer = "";

printf("\n Result2: \n %s \n", commandResponse .c_str());

infile.close();
============ code end =============== ==========
============ logfile start =============== ==========
< [command name]
LOADING PROGRAM

/*** SYNTAX ERROR ***/
/*** INVALID DELIMITER ***/
============ logfile end =============== ==========

There's a snippet of the code and the part of the logfile that
matters. There's bunch of lines and text before and after what's
on this example snippet from the logfile.
The code above goes thru the logfile and searhes until it finds
the /*** bit and puts it in the vector. Then it gets the next
line and puts it in the Buffer.
Then it goes through the vector backwards from the last row in
the vector until it finds the < character and appends it all to
the string. Last it appends the contents of the Buffer string
to the string as well. Or something like that at least.

Thanks for you help and patience Chris. I propably wouldn't have
been able to do this on time without your help. So BIG thanks.

----
mkarja
Jul 22 '05 #12

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

Similar topics

6
1781
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()
4
6284
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...
8
18253
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...
1
4691
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;...
7
6062
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...
2
1841
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: -------------------------------------------------------------------
1
6508
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? ...
10
8356
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,...
7
2270
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...
0
9275
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
10034
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...
1
9843
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
9713
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
8713
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...
1
7248
isladogs
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...
0
6534
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
5142
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...
3
2666
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.