爱在三部曲音乐:sopen函数怎么用阿??

来源:百度文库 编辑:中科新闻网 时间:2024/05/04 16:14:13
sopen函数怎么用阿?? 这是个什么函数阿?

sopen不是ANSI C/C++ 的标准函数,它是某编译器的扩展函数,没有通用性.

VC++中有 _sopen函数,[注意名字前面有个下横线符号],它用于打开文件供多程序分享. 用法:

_sopen, Open a file for sharing.
int _sopen( const char *filename, int oflag, int shflag [, int pmode ] );
int _wsopen( const wchar_t *filename, int oflag, int shflag [, int pmode ] );

_sopen 要求的头文件 <io.h>
其他选项头文件 <fcntl.h>, <sys/types.h>, <sys/stat.h>, <share.h>

程序例子(打开分享文件,锁定字节,打开锁定字节):

/* LOCKING.C: This program opens a file with sharing. It locks
* some bytes before reading them, then unlocks them. Note that the
* program works correctly only if the file exists.
*/

#include <io.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/locking.h>
#include <share.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>

void main( void )
{
int fh, numread;
char buffer[40];

/* Quit if can't open file or system doesn't
* support sharing.
*/
fh = _sopen( "locking.c", _O_RDWR, _SH_DENYNO,
_S_IREAD | _S_IWRITE );
if( fh == -1 )
exit( 1 );

/* Lock some bytes and read them. Then unlock. */
if( _locking( fh, LK_NBLCK, 30L ) != -1 )
{
printf( "No one can change these bytes while I'm reading them\n" );
numread = _read( fh, buffer, 30 );
printf( "%d bytes read: %.30s\n", numread, buffer );
lseek( fh, 0L, SEEK_SET );
_locking( fh, LK_UNLCK, 30L );
printf( "Now I'm done. Do what you will with them\n" );
}
else
perror( "Locking failed\n" );

_close( fh );
}