Solved

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

Posted on 2011-09-28
9
268 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
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 pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.

632 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