开发者

Using Struct Stat()

I'm trying to figure out how exactly to use stat() to capture information about a file. What I need is to be able to print several fields of information about a file. So..

 #include <iostream>
 #include <sys/types.h>
 #include <sys/stat.h>
 #include <fcntl.h>
 using namespace std;

 int main() {
     struct stat buf;
     stat("file",&buf);
               ...
     cout << st_dev << endl;
     cout << st_ino << endl;
     cout << st_mode << endl;
     cout << st_nlink << endl;
     cout << st_uid << endl;
     cout << st_gid << endl;
     cout << st_rdev << endl;
     cout << st_size << endl;
     cout << st_blksize << endl;
     cout << st_blocks << endl;
     cout << st_atime << endl;
     cout << st_mtime << endl;
     cout << st_ctime << endl;
     ...
 }

I'm thoroughly confused about how to do this. Why is &buf a parameter to stat? I don't care about storing this information in memory, I just need the开发者_如何学JAVA outputted fields within my c++ program. How do I access the information contained in the struct? Is buf actually supposed to contain the returned information from stat()?


Yes, buf is being used here as an out-parameter. The results are stored in buf and the return value of stat is an error code indicating if the stat operation succeeded or failed.

It is done this way because stat is a POSIX function, designed for C, which does not support out-of-band error reporting mechanisms like exceptions. If stat returned a struct, then it would have no way to indicate errors. Using this out-parameter method also allows the caller to choose where they want to store the results, but that's a secondary feature. It's perfectly fine to pass the address of a normal local variable, just like you have done here.

You access the fields of a struct like you would any other object. I presume you are at least familar with object notation? E.g. the st_dev field within the stat struct called buf is accessed by buf.st_dev. So:

cout << buf.st_dev << endl;

etc.


For another project, I've whipped up a little function that does something similiar to what you need. Take a look at sprintstatf.

Here's an example of usage:

#include <sys/stat.h>
#include <stdlib.h>
#include <stdio.h>

#include "sprintstatf.h"

int
main(int argc, char *argv[])
{
    char *outbuf = (char *)malloc(2048 * sizeof(char));
    struct stat stbuf;
    char *fmt = \
        "st_atime (decimal) = \"%a\"\n"
        "st_atime (string)  = \"%A\"\n"
        "st_ctime (decimal) = \"%c\"\n"
        "st_ctime (string)  = \"%C\"\n"
        "st_gid   (decimal) = \"%g\"\n"
        "st_gid   (string)  = \"%G\"\n"
        "st_ino             = \"%i\"\n"
        "st_mtime (decimal) = \"%m\"\n"
        "st_mtime (string)  = \"%M\"\n"
        "st_nlink           = \"%n\"\n"
        "st_mode  (octal)   = \"%p\"\n"
        "st_mode  (string)  = \"%P\"\n"
        "st_size            = \"%s\"\n"
        "st_uid             = \"%u\"\n"
        "st_uid             = \"%U\"\n";

    lstat(argv[1], &stbuf);

    sprintstatf(outbuf, fmt, &stbuf);
    printf("%s", outbuf);

    free(outbuf);
    exit(EXIT_SUCCESS);
}

/* EOF */


This question may be way to old to comment but i am posting this as a reference

To get a good understanding about stat() function ,the reason for passing the stat reference and more importantly error handling are explained good in the below link

stat - get file status


You have several errors in your code:

  • You need &buf, with a single 'f'.
  • You need to say e.g. buf.st_dev when printing, since st_dev is a field in the struct variable.

Since buf is a local variable on the stack, you're not "saving the values to memory" permanently, it's just as long as that variable is in-scope.

This is how you return multiple values, typically, in C and C++. You pass a pointer to a structure, and the function being called fills in the structure with the values it has computed for you.


buf is the structure that stat loads with the information about the file you pass in the first parameter. You pass &buf here b/c you have buf allocated on the stack as a local variable and you must pass a pointer to the stat function to enable it to load the data.

All variables of st_* are part of the struct stat object and thus must be accessed via your local buf variable as buf.st_uid, etc.


Similar thing is with ctime library. Is designed similar way. First is to create empty struct. You have access to object of the struct, but all fields are empty. Then You use that function (&name-of-created-obiect) and is an adrres to point obiect outside of that function. Function is designed to store all info to that struct obiect from given reference, and kaboom, you have obiect with ready data to use.

Otherwise, if You don't want use pointer, then you must use Obiect = function(null);

With pointer Function(&obiect);

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜