检查文件/流是否存在 [英] checking if a file/stream exists

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

问题描述



Cracow,3.01.2005


您好,


在追加中打开文件流时模式,

创建一个新文件(如果指定的文件不存在),

或打开现有文件以添加内容。

有没有办法确定,在调用open(文件名,ios :: app)

方法后,这两种选择中的哪一种已经发生?

我想要区分程序操作,取决于我是否写入新文件或现有文件。


L.Bieniasz

* -------------------------------------------------- ----------------- *

| Leslaw Bieniasz博士,|

|波兰科学院物理化学研究所,|
|气体燃料电化学氧化系,|
| UL认证。 Zagrody 13,30-318克拉科夫,波兰。 |

|电话/传真号码:+48(12)266-03-41 |

|电子邮件: nb******@cyf-kr.edu.pl |

* ------------------------------------------- ------------------------ *

|对计算电化学感兴趣? |

|访问我的网站: http://www.cyf-kr.edu。 pl / ~nbbienia |

* -------------------------------- ----------------------------------- *


Cracow, 3.01.2005

Hello,

When opening a file stream in the append mode,
either a new file is created (if a specified file does not exist),
or an existing file is opened for adding stuff.
Is there any way to determine, after calling the open(filename,ios::app)
method, which of these two alternatives has occurred?
I would like to differentiate the program operation, depending
on whether I write to a new or existing file.

L.Bieniasz
*-------------------------------------------------------------------*
| Dr. Leslaw Bieniasz, |
| Institute of Physical Chemistry of the Polish Academy of Sciences,|
| Department of Electrochemical Oxidation of Gaseous Fuels, |
| ul. Zagrody 13, 30-318 Cracow, Poland. |
| tel./fax: +48 (12) 266-03-41 |
| E-mail: nb******@cyf-kr.edu.pl |
*-------------------------------------------------------------------*
| Interested in Computational Electrochemistry? |
| Visit my web site: http://www.cyf-kr.edu.pl/~nbbienia |
*-------------------------------------------------------------------*

推荐答案

Leslaw Bieniasz写道:
Leslaw Bieniasz wrote:

克拉科夫,3.01.2005

你好,

打开文件时在附加模式下流,
创建一个新文件(如果指定的文件不存在),
或打开现有文件添加东西。
有没有办法确定,在调用open(filename,ios :: app)
方法后,发生了这两种替代方法中的哪一种?
我想区分程序操作,取决于是否写入一个新的或现有的文件。

Cracow, 3.01.2005

Hello,

When opening a file stream in the append mode,
either a new file is created (if a specified file does not exist),
or an existing file is opened for adding stuff.
Is there any way to determine, after calling the open(filename,ios::app)
method, which of these two alternatives has occurred?
I would like to differentiate the program operation, depending
on whether I write to a new or existing file.




您的系统可能包含一个特定于系统的功能,允许您检查文件是否存在。


另一种方式:

尝试以非追加模式打开文件。如果这样可行,则

文件确实存在,如果不存在...关闭文件并将其打开并附加

模式。然后,您现在可以使用您在

中找到的信息,首次尝试指导您的操作。


-

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


您可以使用ifstream :: tellg()(STL的一部分)来查找

文件的大小。如果它大于零,则在打开文件之前文件必须已存在

。如果文件大小为零,那么你可以假设

文件是新的..希望它有帮助。


-Shan
You can use the ifstream::tellg() (part of STL) to find the size of the
file. If it is greater than zero then the file must have existed
before you opened it. If the file size is zero then you can assume that
the file is new..Hope it helps.

-Shan


Shan写道:

您可以使用ifstream :: tellg()(STL的一部分)来查找文件。如果它大于零,则在打开文件之前文件必须已存在。如果文件大小为零,那么你可以认为该文件是新的..希望它有所帮助。

You can use the ifstream::tellg() (part of STL) to find the size of the
file. If it is greater than zero then the file must have existed
before you opened it. If the file size is zero then you can assume that
the file is new..Hope it helps.




不是真的。

tellg()返回一些只能用于seekg()的数字。

没有人能保证这个数字代表什么。它可以

是到目前为止读取的字节数,不一定是这样。


-

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


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

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