[C ++]重新整理isteam和GNU G ++ [英] [C++] Rewinding an isteam and GNU G++

查看:48
本文介绍了[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 ++新闻组欢迎辞:
http://www.slack.net/~shiva/welcome.txt

C ++常见问题: http://www.parashift.com/c++-faq-lite

C常见问题: http://www.eskimo。 com /~scs / c-faq / top.html

alt.comp.lang.learn.c-c ++ faq:
http://www.comeaucomputing.com/learn/faq/

其他网站:
http://www.josuttis.com - C ++ STL图书馆书籍
http://www.sgi.com/tech/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

推荐答案



" Thomas Matthews" <钍********** @ sbcglobal.net>写在

消息新闻:41 ************** @ sbcglobal.net ...

"Thomas Matthews" <Th*************************@sbcglobal.net> wrote in
message news:41**************@sbcglobal.net...


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


那是。

哪个编译器是正确的?


Borland。

有人可以和微软一起测试吗?
Hi,
So, what is the standard method for rewinding?
That is.
Which compiler is correct?
Borland.
Can somebody test with Microsoft?




适用于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




" Thomas Matthews" <钍********** @ sbcglobal.net>在留言中写道

news:41 ************** @ sbcglobal.net ...

|

|

|我已经搜索了常见问题解答,没有关于这个的提示。

|搜索新闻组显示正确的

|倒带istream的方法是:

| istream inp;

| //读过去的EOF。

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

| inp.seekg(0);

|

|我和Borland使用了上述技术

| C ++ Builder,它的工作原理。但是,它不是
|使用GNU g ++ 3.3.1(cygwin special)。

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


[snip]


| int main(void)

| {

| ifstream inp(my_data.txt);

| string text_line;

|

| //读取整个数据文件

| //并发送给cout。

| while(getline(inp,text_line))

| {

| cout<< text_line<< endl;

| }

|

| //现在,inp文件出错了

| //状态,在定位之前清除它。

| inp.clear();


为什么不在这里检查''eof()''


if(!inp.eof( ))

//处理错误...


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

| //处于良好状态{inp.good()== true}。

|

| //倒回文件。

| inp.seekg(0);

|

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

| //处于失败状态(inp.fail()== true)。

| //然而,Borland编译器报告说

| // inp处于良好状态。


如果你清除了标记,那么流应该是好的状态,我想。 br />

请尝试这个重载:

inp.seekg(0,std :: ios_base :: beg);


此外,您还可以使用out

模式设置打开流:


std :: ifstream inp(" my_data.txt",std :: ios_base :: out);


[snip]


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

|哪个编译器是正确的?

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


我认为Borland是正确的,但我只想到了

的东西。尝试以二进制模式打开流

以获得准确的搜索。


干杯。

Chris Val

"Thomas Matthews" <Th*************************@sbcglobal.net> wrote in message
news:41**************@sbcglobal.net...
| 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):

[snip]

| 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();

Why not check for ''eof()'' here ?

if( !inp.eof() )
// Handle error...

| // 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.

If you cleared the flags, then the stream should
be in an good state, I would think.

Try this overload instead:
inp.seekg( 0, std::ios_base::beg );

Additionally, you can open the stream with the out
mode set as well:

std::ifstream inp( "my_data.txt", std::ios_base::out );

[snip]

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

I think Borland is correct, but I just thought of
something. Try to open the stream in binary mode
to get an accurate seek as well.

Cheers.
Chris Val


>
我认为Borland是正确的,但我只是想到了什么。尝试以二进制模式打开流
以获得准确的搜索。
I think Borland is correct, but I just thought of
something. Try to open the stream in binary mode
to get an accurate seek as well.




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




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 ++的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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