Discussion:
Opening a file for writing with open() from io.h?
(too old to reply)
Espen Ruud Schultz
2003-07-10 14:28:38 UTC
Permalink
When I use the open() function from io.h to open a file ( or create if it
doesn't exist ) the file is always created with the read only flag. This is
the basic of my code:

int FileHandler = open( FileName.c_str(), O_CREAT | O_TRUNC | O_WRONLY |
O_BINARY );
if( FileHandler == -1 ) { delete[] SaveFileData; return
Status::ErrorOpeningFile; }

int FileWrite = write( FileHandler, SaveFileData,
FileData.FileHeader.FileSize );
if( FileWrite == -1 ) { delete[] SaveFileData; close( FileHandler );
return Status::ErrorWritingFile; }

int FileClose = close( FileHandler );
if( FileClose == -1 ) { delete[] SaveFileData; close( FileHandler );
return Status::ErrorClosingFile; }

This happens with created files or existing files. I remember having this
problem before, but then it only happened when creating files, not when
writing to existing ones. But then I was dealing with text files, and now I
do binary...

How do I fix this?

TIA!

, Espen
Espen Ruud Schultz
2003-07-10 15:16:20 UTC
Permalink
"CheckAbdoul" <636865636B6162646F756C406E6F7370616D2E6D7670732E6F7267>
The documentation of open() API says the pmode argument is required
only when _O_CREAT is specified. So try adding _S_IREAD | _S_IWRITE as
the pmode value ( the third parameter ).
Thanx, but what header are those declared in? Searching on msdn only pops
up a single result...

, Espen
CheckAbdoul
2003-07-10 15:23:23 UTC
Permalink
try
#include <sys\stat.h>

--
Cheers
Check Abdoul [ VC++ MVP ]
-----------------------------------
Post by Espen Ruud Schultz
"CheckAbdoul" <636865636B6162646F756C406E6F7370616D2E6D7670732E6F7267>
The documentation of open() API says the pmode argument is required
only when _O_CREAT is specified. So try adding _S_IREAD | _S_IWRITE as
the pmode value ( the third parameter ).
Thanx, but what header are those declared in? Searching on msdn only pops
up a single result...
, Espen
Espen Ruud Schultz
2003-07-10 15:47:09 UTC
Permalink
"CheckAbdoul" <636865636B6162646F756C406E6F7370616D2E6D7670732E6F7267>
Post by CheckAbdoul
try
#include <sys\stat.h>
Yeah, that made it compile. But still the read only flag is set. Now it's
even set so I can't write to the new file ( or existing file with read only
flag not set ). Before I could at least write to the file, so I guess then
the read only flag was set when closed, but now it's set when created ( or
opened )...

This is the flags I set now:
O_CREAT | O_TRUNC | S_IREAD | S_IWRITE | O_BINARY

, Espen

Loading...