Solved

How can I know if a file is read only?

Posted on 2007-11-14
5
218 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:Kdo
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

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
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 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 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.

744 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now