从指定范围读取文件 [英] Reading a file from a specified range

查看:79
本文介绍了从指定范围读取文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




我正在试图弄清楚如何从

文件中读取某些行。

是否可以使用某些条件搜索文件然后

当找到搜索字符串时读取前后3行

该搜索字符串所在的行找到了?

当然也应该读取找到字符串的行。


我有一个txt格式的日志文件。如果

我有任何错误,我需要阅读该文件。如果出现问题,有两行有错误信息

。我需要得到错误和命令

导致错误大约比第一行

错误行高三行。我必须将错误用作搜索字符串,因为

命令每次都会更改,所以我不能将命令用作

搜索字符串。


错误行如下:

/ *** SYNTAX ERROR *** /

/ ***无效参数名称*** /


我正在使用/ ***作为搜索字符串。

我可以使用seekg如果我只是需要从

找到搜索字符串的点,但我无法想象我怎么能做我想做的事情。


我希望你能理解我在这里要做的事情。如果没有,我会

尝试解释更多。


----

mkarja

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

推荐答案



" mkarja" <毫米********** @ hotmail.com>在消息中写道

news:75 ************************** @ posting.google.c om ...

"mkarja" <mm**********@hotmail.com> wrote in message
news:75**************************@posting.google.c om...


我正在试图弄清楚如何从
文件中读取一些行。
是否可以搜索具有一些标准的文件,然后当找到搜索字符串时读取前后3行读取搜索字符串的那一行?
当然找到字符串的行也应该阅读。
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.




问题是向后读取位。你没有试图这样做,而是你需要保留最后三行在内存中读取的内容,这样当你找到你想要的字符串时,你就会得到所有的信息。你需要




这样的东西


string last_three_lines [3];

string curr_line;

while(getline(file,curr_line))

{

if(当前行是错误行)

{

...

}

last_three_lines [0] = last_three_lines [1];

last_three_lines [1] = last_three_lines [2];

last_three_lines [2] = curr_line;

}


john



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_lines[3];
string curr_line;
while (getline(file, curr_line))
{
if (current line is error line)
{
...
}
last_three_lines[0] = last_three_lines[1];
last_three_lines[1] = last_three_lines[2];
last_three_lines[2] = curr_line;
}

john




" mkarja" <毫米********** @ hotmail.com>在消息中写道

news:75 ************************** @ posting.google.c om ...

|

|

|我正在试图弄清楚如何从

|中读取一些行文件。

|是否可以使用某些标准搜索文件然后

|当找到搜索字符串时,在

|之前和之后读取3行找到搜索字符串的那一行?

|当然也应该读取找到字符串的行。

|

|我有一个txt格式的日志文件。如果

|我需要读取文件有任何错误。有两行有错误信息

|如果出现问题。我需要得到错误和命令

|导致错误大约是第一行

|的三行错误行。我必须将错误用作搜索字符串,因为

|命令每次都会改变,所以我不能将命令用作

|搜索字符串。

|

|错误行如下:

| / *** SYNTAX ERROR *** /

| / *** INVALID PARAMETER NAME *** /

|

|我正在使用/ ***作为搜索字符串。

|如果我只需要从

|中读取文件,我可以使用seekg找到搜索字符串的点,但我不能确定

|我怎么能做我想做的事。

|

|我希望你明白我在这里想做什么。如果没有,我会

|尝试解释更多。


你可以*向后寻求*,就像

你可以寻求*前进*来获得合理的
结果:


//文件中的大致行数...

std :: ifstream :: pos_type OneLine(100);


//从当前位置向后搜索

//在流中...

InFile.seekg(-OneLine ,std :: ios_base :: cur);


....然后:


while(std :: getline(InFile,Buffer) ))

// ...


干杯。

Chris Val

"mkarja" <mm**********@hotmail.com> wrote in message
news:75**************************@posting.google.c om...
| 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 ****** @ bigpond.com.au>在消息新闻中写道:< 2t ************* @ uni-berlin.de> ...
"Chris \( Val \)" <ch******@bigpond.com.au> wrote in message news:<2t*************@uni-berlin.de>...
" mkarja" <毫米********** @ hotmail.com>在消息中写道
新闻:75 ************************** @ posting.google.c om ...
|我有一个txt格式的日志文件。如果
|我需要阅读文件有任何错误。有两行有错误信息
|如果出现问题。我需要得到错误和命令
|导致错误大约比第一行高出三行
|错误行。我必须将错误用作搜索字符串,因为
|命令每次都会改变,因此我不能将命令用作
|搜索字符串。


[snip]你可以*向后寻求*,就像你可以寻求*前进*以获得合理的结果一样:

//文件中的大小行数...
标准:: ifstream :: pos_type OneLine(100);

//从当前位置向后搜索
//在流中......
InFile.seekg(-OneLine,std :: ios_base :: cur);

...然后:

while(std :: getline(InFile,Buffer))
// ...
"mkarja" <mm**********@hotmail.com> wrote in message
news:75**************************@posting.google.c om... | 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 ) )
// ...




典型的解决方案是将行读入循环缓冲区

必要的最小容量。在您的情况下,这似乎是5:2行

的兴趣,加上前3行。


/ david



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


这篇关于从指定范围读取文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆