Solved

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

Posted on 2011-09-28
9
266 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
[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
  • 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: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering 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

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

739 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