【问题标题】:Size is always zero when reading DRM EDID file读取 DRM EDID 文件时,大小始终为零
【发布时间】:2014-08-08 19:38:56
【问题描述】:

我试图读取连接到 LVDS1 的显示器的 EDID。我使用 ArchLinux 和 C++/clang。我的问题是:文件大小总是返回 0。我不知道这是编程问题还是特定于操作系统的问题,其他文件返回正确的文件大小。是特殊文件吗?是符号链接目录/sys/class/drm/card0-DP-1 有问题吗?

文件:/sys/class/drm/card0-LVDS-1/edid

代码:

#include <fstream>
#include <iostream>

using namespace std;

typedef unsigned char BYTE;

long
get_file_size(FILE *f)
{
    long pos_cursor, pos_end;
    pos_cursor = ftell(f);
    fseek(f, 0, 2);
    pos_end = ftell(f);
    fseek(f, pos_cursor, 0);
    return pos_end;
}

int
main()
{
    const char *filepath = "/sys/class/drm/card0-LVDS-1/edid";
    FILE *file = NULL;

    if((file = fopen(filepath, "rb")) == NULL)
    {
        cout << "file could not be opened" << endl;
        return 1;
    }
    else
        cout << "file opened" << endl;

    long filesize = get_file_size(file);
    cout << "file size: " << filesize << endl;

    fclose(file);

    return 0;
}

输出:

file opened
file size: 0

===

按照 MSalters 的建议,我尝试了 stat 文件大小。也返回 0。我认为代码是正确的,所以无法访问该文件?

我还尝试了符号链接目标路径 (/sys/devices/pci0000:00/0000:00:02.0/drm/card0/card0-LVDS-1/edid),以防万一出现问题 - 但仍然是 0。

代码:

#include <iostream>
#include <sys/stat.h>

using namespace std;

int
main()
{
    const char *filepath = "/sys/class/drm/card0-LVDS-1/edid";
    struct stat results;
    
    if (stat(filepath, &results) == 0)
        cout << results.st_size << endl;
    else
        cout << "error" << endl;

    return 0;
}

输出

0

===

我尝试了同一目录中的其他文件 (dpms edid enabled i2c-6 modes power status subsystem uevent)。除了edid之外,它们都返回4096的文件大小。

【问题讨论】:

  • C++代码不多,文件里的东西都是C代码。
  • @MSalters 哦,但它“是”C++:using namespace std;
  • 你是对的,我只是用谷歌搜索并从这里复制代码:dreamincode.net/forums/topic/…
  • 不知道是不是权限问题。如果以root身份运行,它的工作方式有什么不同吗?使用cat(或xxd)之类的命令,是否会列出同一帐户下的内容?
  • cat 可以工作,但它会将二进制文件作为 ascii 字符返回

标签: c++ linux


【解决方案1】:

我怀疑fseek(f, 0, 2); 可能意味着fseek(f, 0, SEEK_CUR); 显然没有任何作用。你会想要SEEK_END,它不是可移植的,但是/sys/ 也不是。 (当然是#include &lt;stdio.h&gt;

但考虑到它已经是特定于 Linux 的,为什么不使用 stat 呢?

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-03-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-10-11
    • 1970-01-01
    相关资源
    最近更新 更多