C++ 使用结构 Stat()
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3512434/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Using Struct Stat()
提问by Dan Snyder
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..
我试图弄清楚如何使用 stat() 来捕获有关文件的信息。我需要的是能够打印有关文件的多个信息字段。所以..
#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 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()?
我对如何做到这一点感到非常困惑。为什么 &buf 是 stat 的参数?我不在乎将这些信息存储在内存中,我只需要我的 c++ 程序中的输出字段。如何访问结构中包含的信息?buf 实际上应该包含从 stat() 返回的信息吗?
回答by Tyler McHenry
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.
是的,buf
这里用作输出参数。结果存储在buf
,返回值stat
是一个错误代码,指示stat
操作是成功还是失败。
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
returneda 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.
这样做是因为它stat
是一个为 C 设计的 POSIX 函数,它不支持像异常这样的带外错误报告机制。如果stat
返回一个结构体,那么它将无法指示错误。使用这个 out-parameter 方法还允许调用者选择他们想要存储结果的位置,但这是次要功能。传递普通局部变量的地址完全没问题,就像你在这里所做的那样。
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:
您可以像访问任何其他对象一样访问结构体的字段。我想你至少熟悉对象符号?例如,被调用st_dev
的stat
结构中的字段buf
由 访问buf.st_dev
。所以:
cout << buf.st_dev << endl;
etc.
等等。
回答by Jon Bringhurst
For another project, I've whipped up a little function that does something similiar to what you need. Take a look at sprintstatf.
对于另一个项目,我创建了一个小函数,可以完成与您需要的类似的功能。看看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 */
回答by Ram
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() 函数,传递 stat 引用的原因和更重要的错误处理在下面的链接中有很好的解释
回答by unwind
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, sincest_dev
is a field in the struct variable.
- 你需要
&buf
, 带有一个'f'。 - 你需要说例如
buf.st_dev
在打印时,因为st_dev
是结构变量中的一个字段。
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.
由于buf
是堆栈上的局部变量,因此您不会永久“将值保存到内存中”,只要该变量在范围内即可。
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.
这就是您通常在 C 和 C++ 中返回多个值的方式。你传递一个指向结构的指针,被调用的函数用它为你计算的值填充结构。
回答by RC.
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.
buf
是 stat 加载的结构,其中包含有关您在第一个参数中传递的文件的信息。您&buf
在此处传递已buf
在堆栈上分配的b/c作为局部变量,并且您必须将指针传递给 stat 函数以使其能够加载数据。
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.
的所有变量st_*
都是 struct stat 对象的一部分,因此必须通过本地buf
变量 asbuf.st_uid
等访问。