What is the equivalent of _sopen_s () for the open () function?

Microsoft has deprecated _open in favor of _sopen_s. What are the recommended equivalent arguments?

+4
source share
3 answers

Well, now I can post my beautifully formatted answer, here it is:

Per

int fd = _open(name,oflags); 

Replace

 int fd; errno_t errno = _sopen_s(&fd,name,oflags,_SH_DENYNO,0); 

The reason I posted these questions and answers is because they contain important non-obvious information that Microsoft did not provide. I agree that _sopen_s is a bad replacement for _open. I investigated this topic only because I corrected the error caused by invalid arguments passed to _sopen_s. _Sopen_s was only in code to get rid of compiler warnings; The original _open call was ok.

+8
source

Best recommendation ignores all Microsoft politically motivated impairment. Their “safe” functions do not provide any real security benefits when used properly with the existing standard C or POSIX functions, and if they are used incorrectly, they are “unsafe” as the functions they want to replace.

0
source

On Windows, I suggest using CreateFile if there is no good reason not to. On Linux, "open" maps to the kernel function, on Windows to the library function, which in any case uses the "CreateFile". In accordance with portability, this function is slightly more portable on Microsoft platforms (Win32 / 64 / CE). And, of course, not available on * nix.

0
source

All Articles