By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,356 Members | 1,023 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,356 IT Pros & Developers. It's quick & easy.

File::GetAttributes Is a file read only

P: n/a
Hi,

I'm trying to see if a file (exportfile) is set to read only using
File::GetAttributes:

if ((File::GetAttributes(exportfile.c_str()) &
FileAttributes::ReadOnly) != 0);
{
MessageBox::Show("The File is Read Only","Warning");
}

I've tried that and a few variations but it always returns true.

Any help would be greatly appreciated.

Thanks,

Chris

Nov 17 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
On 28 Mar 2005 06:49:49 -0800, xenny wrote:
Hi,

I'm trying to see if a file (exportfile) is set to read only using
File::GetAttributes:

if ((File::GetAttributes(exportfile.c_str()) &
FileAttributes::ReadOnly) != 0);
{
MessageBox::Show("The File is Read Only","Warning");
}

I've tried that and a few variations but it always returns true.

Any help would be greatly appreciated.


You have an errant semicolon. VC warns about such "empty controlled
statements" at warning level 3 and above.

--
Doug Harrison
Microsoft MVP - Visual C++
Nov 17 '05 #2

P: n/a

Doug Harrison [MVP] wrote:
On 28 Mar 2005 06:49:49 -0800, xenny wrote:
Hi,

I'm trying to see if a file (exportfile) is set to read only using
File::GetAttributes:

if ((File::GetAttributes(exportfile.c_str()) &
FileAttributes::ReadOnly) != 0);
{
MessageBox::Show("The File is Read Only","Warning");
}

I've tried that and a few variations but it always returns true.

Any help would be greatly appreciated.


You have an errant semicolon. VC warns about such "empty controlled
statements" at warning level 3 and above.

--
Doug Harrison
Microsoft MVP - Visual C++

O yeah, good point :-)

But even if I remove that it still does the same thing.

But, I've found a way to make it work:

FileAttributes fa = File::GetAttributes(exportfile.c_str());

if ((fa == ReadOnly) || (fa % 2 == 1))
{
MessageBox::Show("The File is Read Only","Warning");
}

Now that will only show the message box when the file is read only.
And will work even if the file has other attributes as well as the read
only (the second part of the if statement).

Cheers.

Chris

Nov 17 '05 #3

P: n/a
On 28 Mar 2005 09:29:56 -0800, xenny wrote:
O yeah, good point :-)

But even if I remove that it still does the same thing.

But, I've found a way to make it work:

FileAttributes fa = File::GetAttributes(exportfile.c_str());

if ((fa == ReadOnly) || (fa % 2 == 1))
{
MessageBox::Show("The File is Read Only","Warning");
}

Now that will only show the message box when the file is read only.
And will work even if the file has other attributes as well as the read
only (the second part of the if statement).


That's not how to do it. The following should work:

FileAttributes attr = File::GetAttributes(exportfile.c_str());
if (attr == -1)
{
// error
}
else if (attr & FileAttributes::ReadOnly)
{
// File is readonly
}

You have to check it against -1 because that's what GetAttributes returns
in case of error, e.g. a file that doesn't exist.

--
Doug Harrison
Microsoft MVP - Visual C++
Nov 17 '05 #4

P: n/a
On 28 Mar 2005 09:29:56 -0800, "xenny" <xe*****@hotmail.com> wrote:

Doug Harrison [MVP] wrote:
On 28 Mar 2005 06:49:49 -0800, xenny wrote:
> Hi,
>
> I'm trying to see if a file (exportfile) is set to read only using
> File::GetAttributes:
>
> if ((File::GetAttributes(exportfile.c_str()) &
> FileAttributes::ReadOnly) != 0);
> {
> MessageBox::Show("The File is Read Only","Warning");
> }
>
> I've tried that and a few variations but it always returns true.
>
> Any help would be greatly appreciated.


You have an errant semicolon. VC warns about such "empty controlled
statements" at warning level 3 and above.

--
Doug Harrison
Microsoft MVP - Visual C++

O yeah, good point :-)

But even if I remove that it still does the same thing.

But, I've found a way to make it work:

FileAttributes fa = File::GetAttributes(exportfile.c_str());

if ((fa == ReadOnly) || (fa % 2 == 1))
{
MessageBox::Show("The File is Read Only","Warning");
}

Now that will only show the message box when the file is read only.
And will work even if the file has other attributes as well as the read
only (the second part of the if statement).


All you need is:

if (fa & ReadOnly) {
}

or

if (File::GetAttributes(exportfile.c_str()) & ReadOnly) {
}
--
Sev
Nov 17 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.