如果用ios :: out打开文件,我该如何给用户一个警告和一个out [英] How do I give the user a warning and an out if he is opening a file with in ios::out

查看:117
本文介绍了如果用ios :: out打开文件,我该如何给用户一个警告和一个out的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果他错误地尝试使用ios :: out打开现有文件,我想向用户提供警告和出路。



这是代码我用来打开一个新文件或擦除一个旧文件并开始写新数据。





ofstream output(szSaveName, IOS ::应用程序);

if(!output)

{

cerr<< 无法打开<< szSaveName<< endl;

if(cin.peek())

返回0;

}

我解除了这个片段从某个地方几年,并没有预料到它可能导致的问题。人们希望在擦除数据之前收到警告。

I want to provide the user with a warning and a way out if he mistakenly tries to open an exist file with ios::out.

Here is the code I am using to open a new file or wipe an old one clean and begin writing new data.


ofstream output(szSaveName,ios::app);
if(!output)
{
cerr << "Could not open " << szSaveName << endl;
if (cin.peek())
return 0;
}
I lifted this snippet from somewhere a couple of years and didn't anticipate the problems it could cause. People expect to be warned before wiping out data.

推荐答案

首先使用对话框或控制台消息向用户询问他们要写入的文件的名称。然后检查文件是否存在,如果存在,请询问他们是否要覆盖它。
Start by using a dialog or console message to ask the user for the name of the file they wish to write. Then check if the file exists, and if so ask them if they wish to overwrite it.


Windows GetOpenFileName和GetCloseFileName结构有很多标记可以保存文件。操作,如果正确实施。我完全忘记了他们。感谢那些提供建议的人。
The Windows GetOpenFileName and GetCloseFileName structures have a wonderful selection of flags that make saving a file a well guarded operation, if properly implemented. I had completely forgotten them. Thanks to those who offered advice.


这篇关于如果用ios :: out打开文件,我该如何给用户一个警告和一个out的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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