检查:测试文件存在的更好方法是什么? [英] Checkup: better way to test for file existence?

查看:93
本文介绍了检查:测试文件存在的更好方法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,


我看到这个新闻组中的其他参考文献说,测试文件存在的唯一标准是/或
C ++方式是我的代码的一些变体下面;

有人可以请确认......或提供其他选择吗?


此外,可能还有跨平台的替代品,比如说

库像Boost,还是别的?


-Matt


#include< fstream>


/ *

[类FileHandle包含''fstream文件''和以前初始化的

''字符串文件名''。]

* /


bool

FileHandle :: exists()

{

bool retval = false;


// TODO / XXX:有没有更好的方法

//测试文件是否存在

//没有打开它?


file.open(filename.c_str(),ios :: in | ios :: binary);


if(!file.fail())retval = true;


file.close();


返回retval;

}


-

删除downwithspammers-;给我发电子邮件的文字。

Hello,

I see other references in this newsgroup saying that the only standard
C++ way to test for file existence is some variant of my code below;
can someone please confirm...or offer alternatives?

Additionally, might there be cross-platform alternatives, say in a
library like Boost, or something else?

-Matt

#include <fstream>

/*
[class FileHandle contains ''fstream file'' and previously-initialed
''string filename''.]
*/

bool
FileHandle::exists()
{
bool retval = false;

// TODO/XXX: Is there a better way to
// test the existence of a file
// without opening it?

file.open(filename.c_str(), ios::in | ios::binary);

if (!file.fail()) retval = true;

file.close();

return retval;
}

--
Remove the "downwithspammers-" text to email me.

推荐答案



" Matt" <毫安** @ downwithspammers-mengland.net>在消息中写道

news:18 ******************************** @ 4ax.com ...

"Matt" <ma**@downwithspammers-mengland.net> wrote in message
news:18********************************@4ax.com...
你好,

我看到这个新闻组中的其他参考文献说,测试文件存在的唯一标准的C ++方法是我的代码的一些变体下面;
有人可以确认...或提供替代品吗?


你必须误读它们,或者阅读一些不正确的b / b
。标准C ++中没有办法最终检测到文件的不存在。 (如果使用除了一次以外的模式

打开成功,那么

将创建一个不存在的文件,那么当然

确实表明以前存在的文件)。但是如果开放失败,那么

,这并不一定意味着

不存在。您可以确定的是,打开文件的尝试是否成功或失败。尝试打开

可能会因各种原因而失败,具体取决于

环境,包括但不限于:不足

操作系统权限,文件所在的设备是离线的,是否存在
等等。许多实现确实提供了扩展

来获取有关i / o的更详细信息,但是

他们不是标准的。

此外,可能有跨平台的替代品,比如像Boost这样的库,或其他什么?


可能有一些可以解决至少一小部分b $ b b的环境问题。试试谷歌。

-Matt

#include< fstream>

/ *
[类FileHandle包含' 'fstream文件''和以前 - 刚刚'/'''''''''''* / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / {
bool retval = false;

// TODO / XXX:有没有更好的方法来测试文件是否存在
//无需打开它?


取决于更好的含义。使用标准C ++完全无法使用
。 AFAIK平台特定的

解决方案由大多数实现提供。

file.open(filename.c_str(),ios :: in | ios :: binary);

if(!file.fail())retval = true;

file.close();

返回retval;
}
Hello,

I see other references in this newsgroup saying that the only standard
C++ way to test for file existence is some variant of my code below;
can someone please confirm...or offer alternatives?
You must have misread them, or read some that were not
correct. There is no way in standard C++ to detect
conclusively the nonexistence of a file. (If an
open succeeds when using a mode other than once which
will create a nonexistent file, then of course that
does indicate that the file previously existed). But
if an open fails, that does not necessarily mean it
doesn''t exist. All you can determine is if an attempt
to open a file succeeded or failed. An attempt to open
could fail for a variety of reasons depending upon the
environment, including but not limited to: insufficient
OS permissions, device where file resides is offline, does
not exist, etc. Many implementations do provide extensions
for obtaining more detailed information about i/o, but
they''re not standard.

Additionally, might there be cross-platform alternatives, say in a
library like Boost, or something else?
There probably are some which address at least a small
number of environments. Try google.


-Matt

#include <fstream>

/*
[class FileHandle contains ''fstream file'' and previously-initialed
''string filename''.]
*/

bool
FileHandle::exists()
{
bool retval = false;

// TODO/XXX: Is there a better way to
// test the existence of a file
// without opening it?
Depends upon what ''better'' means. There is no way
at all using standard C++. AFAIK platform-specific
solutions are provided by most implementations.

file.open(filename.c_str(), ios::in | ios::binary);

if (!file.fail()) retval = true;

file.close();

return retval;
}




如果此函数返回''false'',那么这意味着

是无法打开文件(对于未知的

原因)。没有更多,没有更少。


-Mike



If this function returns ''false'', all that means
is that the file could not be opened (for an unknown
reason). Nothing more, nothing less.

-Mike


您可以使用旧的C学校功能进行测试:


#include" unistd.h。"

#include< iostream>

#include< string>

int main(int argc,char ** argv)

{

int ret(0);

std :: string fname(" C:\\AUTOEXEC.BAT");

ret = access(fname.c_str(),F_OK);

if(ret == -1)

std :: cout<< 在文件中访问 << fname<< "被拒绝 <<

std :: endl;


system(" Pause");

返回EXIT_SUCCESS;

}


如果你可以访问该文件,可以更具体地测试这个。

You can test this with an old school C function:

#include "unistd.h."
#include <iostream>
#include <string>
int main(int argc, char **argv)
{
int ret(0);
std::string fname("C:\\AUTOEXEC.BAT");
ret = access(fname.c_str(), F_OK);
if (ret == -1)
std::cout << "Access on file " << fname << " is denied" <<
std::endl;

system("Pause");
return EXIT_SUCCESS;
}

more specific tests this if you can get access to the file.


Matt写道:
Matt wrote:
bool
FileHandle :: exists()
{
bool retval = false;

/ / TODO / XXX:有没有更好的方法来测试文件的存在
//而不打开它?
bool
FileHandle::exists()
{
bool retval = false;

// TODO/XXX: Is there a better way to
// test the existence of a file
// without opening it?




为什么如果你不想打开它,你想测试文件的存在吗?

如果你只是想确保它在那里所以你以后可以打开它,那个'b $ b'不是一个好主意,因为有些东西可能会发生变化(文件

删除/创建/重命名,权限更改,设备未安装,无论如何)。



Why would you want to test for the existence of a file if you don''t want to
open it?
If you just want to make sure it''s there so you can open it later, that''s
not a good idea since something could change in between (file
removed/created/renamed, permissions changed, device unmounted, whatever).


这篇关于检查:测试文件存在的更好方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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