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
Solved

How can I know if a file is read only?

Posted on 2007-11-14
5
224 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
  • 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 250 total points
ID: 20282991
DWORD attributes = GetFileAttributesA(srcPath);

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

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: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering 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

  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

839 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