文件流问题 [英] filestream problem

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

问题描述

我有一点文件流问题。使用下面的代码,我打开一个文件

,以便将其内容插入另一个文件。但是,在

打开文件之后,insertfile.eof()似乎为TRUE。在打开

有问题的文件之前,打开另一个文件并通过

相同的代码片段(在循环中)插入,没有任何问题。我肯定

要插入的文件有几行内容,我还检查了插入文件流是否已关闭,然后再打开它。


if(FileExists(insertfilepath))

{

insertfile.open(insertfilepath.c_str());

while(!insertfile.eof())

{

getline(insertfile,insertline);

outfile<< insertline<< endl;

}

insertfile.close();

}


我真的很感激一些建议在这里。


谢谢,

彼得

I''m having a little filestream problem. With the code below, I open a file
in order to insert its contents into another file. However, right after the
opening of the file, insertfile.eof() seems to be TRUE. Before the
problematic file is opened, one other file is opened and inserted by the
same piece of code (which is in a loop), without any problems. I''m sure that
the file to be inserted has several lines of contents, and I also checked if
the insertfile stream is closed before opening it.

if(FileExists(insertfilepath))
{
insertfile.open(insertfilepath.c_str());
while(! insertfile.eof())
{
getline(insertfile, insertline);
outfile << insertline << endl;
}
insertfile.close();
}

I would really appreciate some advise here.

Thanks,
Pieter

推荐答案

Pieter Provoost写道:
Pieter Provoost wrote:
我有一点文件流问题。使用下面的代码,我打开一个文件
,以便将其内容插入另一个文件。但是,在
打开文件后,insertfile.eof()似乎为TRUE。在打开
有问题的文件之前,打开另一个文件并通过相同的代码片段(在循环中)插入,没有任何问题。我确定要插入的文件有几行内容,我还检查了插件文件流是否关闭后再打开它。

if(FileExists (insertfilepath))
{
insertfile.open(insertfilepath.c_str());
while(!insertfile.eof())
{
getline(insertfile, insertline);
outfile<< insertline<< endl;
}
insertfile.close();


添加


insertfile.clear();


此处。 ''eof''位被转移并且不会被''open''

操作清除,很可能是。

}
我真的很感激一些建议。
I''m having a little filestream problem. With the code below, I open a file
in order to insert its contents into another file. However, right after the
opening of the file, insertfile.eof() seems to be TRUE. Before the
problematic file is opened, one other file is opened and inserted by the
same piece of code (which is in a loop), without any problems. I''m sure that
the file to be inserted has several lines of contents, and I also checked if
the insertfile stream is closed before opening it.

if(FileExists(insertfilepath))
{
insertfile.open(insertfilepath.c_str());
while(! insertfile.eof())
{
getline(insertfile, insertline);
outfile << insertline << endl;
}
insertfile.close();
Add

insertfile.clear();

here. The ''eof'' bit gets carried over and is not cleared by the ''open''
operation, most likely.
}

I would really appreciate some advise here.




HTH


V



HTH

V



" Victor Bazarov" <五******** @ comAcast.net> schreef in bericht

新闻:Ya **************** @ newsread1.mlpsca01.us.to.v erio.net ...

"Victor Bazarov" <v.********@comAcast.net> schreef in bericht
news:Ya****************@newsread1.mlpsca01.us.to.v erio.net...
Pieter Provoost写道:
Pieter Provoost wrote:
我有一点文件流问题。使用下面的代码,我打开一个
文件,以便将其内容插入另一个文件。但是,在文件打开
之后,insertfile.eof()似乎为TRUE。在打开
有问题的文件之前,打开另一个文件并通过相同的代码片段(在循环中)插入,没有任何问题。我确定
要插入的文件有几行内容,并且我还要检查插件文件流在打开之前是否已关闭


if(FileExists (insertfilepath))
{
insertfile.open(insertfilepath.c_str());
while(!insertfile.eof())
{
getline(insertfile, insertline);
outfile<< insertline<< endl;
}
insertfile.close();
I''m having a little filestream problem. With the code below, I open a file in order to insert its contents into another file. However, right after the opening of the file, insertfile.eof() seems to be TRUE. Before the
problematic file is opened, one other file is opened and inserted by the
same piece of code (which is in a loop), without any problems. I''m sure that the file to be inserted has several lines of contents, and I also checked if the insertfile stream is closed before opening it.

if(FileExists(insertfilepath))
{
insertfile.open(insertfilepath.c_str());
while(! insertfile.eof())
{
getline(insertfile, insertline);
outfile << insertline << endl;
}
insertfile.close();



添加

insertfile.clear();

这里。 ''eof''位被转移并且不会被''open''
操作清除。很可能。



Add

insertfile.clear();

here. The ''eof'' bit gets carried over and is not cleared by the ''open''
operation, most likely.



工作正常,谢谢!我忘了提到我正在使用BCB6,我不知道是否

这个问题也会出现在其他编译器上......


Pieter


Works fine, thanks! I forgot to mention that I''m using BCB6, I don''t know if
this problem also occurs with other compilers...

Pieter


Pieter Provoost写道:
Pieter Provoost wrote:

我有一点文件流问题。使用下面的代码,我打开一个文件
,以便将其内容插入另一个文件。但是,在
打开文件后,insertfile.eof()似乎为TRUE。在打开
有问题的文件之前,打开另一个文件并通过相同的代码片段(在循环中)插入,没有任何问题。我确定要插入的文件有几行内容,我还检查了插件文件流是否关闭后再打开它。

if(FileExists (insertfilepath))
{
insertfile.open(insertfilepath.c_str());
while(!insertfile.eof())
{
getline(insertfile, insertline);
outfile<< insertline<< endl;
}
insertfile.close();
}

我非常感谢你的一些建议。

I''m having a little filestream problem. With the code below, I open a file
in order to insert its contents into another file. However, right after the
opening of the file, insertfile.eof() seems to be TRUE. Before the
problematic file is opened, one other file is opened and inserted by the
same piece of code (which is in a loop), without any problems. I''m sure that
the file to be inserted has several lines of contents, and I also checked if
the insertfile stream is closed before opening it.

if(FileExists(insertfilepath))
{
insertfile.open(insertfilepath.c_str());
while(! insertfile.eof())
{
getline(insertfile, insertline);
outfile << insertline << endl;
}
insertfile.close();
}

I would really appreciate some advise here.




你以错误的方式使用eof()

(我只是等待你的下一个帖子题为:

"为什么我的最后一行文件被处理两次?)


-

Karl Heinz Buchegger
kb ****** @ gascad.at


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

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