使用 stat 的分段错误(核心转储)
Segmentation Fault (core dumped) using stat
我需要我的程序显示有关文件的信息。所以这是我的代码
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char *argv[]){
struct stat fileStat;
if (argc != 2) {
fprintf(stderr, "Usage: %s <pathname>\n", argv[0]);
exit(0);
}
if (stat(argv[1], &fileStat) == -1) {
exit(1);
}
printf("ID ", fileStat.st_uid);
printf("Dydis: \t\t%d bytes\n" + fileStat.st_size);
}
但是我得到这个错误
Segmentation Fault (core dumped)
有什么问题吗?
您需要更改密码
printf("Dydis: \t\t%d bytes\n" + fileStat.st_size);
到
printf("Dydis: \t\t%d bytes\n", fileStat.st_size);
^
|
notice this change
参考:根据C11
标准,章节§7.21.6.3,语法是,
int printf(const char * restrict format, ...);
我需要我的程序显示有关文件的信息。所以这是我的代码
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char *argv[]){
struct stat fileStat;
if (argc != 2) {
fprintf(stderr, "Usage: %s <pathname>\n", argv[0]);
exit(0);
}
if (stat(argv[1], &fileStat) == -1) {
exit(1);
}
printf("ID ", fileStat.st_uid);
printf("Dydis: \t\t%d bytes\n" + fileStat.st_size);
}
但是我得到这个错误
Segmentation Fault (core dumped)
有什么问题吗?
您需要更改密码
printf("Dydis: \t\t%d bytes\n" + fileStat.st_size);
到
printf("Dydis: \t\t%d bytes\n", fileStat.st_size);
^
|
notice this change
参考:根据C11
标准,章节§7.21.6.3,语法是,
int printf(const char * restrict format, ...);