changing file attributes (windows) in C/C++

sciencewhiz

Diamond Member
Jun 30, 2000
5,885
8
81
Is there a way to make a file read only in windows in C/C++. I figure I can probably do it with a system call to attrib, but that doesn't seem to be a very clean way to do it.

Basically, I want to create a file, write data to it, close it, make it read only.
 

oog

Golden Member
Feb 14, 2002
1,721
0
0
if it's C++ on a Windows machine, you'll want to use the Win32 API to set the file attributes:

BOOL SetFileAttributes(LPCTSTR lpFileName, DWORD dwFileAttributes);

The first parameter is the filename, specified as an array of TCHARs (which are single byte or multiple byte characters depending on the OS) and the second parameter is a bitfield for the different attributes you can set.

More information can be found here: http://msdn.microsoft.com/library/defau.../en-us/fileio/fs/setfileattributes.asp

 

Barnaby W. Füi

Elite Member
Aug 14, 2001
12,343
0
0
Yeah chmod is posix, and supposedly NT is posix-compliant .. or something like that. But it's probably not very idiomatic.