Solved

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

Posted on 2011-09-28
9
264 Views
Last Modified: 2012-05-12
I am in need of anExample of a C++ program file that displays and/or manipulates file attributes
0
Comment
Question by:jaypappas
  • 4
  • 3
9 Comments
 
LVL 86

Expert Comment

by:jkr
ID: 36718696
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

0
 

Author Comment

by:jaypappas
ID: 36719876
This is a windows operating sysytem program.  I need to manipulate read and write
0
 
LVL 86

Expert Comment

by:jkr
ID: 36719959
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

0
Technology Partners: 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!

 

Author Comment

by:jaypappas
ID: 36720767
Is it a way to print a list of user/groups and what permission a user has on a file
0
 
LVL 86

Expert Comment

by:jkr
ID: 36769779
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?
0
 

Author Comment

by:jaypappas
ID: 36815759
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
0
 
LVL 86

Accepted Solution

by:
jkr earned 500 total points
ID: 36818004
Hmm, cannot check your file from here (hint: If it is a screen print, upload a picture, not a Word document. The following article wil lshow you how to check permissions: http://www.codeproject.com/KB/asp/useraccesscheck.aspx ("Check User’s Permissions On A File or Folder"). To grant access to a specific file or folder, you can use the following:
#include <windows.h>
#include <aclapi.h>

#pragma comment(lib,"advapi32.lib")

void main () {

 LPTSTR FileName = "C:\\tmp\\Directory";
 LPTSTR TrusteeName = "User";

 DWORD InheritFlag = SUB_CONTAINERS_AND_OBJECTS_INHERIT;
 ACCESS_MODE option = GRANT_ACCESS;
 EXPLICIT_ACCESS explicitaccess;

 PACL NewAcl = NULL;

 DWORD dwError = 0;

 CreateDirectory ( FileName, NULL);


 BuildExplicitAccessWithName(
       &explicitaccess,
       TrusteeName,
       GENERIC_READ | STANDARD_RIGHTS_READ | GENERIC_WRITE | STANDARD_RIGHTS_READ,
       SET_ACCESS,
       InheritFlag
       );


 //
 // add specified access to the object
 //

 dwError = SetEntriesInAcl(
         1,
         &explicitaccess,
         NULL,
         &NewAcl
         );

 //
 // apply new security to file
 //

 dwError = SetNamedSecurityInfo(
                 FileName,
                 SE_FILE_OBJECT, // object type
                 DACL_SECURITY_INFORMATION,
                 NULL,
                 NULL,
                 NewAcl,
                 NULL
                 );

}

Open in new window

0
 
LVL 40

Expert Comment

by:evilrix
ID: 37419288
This question has been classified as abandoned and is closed as part of the Cleanup Program. See the recommendation for more details.
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone 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

Suggested Solutions

Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

679 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