首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >读取DRM EDID文件时大小始终为零

读取DRM EDID文件时大小始终为零
EN

Stack Overflow用户
提问于 2014-08-08 19:23:03
回答 1查看 155关注 0票数 1

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

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

代码:

代码语言:javascript
复制
#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;
}

输出:

代码语言:javascript
复制
file opened
file size: 0

===

按照MSalters的建议,我尝试使用stat作为文件大小。也返回0。我假设代码是正确的,所以不知何故不可能访问该文件?

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

代码:

代码语言:javascript
复制
#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;
}

输出

代码语言:javascript
复制
0

===

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

EN

回答 1

Stack Overflow用户

发布于 2014-08-08 19:31:14

我怀疑fseek(f, 0, 2);可能指的是fseek(f, 0, SEEK_CUR);,它显然没有做任何事情。你可能想要不可移植的SEEK_END,但是/sys/也不是。(当然,使用#include <stdio.h>)

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

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/25202393

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档