FILESTREAM需要帮助 [英] FILESTREAM HELP NEEDED

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

问题描述

#include< fstream>

使用命名空间std;


int main()

{

ifstream in;

ofstream out;

int a = 0;

in.open(" a.txt");

out.open(" b.txt");

while(!in.eof())

{

in>> a;

out<< a<< endl;

}

返回0;

}


a.txt包含:


1

2

3

4

5

6

7

8


b.txt包含在程序结束时:


1

2

3

4

5

6

7

8

8


而不是:


1

2

3

4

5

6

7

8


如何防止8次打印两次?

#include<fstream>
using namespace std;

int main()
{
ifstream in;
ofstream out;
int a=0;
in.open("a.txt");
out.open("b.txt");
while(!in.eof())
{
in>>a;
out<<a<<endl;
}
return 0;
}

a.txt contains:

1
2
3
4
5
6
7
8

b.txt contains at the end of the program:

1
2
3
4
5
6
7
8
8

Instead of:

1
2
3
4
5
6
7
8

How can i prevent 8 from printing twice?

推荐答案



" coinjo" <共**** @ gmail.com>在消息中写道

news:11 ********************** @ g49g2000cwa.googlegr oups.com ...

"coinjo" <co****@gmail.com> wrote in message
news:11**********************@g49g2000cwa.googlegr oups.com...
#include< fstream>
使用命名空间std;

int main()
{
ifstream in;
ofstream out;
int a = 0;
in.open(" a.txt");
out.open(" b.txt");
while(!in.eof( ))
{
>> a;
out<< a<< endl;
}
返回0;
}
#include<fstream>
using namespace std;

int main()
{
ifstream in;
ofstream out;
int a=0;
in.open("a.txt");
out.open("b.txt");
while(!in.eof())
{
in>>a;
out<<a<<endl;
}
return 0;
}




首先,你应该遵循建议mlimber给你以前的

帖子:


将文件名传递给流对象的构造函数,而不是

显式调用open 。


然后,在你的while条件下输入语句,而不是eof()。在你的while语句中使用

eof()是不正确的,因为在_after_无法读取之前它没有检测到文件的结尾

_after_读取失败,检测读取失败的_reason_是否是文件结尾的b =(与某些文件相反)其他意想不到的情况)。


这无疑是导致问题的原因。如果

的空白行是文本文件的结尾(通常是这样,因为无论谁输入

数据,通常在每行之后点击Enter),然后最后一次通过

循环,它尝试将一个空行读入一个整数,但失败了。由于

读取失败,变量a仍然包含它之前所做的,即

值8.因此,您输出8.

-Howard



First, you should be following the advice "mlimber" gave to your previous
post:

Pass the filenames to the constructors of the stream objects, instead of
calling open explicitly.

Then, pust the input statement in your while condition, not eof(). Using
eof() in your while statement is incorrect, since it does not detect the end
of file until _after_ it fails to read. The eof() test should be used
_after_ a read fails, to detect if the _reason_ for the read failure was an
end-of-file (as opposed to some other unexpected condition).

This is undoubtedly the cause of your problem. If there''s a blank line at
the end of your text file (which there often is, because whoever enters the
data usually hits Enter after each line), then the last time through the
loop, it tries to read a blank line into an integer, which fails. Since the
read failed, the variable a still contains what it did before, which is the
value 8. So, you output 8.

-Howard




coinjo写道:

coinjo wrote:
#include< fstream>
使用命名空间std;

int main()
{if / in ifstream;
ofstream out;
int a = 0;
in.open(" a.txt");
out.open(" b.txt");
while(!in.eof())
{
in>> a;
out<< a<< endl;
}


eof()方法告诉你* last *输入操作试图通过EOF读取
,而不是* next *输入操作将读取超过

EOF,因此你的循环执行太多次。以下是最后一次

三次迭代:


1.测试EOF

2.不在EOF,所以阅读下一个字符

3.下一个字符读取为8,分配给

4.写入a的内容为

5.测试对于EOF

6.不在EOF,所以阅读下一个字符

7.下一个字符读取是EOF,因此读取失败; a保持不变

8.写出a的内容

9.测试EOF

10.在EOF,退出循环


而不是测试in.eof(),测试输入结果

运算符:


而(在>> a)

{

out<< a<< endl;

}

if(in.eof())

{

cerr<< 命中文件结束 <<结束;

}

其他

{

cerr<< 其他一些错误 << endl;

}

返回0;
}
#include<fstream>
using namespace std;

int main()
{
ifstream in;
ofstream out;
int a=0;
in.open("a.txt");
out.open("b.txt");
while(!in.eof())
{
in>>a;
out<<a<<endl;
}
The eof() method tells you if the *last* input operation attempted to
read past the EOF, not if the *next* input operation will read past the
EOF, so your loop executes one too many times. Here''s how the last
three iterations go:

1. Test for EOF
2. Not at EOF, so read next character
3. Next character read was 8, assign to a
4. Write contents of a to out
5. Test for EOF
6. Not at EOF, so read the next character
7. Next character read was EOF, so read fails; a is left unchanged
8. Write contents of a to out
9. Test for EOF
10. At EOF, exit loop

Instead of testing on in.eof(), test on the result of the input
operator:

while (in >> a)
{
out << a << endl;
}
if (in.eof())
{
cerr << "hit end of file" << endl;
}
else
{
cerr << "some other error" << endl;
}
return 0;
}








" Howard" <人***** @ hotmail.com>在消息中写道

新闻:OA ******************* @ bgtnsc05-news.ops.worldnet.att.net ...

"Howard" <al*****@hotmail.com> wrote in message
news:OA*******************@bgtnsc05-news.ops.worldnet.att.net...
这无疑是导致问题的原因。如果你的文本文件末尾有一个空行(通常是这样,因为无论谁输入
数据通常会在每行后点击Enter),那么最后一次通过
在循环中,它尝试将一个空行读入一个整数,但失败了。
由于读取失败,变量a仍然包含它之前所做的,
这是值8.所以,你输出8。
This is undoubtedly the cause of your problem. If there''s a blank line at
the end of your text file (which there often is, because whoever enters
the data usually hits Enter after each line), then the last time through
the loop, it tries to read a blank line into an integer, which fails.
Since the read failed, the variable a still contains what it did before,
which is the value 8. So, you output 8.




想想看,即使最后还没有额外的

行,它也会表现得这样。 ,因为上次成功读取(值8进入a)

仍然没有设置eof标志。该标志仅在

读取失败后才会设置。


-Howard



Come to think of it, it will behave this way even if there''s _not_ an extra
line at the end, because the last successful read (of the value 8 into a)
will still not have set the eof flag. That flag only gets set after a
failure to read occurs.

-Howard


这篇关于FILESTREAM需要帮助的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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