Simple question:
How to check the access mode to an already open file pointer?
So to say, the function is passed by the already open FILE pointer:
bool PseudoFunction(FILE *Ptr)
{
if( ... Insert check for read-only access rights )
{
return true;
}
return false;
}
What would I use in an if statement to test a FILE pointer, be open as read-only (or, as the case may be), without writing to a file and not relying on a user passing (possibly conflicting) arguments?
The system is a windows, code :: blocks compiler, but cross-compatibility is preferred for code portability interests.
Note that this does not ask about file permissions, but what access mode was used by the FILE pointer.
SELF-ANSWER [Unable to add a separate response due to user rights restrictions]:
, #defines
, , FILE _flag ( _iobuf) , . , , , , :
#define READ_ONLY_FLAG 1
bool PrintFlagPtr(const char FileName[], const char AccessMode[])
{
FILE *Ptr = NULL;
Ptr = fopen(FileName,AccessMode);
printf("%s: %d ",AccessMode,Ptr->_flag);
int IsReadOnly = Ptr->_flag;
fclose(Ptr);
Ptr = NULL;
if( (IsReadOnly&READ_ONLY_FLAG) == READ_ONLY_FLAG )
{
printf("File is read only!\n");
return true;
}
printf("\n");
return false;
}
, , :
Output:
w: 2
r: 1 File is read only!
a: 2
wb: 2
rb: 1 File is read only!
ab: 2
w+: 128
r+: 128
a+: 128
w+b: 128
r+b: 128
a+b: 128
, ( ), - front-end ( , , ), const int, FILE _flag .