?
Solved

How can I know if a file is read only?

Posted on 2007-11-14
5
Medium Priority
?
231 Views
Last Modified: 2010-04-01
I developpe an application using visual c++ 6.0 and I need to get information on read only property of some file before overwrite them. How can I do this?
0
Comment
Question by:chouproute
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
5 Comments
 
LVL 30

Expert Comment

by:Axter
ID: 20282972
You can use the stat function or the windows API function GetFileAttributes
0
 
LVL 30

Accepted Solution

by:
Axter earned 1000 total points
ID: 20282991
DWORD attributes = GetFileAttributesA(srcPath);

if (attributes & FILE_ATTRIBUTE_READONLY)
{
   //It's a read only file
}
0
 
LVL 46

Expert Comment

by:Kent Olsen
ID: 20282992
Hi chouproute,

call stat() on the file.  One of the fields is the file's flags, which include the READONLY property.

Of course, it's probably easiest to just open the file for write.  If the open fails, check errno.  If errno is EROFS or EACCES then the file or filesystem is read-only and the application can not write to it.



Good Luck,
Kent
0
 
LVL 30

Expert Comment

by:Axter
ID: 20283037
struct stat my_stat;
stat(FileName, &my_stat);

if  ( ! (my_stat.st_mode &  S_IWRITE) && (my_stat.st_mode &  S_IREAD) )
{
  //It's a read only file
}
0
 
LVL 12

Expert Comment

by:sarangk_14
ID: 20283208
You might find the following links useful:

http://www.devasp.net/net/articles/display/624.html
http://msdn2.microsoft.com/en-us/library/fa0hc0ht(VS.80).aspx

Frankly, I wouldn't bet my life on it, as my knowledge of VC++ can be described as moderate at best ;-)

Hope this helps.
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.
Suggested Courses
Course of the Month12 days, 7 hours left to enroll

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question