我正在将应用程序从 Windows 移植到 Mac OS X。在下面给出的代码中,我打开给定文件进行写入。我观察到在 Windows 上,它在 second 上失败OpenFileForWrite
。但是在 Mac OS X 上,它允许使用OpenFileForWrite
. 造成这种差异的原因是什么?
代码:
//ON WINDOWS
typedef wchar_t * FileStr;
int OpenFileForWrite (const FileStr filename)
{
return OpenFile (filename, GENERIC_READ | GENERIC_WRITE, 0, OPEN_ALWAYS);
}
int OpenFile (const FileStr filename, ULong func, ULong mode, ULong flag)
{
if(filehandle_) {
Close ();
}
// Save the mode in which the file was opened
openmode_ = mode;
openfunc_ = func;
openflag_ = flag;
// open file
filehandle_ = CreateFile (filename, func, mode, NULL, flag, FILE_ATTRIBUTE_NORMAL, 0);
//CASE : success to open file.. check for unicode file or not and return
if ((filehandle_) && (filehandle_ != INVALID_HANDLE_VALUE)) {
return 0;
}
filehandle_ = NULL;
// not able to open file..set last error
return GetLastError ();
}
//ON MAC OS X
typedef char * FileStr;
int OpenFileForWrite (const FileStr filename)
{
//read, write the file
// no share
// Open always
return OpenFile (filename, O_RDWR|O_CREAT|O_EXCL);
}
int OpenFile (const FileStr filename, Long flag)
{
if(fd_) {
Close ();
}
openmode_ = flag;
fd_ = open(filename, flag);
//CASE : success to open file.. check for unicode file or not and return
if (fd_ > 0) {
return 0;
}
// not able to open file..set last error
return error;
}
另外,我想知道 Mac OS X 上 optionFILE_SHARE_READ
和FILE_SHARE_WRITE
options 的等价物。有没有办法实现FILE_SHARE_READ
和FILE_SHARE_WRITE
使用open
函数或任何其他系统调用的行为?