拷贝文件。 [英] Copy file.

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

问题描述

这是用C ++复制文件所需要的吗?这个函数中是否有任何错误处理



bool copy_file(std :: string first,std :: string second)

{

std :: ifstream in(first.c_str());

if(!in.is_open())

返回false;


std :: ofstream out(second.c_str());

if(!out.is_open())

返回false;


返回<< in.rdbuf();

}


谢谢。

解决方案

Jason Heyes写道:

这是用C ++复制文件所需要的吗?这个函数中是否有任何错误处理缺失?

bool copy_file(std :: string first,std :: string second)


I ''可能仍然通过引用将参数传递给const。

{
std :: ifstream in(first.c_str());
if(!in.is_open( ))
返回false;

std :: ofstream out(second.c_str());
if(!out.is_open())
返回false;

返回<< in.rdbuf();
}




似乎很好。当然,返回''false''不知道发生了什么错误,只是出了问题。


V


Victor Bazarov写道:

Jason Heyes写道:

这是用C ++复制文件所需的全部内容吗?这个函数中是否缺少任何错误处理?

bool copy_file(std :: string first,std :: string second)



我''可能仍然会通过引用将参数传递给const。

{
std :: ifstream in(first.c_str());
if(! in.is_open())
返回false;

std :: ofstream out(second.c_str());
if(!out.is_open())
return false;

返回<< in.rdbuf();
}



似乎很好。当然,返回''false''不知道出了什么错误,只是出了问题。




嗯,标准流不要除了

失败之外,不提供更多。 Rocket Magnus写道:

Victor Bazarov写道:

<和。 blockquote class =post_quotes> Jason Heyes写道:

这是用C ++复制文件所需的全部内容吗?这个函数中是否缺少任何错误处理?

bool copy_file(std :: string first,std :: string second)



我''可能仍然会通过引用将参数传递给const。

{
std :: ifstream in(first.c_str());
if(!in.is_open())
返回false;

std :: ofstream out(second.c_str());
if(!out.is_open() )
返回false;

返回<< in.rdbuf();
}



似乎很好。当然,返回''false''不知道出错了什么,只是出了问题。



嗯,标准流不提供无论如何,除了
失败之外和坏。




是的,但是失败和坏,你知道什么流失败了。使用

OP的代码,您不知道是否无法打开源或

目标流。

Is this all it takes to copy a file in C++? Is there any error handling
missing in this function?

bool copy_file(std::string first, std::string second)
{
std::ifstream in(first.c_str());
if (!in.is_open())
return false;

std::ofstream out(second.c_str());
if (!out.is_open())
return false;

return out << in.rdbuf();
}

Thanks.

解决方案

Jason Heyes wrote:

Is this all it takes to copy a file in C++? Is there any error handling
missing in this function?

bool copy_file(std::string first, std::string second)
I''d probably still pass the arguments by ref to const.
{
std::ifstream in(first.c_str());
if (!in.is_open())
return false;

std::ofstream out(second.c_str());
if (!out.is_open())
return false;

return out << in.rdbuf();
}



Seems fine. Of course, returning ''false'' gives no idea what has gone
wrong, only that something did go wrong.

V


Victor Bazarov wrote:

Jason Heyes wrote:

Is this all it takes to copy a file in C++? Is there any error handling
missing in this function?

bool copy_file(std::string first, std::string second)



I''d probably still pass the arguments by ref to const.

{
std::ifstream in(first.c_str());
if (!in.is_open())
return false;

std::ofstream out(second.c_str());
if (!out.is_open())
return false;

return out << in.rdbuf();
}



Seems fine. Of course, returning ''false'' gives no idea what has gone
wrong, only that something did go wrong.



Well, the standard streams don''t provide much more anyway, other than
"failed" and "bad".


Rolf Magnus wrote:

Victor Bazarov wrote:

Jason Heyes wrote:

Is this all it takes to copy a file in C++? Is there any error handling
missing in this function?

bool copy_file(std::string first, std::string second)



I''d probably still pass the arguments by ref to const.

{
std::ifstream in(first.c_str());
if (!in.is_open())
return false;

std::ofstream out(second.c_str());
if (!out.is_open())
return false;

return out << in.rdbuf();
}



Seems fine. Of course, returning ''false'' gives no idea what has gone
wrong, only that something did go wrong.


Well, the standard streams don''t provide much more anyway, other than
"failed" and "bad".



Yeah, but with failed and bad, you know what stream failed. With the
OP''s code, you don''t know whether you couldn''t open the source or the
target stream.


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

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