C语言 在 C 上正确使用 Stat
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3138600/
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
Correct use of Stat on C
提问by necronet
Why does this work :
为什么这样做:
char *fd = "myfile.txt";
struct stat buf;
stat(fd, &buf);
int size = buf.st_size;
printf("%d",size);
But this does not work:
但这不起作用:
char *fd = "myfile.txt";
struct stat *buf;
stat(fd, buf);
int size = buf->st_size;
printf("%d",size);
回答by Puppe
The reason for it not working is that buf in the first example is allocated on the stack. In the Second example you only have a pointer to a struct stat, pointing to anywhere (probably pointing to address 0x0, i.e. a NULL pointer), you need to allocate memory for it like this:
它不起作用的原因是第一个示例中的 buf 是在堆栈上分配的。在第二个示例中,您只有一个指向 struct stat 的指针,指向任何地方(可能指向地址 0x0,即 NULL 指针),您需要为它分配内存,如下所示:
buf = malloc(sizeof(struct stat));
Then both examples should work. When using malloc(), always remember to use free()after you are finished with using the struct stat:
那么这两个例子都应该有效。使用时malloc(),请务必在使用free()完后记得使用struct stat:
free(buf);
回答by Muhammad Anjum Kaiser
It is just a simple memory allocation problem.
这只是一个简单的内存分配问题。
char *fd = "myfile.txt";
struct stat *buf;
stat(fd, buf);
int size = buf->st_size;
printf("%d",size);
The above code only declares a pointer, but in reality, there is no memory space allocated.
上面的代码只声明了一个指针,但实际上并没有分配内存空间。
you should modify the code to look like this:
您应该将代码修改为如下所示:
char *fd = "myfile.txt";
struct stat *buf;
buf = malloc(sizeof(struct stat));
stat(fd, buf);
int size = buf->st_size;
printf("%d",size);
free(buf);
This will allocate the memory, and free after it is used.
这将分配内存,并在使用后释放。
回答by Didier Trosset
In the second, you use a pointer pointing to you-dont-know-where. statwill by chance be able to correctly fill the values in the area pointed (your program could have terminated abruptly here). Then, as you have no idea on where this data is, you use it buf->st_sizebut maybe someone has used this memory area that you don't own.
在第二个中,您使用指向您不知道的位置的指针。stat将偶然能够正确填充所指向区域中的值(您的程序可能在这里突然终止)。然后,由于您不知道这些数据在哪里,您可以使用它,buf->st_size但也许有人使用了您不拥有的内存区域。
回答by Lajos Arpad
It's a big difference between creating a structure or a pointer to a structure. The first code creates the structure, the second creates a pointer, to a not existing structure. Using malloc or calloc you can allocate memory and your structure will be initialized. After this you do whatever you want and at the moment when you don't need this structure anymore, you must use the free() function to free the allocated space.
创建结构或指向结构的指针之间有很大的区别。第一个代码创建结构,第二个代码创建一个指向不存在结构的指针。使用 malloc 或 calloc 您可以分配内存并且您的结构将被初始化。在这之后你可以做任何你想做的事情,当你不再需要这个结构时,你必须使用 free() 函数来释放分配的空间。
回答by Kumar Alok
You haven't allocated any memory for your pointer dude.
你还没有为你的指针家伙分配任何内存。
You should allocate memory to buf.
您应该为 buf 分配内存。
buf = malloc(sizeof(struct stat));
now it will work.
现在它将起作用。
回答by CyberSkull
This should fix your problem and another: file size can be either a 32 or 64-bit int. This example assumes a 64-bit machine.
这应该可以解决您的问题和另一个问题:文件大小可以是 32 位或 64 位整数。此示例假定为 64 位机器。
#include <stat.h>
#include <errno.h>
char *file = "myfile.txt";
long long size; //st_size can be a 64-bit int.
struct stat *buf = malloc(sizeof(struct stat)); //allocates memory for stat structure.
errno = 0; //always set errno to zero first.
if(stat(file, buf) == 0)
{
size = buf->st_size;
printf("Size of \"%s\" is %lld bytes.\n", file, size);
}
else
{
perror(file); //if stat fails, print a diagnostic.
}

