本文介绍了[C ++]重新整理isteam和GNU G ++的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




我搜索了常见问题解答,没有关于此问题的提示。

搜索新闻组显示正确的

倒带istream的方法是:

istream inp;

//读取过去的EOF。

inp.clear(); //清除流状态

inp.seekg(0);


我在Borland上使用了上述技术

C ++ Builder,它的工作原理。但是,使用GNU g ++ 3.3.1(cygwin特别版)并不是


这是一个小型演示程序(带注释):


#include< iostream>

#include< fstream>

#include< cstdlib>

#使用命名空间std包含< string>


; //这个例子。


int main(无效)

{

ifstream inp(" my_data.txt") ;

string text_line;


//读取整个数据文件

//并发送给cout。

while(getline(inp,text_line))

{

cout<< text_line<< endl;

}


//现在,inp文件出错了

//状态,所以之前要清除它定位。

inp.clear();


//此时,G ++报告inp是

// in一个好状态{inp.good()== true}。


//回卷文件。

inp.seekg(0);


//此时,G ++报告在失败状态下inp是

//(inp.fail()== true)。

//然而,Borland编译器报告

// inp处于良好状态。


//在倒带后清除状态。 />
//这主要是针对G ++。

inp.clear();


//阅读{第一行}文字。

if(getline(inp,text_line))

{

cout<< text_line<<结束;

}

其他

{

// G ++编译器总是来这里。

cerr<< 倒带后的getline失败。 <<结束;

}


返回EXIT_SUCCESS;

}


那么,什么是倒带的标准方法吗?

哪个编译器是正确的?

有人可以和微软一起测试吗?


-

Thomas Matthews


C ++新闻组欢迎辞:


C ++常见问题:

C常见问题:

alt.comp.lang.learn.c-c ++ faq:


其他网站:
- C ++ STL图书馆书籍
- 标准模板库

Hi,

I''ve searched the FAQ and no tips on this one.
Searching the newsgroups shows that the proper
method for rewinding an istream is:
istream inp;
// read past EOF.
inp.clear(); // Clear the stream state
inp.seekg(0);

I''ve used the above technique with Borland
C++ Builder and it works. However, it doesn''t
work with GNU g++ 3.3.1 (cygwin special).
Here is a small demo program (annotated):

#include <iostream>
#include <fstream>
#include <cstdlib>
#include <string>

using namespace std; // for this example.

int main(void)
{
ifstream inp("my_data.txt");
string text_line;

// Read the entire data file
// and send to cout.
while (getline(inp, text_line))
{
cout << text_line << endl;
}

// Now, the inp file is in an error
// state, so clear it before positioning.
inp.clear();

// At this point, G++ reports that inp is
// in a good state {inp.good() == true}.

// Rewind the file.
inp.seekg(0);

// At this point, G++ reports that inp is
// in a failed state (inp.fail() == true).
// However, Borland compiler reports that
// inp is in a good state.

// Clear the state after rewinding.
// This is primarily for G++.
inp.clear();

// Read {the first} line of text.
if (getline(inp, text_line))
{
cout << text_line << endl;
}
else
{
// The G++ compiler always comes here.
cerr << "getline after rewind failed." << endl;
}

return EXIT_SUCCESS;
}

So, what is the standard method for rewinding?
Which compiler is correct?
Can somebody test with Microsoft?

--
Thomas Matthews

C++ newsgroup welcome message:
http://www.slack.net/~shiva/welcome.txt
C++ Faq: http://www.parashift.com/c++-faq-lite
C Faq: http://www.eskimo.com/~scs/c-faq/top.html
alt.comp.lang.learn.c-c++ faq:
http://www.comeaucomputing.com/learn/faq/
Other sites:
http://www.josuttis.com -- C++ STL Library book
http://www.sgi.com/tech/stl -- Standard Template Library

推荐答案




适用于Microsoft 7.1(没有额外的clear()调用。另外

在g ++ 3.3.3上运行正常(cygwin特别版)


听起来像g ++ bug。我会使用一个调试器来深入研究fstream / filebuf

代码(或者你可以只看一眼)然后找出确切的错误。

既然它可能只需要更改头文件来修复它也许你可以从某个地方下载修复程序。似乎不太可能这种bug会长时间被忽视。


或许你可以升级。


john



Works fine with Microsoft 7.1 (without the additional call to clear()). Also
works fine on g++ 3.3.3 (cygwin special)

Sounds like a g++ bug. I''d use a debugger to dig into the fstream/filebuf
code (or maybe you can just eyeball it) and find out exactly what is wrong.
Since it is only likely to need a header file change to fix it maybe you can
download the fix from somewhere. Seems unlikely that this sort of bug would
have gone unnoticed for long.

Or maybe you could just upgrade.

john






寻找文件的开头是有效的,即使在文件模式下打开文件




john



It''s valid to seek to the beginning of a file, even when the file is opened
in text mode.

john


这篇关于[C ++]重新整理isteam和GNU G ++的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-18 19:24