唯一的文件名生成 [英] Unique file name generation

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

问题描述



大家好,


我们遇到了需要生成唯一文件名的问题。

我有一些想法,但也想征求

组的建议。


对方案的任何建议,仅使用* standard * C ++ ,要做到这一点?


谢谢,

戴夫


Hello all,

We''re presented with the problem of needing to generate a unique file name.
I''ve had some thoughts, but also wanted to solicit suggestions from the
group.

Any suggestions for schemes, using only *standard* C++, to do this?

Thanks,
Dave

推荐答案

Dave Theese写道:
Dave Theese wrote:
大家好,

我们遇到了需要生成一个独特文件名称的问题。我有一些想法,但也想从小组中征求建议。

对方案的任何建议,只使用* standard * C ++来做这个?
Hello all,

We''re presented with the problem of needing to generate a unique file
name. I''ve had some thoughts, but also wanted to solicit suggestions
from the group.

Any suggestions for schemes, using only *standard* C++, to do this?




如果你想坚持使用标准的C ++,那么(恕我直言)唯一的方法就是试用和试错。
试错。创建一个随机文件名(如果你想要偏执,那么在POSIX文件名限制内是
)。试着看看它是否在那里,如果没有,那么如果它是
一个,那就换一个新的随机值。


如果你准备好使用平台具体的代码,那么你可能会要求你自己的进程ID(也可能是MT中的线程ID)并使用

那些。我对POSIX不太熟悉,但它可能为两者提供功能

- 这也是标准的。并可移植到许多平台。


-

WW aka Attila



If you want to stick to standard C++ then (IMHO) the only way to do it is
trial and error. Make a random filename (if you want to be paranoid then
within the POSIX filename limits). Try to see if it is there, if not make
one if it is there, make a new random value.

If you are ready to use platform specific code, then you might be able to
ask for your own process ID (possibly thread ID as well in MT) and use
those. I am not much familiar with POSIX, but it might provide functions
for both - and that is standard, too. And portable to many platforms.

--
WW aka Attila


White Wolf写道:
White Wolf wrote:
Dave Theese写道:
Dave Theese wrote:
大家好,

我们遇到了需要生成一个唯一文件的问题
名称。我有一些想法,但也想从小组中征求建议。

对方案的任何建议,只使用* standard * C ++来做这个?
Hello all,

We''re presented with the problem of needing to generate a unique file
name. I''ve had some thoughts, but also wanted to solicit suggestions
from the group.

Any suggestions for schemes, using only *standard* C++, to do this?



如果你想坚持使用标准的C ++,那么(恕我直言)唯一的方法就是试用和试错。创建一个随机文件名(如果你想要偏执,那么
在POSIX文件名限制内)。试着看看它是否在那里,如果没有那么一个,如果它在那里,做一个新的随机值。

如果你准备使用平台特定代码,那么你可能能够要求你自己的进程ID(也可能是MT中的线程ID)并使用
那些。我对POSIX并不熟悉,但它可能为两者提供功能 - 这也是标准功能。并可移植到许多平台。


If you want to stick to standard C++ then (IMHO) the only way to do it is
trial and error. Make a random filename (if you want to be paranoid then
within the POSIX filename limits). Try to see if it is there, if not make
one if it is there, make a new random value.

If you are ready to use platform specific code, then you might be able to
ask for your own process ID (possibly thread ID as well in MT) and use
those. I am not much familiar with POSIX, but it might provide functions
for both - and that is standard, too. And portable to many platforms.




您描述的内容存在竞争条件。


保证这一点的唯一方法一个文件不是要使用

" exclusive"模式(unix上的O_EXCL)并创建文件。


我认为使用std :: ftream没有办法做到这一点。



There is a race condition in what you describe.

The only way to guarentee that a file is not there is to use the
"exclusive" mode (O_EXCL on unix) and create the file.

I don''t think there is any way to do this using std::ftream.


Gianni Mariani写道:
Gianni Mariani wrote:
White Wolf写道:
White Wolf wrote:
Dave Theese写道:
Dave Theese wrote:
大家好,

我们遇到了需要生成唯一
文件名的问题。我有一些想法,但也想从小组中征求
建议。

对方案的任何建议,只使用* standard * C ++来做到这一点?
Hello all,

We''re presented with the problem of needing to generate a unique
file name. I''ve had some thoughts, but also wanted to solicit
suggestions
from the group.

Any suggestions for schemes, using only *standard* C++, to do this?



如果你想坚持使用标准的C ++,那么(恕我直言)唯一的办法就是试错了。创建一个随机文件名(如果你想在
偏执,那么在POSIX文件名限制内)。试着看看它是否存在,如果没有,那就制作一个新的随机值。

如果你准备使用特定于平台的代码,那么你可能会
能够询问您自己的进程ID(可能还有MT中的线程ID)并使用它们。我对POSIX并不熟悉,但它可能会为两者提供功能 - 这也是标准的。并且
可以移植到许多平台。


If you want to stick to standard C++ then (IMHO) the only way to do
it is trial and error. Make a random filename (if you want to be
paranoid then within the POSIX filename limits). Try to see if it
is there, if not make one if it is there, make a new random value.

If you are ready to use platform specific code, then you might be
able to ask for your own process ID (possibly thread ID as well in
MT) and use those. I am not much familiar with POSIX, but it might
provide functions for both - and that is standard, too. And
portable to many platforms.



你所描述的内容存在竞争条件。

保证文件不是唯一的方法有使用
独家的模式(unix上的O_EXCL)并创建文件。

我认为使用std :: ftream没有办法做到这一点。



There is a race condition in what you describe.

The only way to guarentee that a file is not there is to use the
"exclusive" mode (O_EXCL on unix) and create the file.

I don''t think there is any way to do this using std::ftream.




默认情况下,正在创建的AFAIK文件在所有系统上都是独占打开的。

另外AFAIK fstream * only *是独占打开的,因为如果它没有,那么

应该提供锁定功能。


-

WW又名阿提拉



AFAIK files being created are opened exclusive on all systems by default.
Also AFAIK fstream *only* does exclusive open, since if it did not, it
should have provided locking capabilities.

--
WW aka Attila


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

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