Expiring Today—Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

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

Posted on 2011-09-28
9
Medium Priority
?
276 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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 

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 2000 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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
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.

719 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