C++ 使用Struct Stat()
我正试图弄清楚如何准确地使用stat()来捕获有关文件的信息。我需要的是能够打印关于一个文件的多个信息字段。所以C++ 使用Struct Stat(),c++,struct,posix,C++,Struct,Posix,我正试图弄清楚如何准确地使用stat()来捕获有关文件的信息。我需要的是能够打印关于一个文件的多个信息字段。所以 #include <iostream> #include <sys/types.h> #include <sys/stat.h> #include <fcntl.h> using namespace std; int main() { struct stat buf; stat("file",&
#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;
...
}
操作是成功还是失败 之所以这样做,是因为#包括 #包括 #包括 #包括 使用名称空间std; int main(){ 结构统计buf; stat(“文件”、&buf); ...
您的代码中有几个错误:由于
- 您需要
,带有一个“f”&buf
- 打印时需要说,例如
,因为buf.st_dev
是struct变量中的一个字段st_dev
是堆栈上的一个局部变量,因此您不会永久地“将值保存到内存”,只要该变量在范围内即可buf
<>这是您返回多个值的方法,通常是在C++和C++中。您传递一个指向结构的指针,调用的函数用它为您计算的值填充结构。 是,BUF 这里是一个OUT参数。结果存储在<代码> BUF和“代码> STATstat
stat
是为C设计的POSIX函数,它不支持异常等带外错误报告机制。如果stat
返回了一个结构,那么它将无法指示错误。使用此out参数方法还允许调用方选择将结果存储在何处lts,但这是第二个特性。传递一个普通局部变量的地址是非常好的,就像你在这里做的那样
您可以像访问任何其他对象一样访问结构的字段。我假定您至少熟悉对象表示法?例如,stat
struct中名为buf
的st\u dev
字段由buf.st\u dev
访问。因此:
cout << buf.st_dev << endl;
coutbuf
是stat加载第一个参数中传递的文件信息的结构。在这里传递&buf
,b/c在堆栈上分配了buf
作为局部变量,并且必须传递指向stat函数的指针以使其能够加载数据
st.*
的所有变量都是struct stat对象的一部分,因此必须通过本地buf
变量作为buf.st.\u uid
等进行访问。对于另一个项目,我设计了一个小函数,它的功能与您需要的类似。请看
下面是一个用法示例:
#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 */
#包括
#包括
#包括
#包括“sprintstatf.h”
int
main(int argc,char*argv[])
{
char*exputf=(char*)malloc(2048*sizeof(char));
结构统计stbuf;
字符*fmt=\
“时间(十进制)=\%a\\n”
“时间(字符串)=\%A\\n”
“时间(十进制)=\%c\\n”
“时间(字符串)=\%C\\n”
“st_gid(十进制)=\%g\”\n”
“st_gid(字符串)=\%G\”\n”
“st_ino=\%i\”\n”
“st\u mtime(十进制)=\%m\\n”
“st\u mtime(字符串)=\%M\\n”
“st\U nlink=\%n\”\n”
“STU模式(八进制)=\%p\”\n”
“st\U模式(字符串)=\%P\\n”
“st\U大小=\%s\\n”
“st\u uid=\%u\\n”
“st\U uid=\%U\”\n;
lstat(argv[1],&stbuf);
sprintstatf(突发事件、fmt和stbuf);
printf(“%s”,f);
免费(免费);
退出(退出成功);
}
/*EOF*/
这个问题可能是一个很老的评论方式,但我将其作为参考发布
为了更好地理解stat()函数,下面的链接详细解释了传递stat引用的原因以及更重要的错误处理
ctime库也有类似的功能。设计方式也类似。
首先是创建空结构。
您可以访问结构的对象,但所有字段均为空。
然后使用该函数(&created obect的名称),并将obect指向该函数之外。
函数的设计目的是将给定引用中的所有信息存储到该结构对象中,而kaboom,则表示您有一个对象,该对象具有可用的数据
否则,若不想使用指针,则必须使用
对象=函数(空)
带指针
函数(&OBECT)我明白了,谢谢大家的回答。它们非常有用。有人知道如何使用stdin、stdout或stderr作为stat的参数吗?这些是流而不是文件,所以stat如何用数据填充结构?使用fstat
,它将文件描述符作为其第一个参数,而不是路径。通常我会这样做,但环境有些人为,因为这是我编写的架构模拟器的操作系统部分。因此,当一个文件被系统调用打开时,我有自己的向量作为文件描述符表。有没有一种好方法可以在文件打开时捕获机器上的实际文件描述符?如果是这样,我会使用fstat因为流统计数据和文件统计数据之间不需要翻译。你应该把这些问题作为一个单独的问题来问,这样更多的人可以看到它们,这样就比在评论中有更多的空间来写。可以。我不确定这是不是一个微不足道的问题。