- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
#include "stdio.h"
#include <sys/stat.h>
int
main(int argc, char *argv[]) {
struct stat buf;
//int fd = open("./fstatat.c", "r");
//int fd2 = fstatat(fd, "a.txt", &buf, 0);
//printf("%d\n", buf.st_ino);
stat("./fstatat.c", &buf);
printf("%d\n", buf.st_ino);
return 0;
}
如果我使用函数 stat 获取结构 stat,则 st_ino 与带 ls -i 的 i 节点编号相同。
1305609
[inmove@localhost chapter-four]$ ls -i
1305607 a.txt 1305606 fstatat.bin 1305609 fstatat.c 1305605 tmp.txt
buf 如果我使用函数 fstat,st_ino 总是 4195126。
谁能告诉我为什么会这样?
最佳答案
问题是您没有正确使用 open
并且没有检查返回值是否有错误。因此,您随后在 open
错误返回的无效文件描述符值 -1
上调用 fstat
,这也会失败并且不会触及 buf
,所以结构中未初始化的垃圾仍然存在(4195126
,十六进制 0x400336
闻起来很像先前函数调用的返回地址仍在堆栈中或类似的东西。)
正如 davmac 已经指出的,open
的第二个参数必须是标志列表,它们是数字。检查docs .
所以,正确的代码应该是:
#include "stdio.h"
#include <sys/stat.h>
#include <sys/fcntl.h> // for the O_RDONLY constant
#include <errno.h> // for error output
int main(int argc, char *argv[]) {
struct stat buf;
int fd = open("./fstatat.c", O_RDONLY);
if(fd == -1) {
printf("Error calling open: %s\n", strerror(errno));
} else {
if(fstat(fd, &buf) == -1) {
printf("Error calling fstat: %s\n", strerror(errno));
} else {
printf("%d\n", buf.st_ino);
if(close(fd) == -1) {
printf("Error calling close: %s\n", strerror(errno));
}
}
}
return 0;
}
关于对 st_ino 感到困惑?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37135110/
#include "stdio.h" #include int main(int argc, char *argv[]) { struct stat buf; //int fd =
这个问题已经有答案了: stat outputting the wrong values for files in a directory (1 个回答) 已关闭 9 年前。 我正在编写一个程序来打印
找不到合适的文档。 在E-tag header中使用。 为了什么目的? 谁能用两个词描述它是什么? 最佳答案 这是 inode 号。它也是 struct stat 的成员,在 stat.h 中. 关于
这是来自 UNP Richard Stevens 的代码副本 #include "apue.h"
我是一名优秀的程序员,十分优秀!