Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/59.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 分割错误打印文件特征_C++_C - Fatal编程技术网

C++ 分割错误打印文件特征

C++ 分割错误打印文件特征,c++,c,C++,C,我试图编写一个简单的ls命令实现,接近ls-l,但并不完全相同 这是我到目前为止所拥有的 #include <sys/types.h> #include <sys/stat.h> #include <dirent.h> #include <unistd.h> #include <iostream> #include <stdio.h> #include <time.h> using namespace std;

我试图编写一个简单的
ls
命令实现,接近
ls-l
,但并不完全相同

这是我到目前为止所拥有的

#include <sys/types.h>
#include <sys/stat.h>
#include <dirent.h>
#include <unistd.h>
#include <iostream>
#include <stdio.h>
#include <time.h>

using namespace std;
int main(int argc, char **argv)
{
    if(argc != 2)
    {
        cout << "Usage: ./Asg4 <directoryName>" << endl;
        return -1;
    }

    struct stat sb;
    DIR *d;
    struct dirent *dir;
    d = opendir(argv[1]);

    if(d)
    {
        while((dir = readdir(d)) != NULL)
        {
            stat(dir->d_name, &sb);
            printf("%ld\t %s\t%lo\t%ld\t%lld\t%s\n", sb.st_ino, sb.st_dev, (unsigned long) sb.st_mode, (long) sb.st_nlink, (long long) sb.st_size, ctime(&sb.st_mtime));
        }
        closedir(d);
    }
    return 0;
}

这似乎解决了分段错误。

当我编译时,我收到一些令人讨厌的警告:

xc.cpp:28:54: warning: format specifies type 'long' but the argument has type '__darwin_ino64_t' (aka 'unsigned long long') [-Wformat]
            printf("%ld\t %s\t%lo\t%ld\t%lld\t%s\n", sb.st_ino, sb.st_dev, (unsigned long) sb.st_mode, (long) sb.st_nlink, (long long) sb.st_size, ctime(&sb.st_mtime));
                    ~~~                              ^~~~~~~~~
                    %llu
xc.cpp:28:65: warning: format specifies type 'char *' but the argument has type 'dev_t' (aka 'int') [-Wformat]
            printf("%ld\t %s\t%lo\t%ld\t%lld\t%s\n", sb.st_ino, sb.st_dev, (unsigned long) sb.st_mode, (long) sb.st_nlink, (long long) sb.st_size, ctime(&sb.st_mtime));
                          ~~                                    ^~~~~~~~~
                          %d
这些看起来像会崩溃的错误。

它也是一个很好的广告,使用C++而不是C。C++流没有这个问题。

< p>编译时,我得到一些讨厌的警告:

xc.cpp:28:54: warning: format specifies type 'long' but the argument has type '__darwin_ino64_t' (aka 'unsigned long long') [-Wformat]
            printf("%ld\t %s\t%lo\t%ld\t%lld\t%s\n", sb.st_ino, sb.st_dev, (unsigned long) sb.st_mode, (long) sb.st_nlink, (long long) sb.st_size, ctime(&sb.st_mtime));
                    ~~~                              ^~~~~~~~~
                    %llu
xc.cpp:28:65: warning: format specifies type 'char *' but the argument has type 'dev_t' (aka 'int') [-Wformat]
            printf("%ld\t %s\t%lo\t%ld\t%lld\t%s\n", sb.st_ino, sb.st_dev, (unsigned long) sb.st_mode, (long) sb.st_nlink, (long long) sb.st_size, ctime(&sb.st_mtime));
                          ~~                                    ^~~~~~~~~
                          %d
这些看起来像会崩溃的错误。

它也是一个很好的使用C++而不是C的广告。C++流没有这个问题。查看前面的两个问题:,查看@LokiAstari关于代码崩溃原因的答案。此外,您还应该明确地检查
stat
的返回值。首先,您并不是在
stat
-ing您认为是的文件。查看前面的两个问题:,查看@LokiAstari关于代码崩溃原因的答案。另外,您应该明确地检查
stat
的返回值。具体地说,使用
%s
st_dev
打印为字符串肯定会崩溃<代码>st_dev是一个数字,而不是字符串。我用我所做的编辑了我的问题,出于某种原因,我没有收到这些警告。谢谢你@Loki Astari!具体来说,使用
%s
st_dev
打印为字符串肯定会崩溃<代码>st_dev是一个数字,而不是字符串。我用我所做的编辑了我的问题,出于某种原因,我没有收到这些警告。谢谢你@Loki Astari!