Link to home
Start Free TrialLog in
Avatar of jaypappas
jaypappas

asked on

Example of a C++ file that displays and manipulates file attributes

I am in need of anExample of a C++ program file that displays and/or manipulates file attributes
Avatar of jkr
jkr
Flag of Germany image

File attributes are not primarily a matter of C++, but the underlying operating system. Which one are you targetting and what attributes do you need to manipulate? 'stat()' probably offers the most portable way of reading them, see http://www.minek.com/files/unix_examples/statfile.html ("stat() example") - e.g.
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <time.h>
#include <pwd.h>

void report(char *, struct stat *);

void main(int argc, char * argv[])
/* Print status of files on argument list. */
{
   struct stat status_buf;
   if (argc < 2)
   {
      fprintf(stderr, "statfile file1 ...\n");
      exit(-1);
   }
   while(--argc)
   {
      if( stat(*++argv, &status_buf) < 0 )
         perror(*argv);
      else
         report(*argv, &status_buf);
   }
}

/* Possible access modes... one for each octal value. */
char * accesses[] = {"...", "..x", ".w.", ".wx", "r..", "r.x", "rw.", "rwx"};

void report( char * name, struct stat * buffer)
/* Decode and present the status information. */
{
   int i;
   struct passwd * passent;
   ushort mode = buffer -> st_mode;
   printf("\n %s :\n", name);
   printf(" Last access : %s\n", ctime( &(buffer -> st_atime)) );
   printf(" Last modification : %s\n", ctime( &(buffer -> st_mtime)));
   printf(" Last status change : %s\n", ctime( &(buffer -> st_ctime)));
   printf(" Current file size : %ld \n", buffer -> st_size);

   /* type dev_t is int*/
   printf(" Directory entry is on device %d.\n", buffer -> st_dev);

   /* type ino_t is unsigned  */
   printf(" Inode number is %lu\n", buffer -> st_ino);

   /* Identify the owner by number and by name. */
   passent = getpwuid(buffer -> st_uid);
   if(passent != NULL)
      printf("The owner of the file is #%d - %s\n",
          buffer -> st_uid, passent -> pw_name);
   else
      printf(" The owner of the file is #%d - unknown\n", buffer -> st_uid);

   printf(" Access mode 0%o: ", mode);
   for(i = 6; i >= 0; i -=3)
      printf("%s", accesses[(mode >> i) & 7]);
   printf("\n");
}

Open in new window

Avatar of jaypappas
jaypappas

ASKER

This is a windows operating sysytem program.  I need to manipulate read and write
Well - then here you go:
DWORD dwAtt = GetFileAttributes(_T("c:\\path\\file.txt");

// check for 'readonly'
if (dwAtt & FILE_ATTRIBUTE_READONLY) {

  // remove 'readonly'

  dwAtt &= ~FILE_ATTRIBUTE_READONLY;

  SetFileAttributes(_T("c:\\path\\file.txt"),dwAtt);
}

Open in new window

Is it a way to print a list of user/groups and what permission a user has on a file
OK, so yo uare talking about NTFS ACLs in that case? Yes, there is, but that's a bit more complicated. Could you be a bit more specific?
I attached a sample file with a screen print of permissions.  I want to list each user and group permissions and be able to modify permissions on the group and/or user.
file-attr.docx
ASKER CERTIFIED SOLUTION
Avatar of jkr
jkr
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
This question has been classified as abandoned and is closed as part of the Cleanup Program. See the recommendation for more details.