我正在将应用程序从Windows移植到Mac OS X.在下面给出的代码中,我打开给定的文件进行写入。我发现在Windows上,它在第二个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上的选项FILE_SHARE_READ
和FILE_SHARE_WRITE
选项的等价物。有没有办法实现FILE_SHARE_READ
和FILE_SHARE_WRITE
的行为使用open
函数或任何其他系统调用?