fstream,getline()和failbit [英] fstream, getline() and failbit

查看:71
本文介绍了fstream,getline()和failbit的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在读取二进制文件,我想在其中搜索字符串。

唯一的问题是,在只需要几次调用

getline()之后,failbit就会被设置,所以它永远不会到达字符串为
的文件的末尾。
包含。从阅读帖子到这个列表,如果在阅读时出现格式错误,似乎

failbit会被设置。将二进制数据读入char []数组是不是很糟糕

表格?这是为什么我的

函数下面的功能不起作用?


void ReadBinData()

{

int reads = 0;

字符串数据;

char str [1024];

fstream myFile(" test.exe" ,ios :: in | ios :: binary);

if((myFile.rdstate()& ifstream :: failbit)!= 0)

cout< < 错误;


while(myFile.getline(str,1024))

{

data = str;

if(data.find(" roryrory",0)!= string :: npos)

cout<< 找到它;

读取++;

}


cout<< " \ nno of times getline被称为=" <<读取<< endl;


if((myFile.rdstate()& ifstream :: failbit)!= 0)

cout<< \ nerror,failbit set ...." ;;


myFile.close();

}


Rory。

I am reading a binary file and I want to search it for a string. The
only problem is that failbit gets set after only a few calls to
getline() so it never reaches the end of the file where the string is
contained. From reading through posts to this list it seems that
failbit gets set if there is a format error whilst reading. Is it bad
form to reading binary data into a char[] array? Is this why my
function below doesn''t work?

void ReadBinData()
{
int reads=0;
string data;
char str[1024];
fstream myFile ("test.exe", ios::in | ios::binary);
if ( (myFile.rdstate() & ifstream::failbit ) != 0 )
cout << "error";

while(myFile.getline(str, 1024 ))
{
data = str;
if(data.find("roryrory", 0)!=string::npos)
cout << "found it";
reads++;
}

cout << "\nno of times getline was called = " << reads << endl;

if ( (myFile.rdstate() & ifstream::failbit ) != 0 )
cout << "\nerror, failbit set....";

myFile.close();
}

Rory.

推荐答案

rory写道:
rory wrote:

while(myFile。 getline(str,1024))
while(myFile.getline(str, 1024 ))



尝试读取(str,1024)而不是getline(不能使用while(read(...))然后

) - 你为什么要阅读行?从二进制(.exe)

文件无论如何?


但是这不应该是你的问题的原因。可能会发生什么

是你的搜索表达式会被分成2个不同的

" getlines"在你的情况下,当已经读取1023个字节而没有找到换行符分隔符时。
。例如,str将包含roryr \ n。在

最后6个字节,并在下一个电话中填写ory字样。在

前3个字节。你永远不会找到你的搜索表达式。

并且在仅少数之后可能会设置failbit。调用getline,

如果可执行文件只包含一些换行符并且不比$几千字节大得多......那么长话短说:不要使用getline!

然后看看你的问题是否仍然存在。


最好的问候,


Lars

Try read (str, 1024) instead of getline (can''t use "while (read (...))"
then though) - why would you want to read "lines" from a binary (.exe)
file anyways?

However that should not be the cause of your problem. What COULD happen
is that your search expression will get split over 2 different
"getlines" in your case, when 1023 bytes have been read without finding
a newline delimiter. str will then for example contain "roryr\n" in the
last 6 bytes, and on the next call will be filled with "ory" in the
first 3 bytes. You''d never find your search expression.
And the failbit might well be set after "only a few" calls to getline,
if the executable only contains a few newlines and isn''t much bigger
than a couple of kilobytes.. So long story short: don''t use getline!
Then see if your problem still occurs.

Best Regards,

Lars


1月23日下午1:43,Lars Uffmann< a ... @ nurfuerspam.dewrote:
On Jan 23, 1:43 pm, Lars Uffmann <a...@nurfuerspam.dewrote:

rory写道:
rory wrote:

while(myFile.getline(str,1024))
while(myFile.getline(str, 1024 ))



尝试阅读( str,1024)而不是getline(不能使用&while; while(read(...))"

然后) - 为什么你要读取行?从二进制(.exe)

文件无论如何?


但是这不应该是你的问题的原因。可能会发生什么

是你的搜索表达式会被分成2个不同的

" getlines"在你的情况下,当已经读取1023个字节而没有找到换行符分隔符时。
。例如,str将包含roryr \ n。在

最后6个字节,并在下一个电话中填写ory字样。在

前3个字节。你永远不会找到你的搜索表达式。

并且在仅少数之后可能会设置failbit。调用getline,

如果可执行文件只包含一些换行符并且不比$几千字节大得多......那么长话短说:不要使用getline!

然后看看你的问题是否仍然存在。


最好的问候,


Lars


Try read (str, 1024) instead of getline (can''t use "while (read (...))"
then though) - why would you want to read "lines" from a binary (.exe)
file anyways?

However that should not be the cause of your problem. What COULD happen
is that your search expression will get split over 2 different
"getlines" in your case, when 1023 bytes have been read without finding
a newline delimiter. str will then for example contain "roryr\n" in the
last 6 bytes, and on the next call will be filled with "ory" in the
first 3 bytes. You''d never find your search expression.
And the failbit might well be set after "only a few" calls to getline,
if the executable only contains a few newlines and isn''t much bigger
than a couple of kilobytes.. So long story short: don''t use getline!
Then see if your problem still occurs.

Best Regards,

Lars



感谢Lars,使用读取它似乎读取整个文件,文件大小

是3.830 mbs,读取被称为3829次。我的下一个问题是你提到的
,读取数据块意味着字符串可能被切断

up这是我想要的最后一件事。我的想法是,我将一个唯一的

字符串标识符附加到二进制文件中,然后我在它后面添加一些文本。

然后我想在该文件中搜索唯一字符串标识符和

然后检索它后面的文本。在写这个独特的

字符串之前,我首先写一个换行符,这就是为什么我认为我可以只用

使用getline()直到换行。但有效点是,它可能并不总是能够获得一条新线。你对我有什么建议

我怎么可能这样做?感谢您的回复,


Rory。

Thanks Lars, using read it seems to read the entire file, the filesize
is 3.830 mbs and read is called 3829 times. My next problem is one you
alluded to, reading blocks of data means the string could get chopped
up which is the last thing I want. The idea is that I append a unique
string identifier to a binary file, then I append some text after it.
I then want to search that file for the unique string identifier and
then retrieve the text that follows it. Before writing the unique
string I first write a newline char, that''s why I thought I could just
use getline() as it runs until a new line. Valid point however that it
might not always get to a new line. Have you any suggestions for me on
how I might do this? Thanks for the reply,

Rory.


文章< ed81c061-791b-4d38-ac59-
54**********@v67g2000hse.googlegroups.com >, ro*******@gmail.com 说...


[...]
In article <ed81c061-791b-4d38-ac59-
54**********@v67g2000hse.googlegroups.com>, ro*******@gmail.com says...

[ ... ]

感谢Lars,使用读取它似乎读取整个文件,文件大小

是3.830 mbs读取被称为3829次。我的下一个问题是你提到的
,读取数据块意味着字符串可能被切断

up这是我想要的最后一件事。我的想法是,我将一个唯一的

字符串标识符附加到二进制文件中,然后我在它后面添加一些文本。

然后我想在该文件中搜索唯一字符串标识符和

然后检索它后面的文本。在写这个独特的

字符串之前,我首先写一个换行符,这就是为什么我认为我可以只用

使用getline()直到换行。但有效点是,它可能并不总是能够获得一条新线。你对我有什么建议

我怎么可能这样做?感谢您的回复,
Thanks Lars, using read it seems to read the entire file, the filesize
is 3.830 mbs and read is called 3829 times. My next problem is one you
alluded to, reading blocks of data means the string could get chopped
up which is the last thing I want. The idea is that I append a unique
string identifier to a binary file, then I append some text after it.
I then want to search that file for the unique string identifier and
then retrieve the text that follows it. Before writing the unique
string I first write a newline char, that''s why I thought I could just
use getline() as it runs until a new line. Valid point however that it
might not always get to a new line. Have you any suggestions for me on
how I might do this? Thanks for the reply,



我的猜测是失败是由于文件中的某些值被解释为信号结束当它被视为文本时的文件。

Unix通常以这种方式处理control-D;对于Windows它是控件-Z。

无论如何,你需要通过打开它作为二进制流来告诉你的流不要解释控制

字符:


std :: ifstream文件(your_file_name,std :: ios :: binary);


std :: stringstream temp;


//将文件复制到一个字符串中

temp<< file.rdbuf();


//数据开头的标记:

std :: string sentinel(" \ nroryrory");


//找到你的数据(std :: string :: npos如果它不存在)

int data_pos = temp.str()。 find(sentinel)+ sentinel.length();


-

后来,

杰里。


宇宙是自己想象的虚构。

My guess is that the failure is due to some value in the file being
interpreted as signaling the end of the file when it''s treated as text.
Unix generally treats control-D this way; for Windows it''s control-Z.
Regardless, you need to tell your stream not to interpret control
characters that way, by opening it as a binary stream:

std::ifstream file(your_file_name, std::ios::binary);

std::stringstream temp;

// copy the file into a string
temp << file.rdbuf();

// marker for the beginning of your data:
std::string sentinel("\nroryrory");

// find your data (std::string::npos if it doesn''t exist)
int data_pos = temp.str().find(sentinel)+sentinel.length();

--
Later,
Jerry.

The universe is a figment of its own imagination.


这篇关于fstream,getline()和failbit的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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