读取DR​​M EDID文件时,大小始终为零

时间:2014-08-08 11:23:03

标签: c++ linux

我试着读取连接到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的文件大小。

1 个答案:

答案 0 :(得分:0)

我怀疑fseek(f, 0, 2);可能意味着fseek(f, 0, SEEK_CUR);显然没有做任何事情。您希望SEEK_END不可移植,但是/sys/也不是。 (当然,做#include <stdio.h>

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