Solved

How can I know if a file is read only?

Posted on 2007-11-14
5
226 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 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

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…
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

735 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