我尝试读取连接到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的文件大小。
发布于 2014-08-08 19:31:14
我怀疑fseek(f, 0, 2);可能指的是fseek(f, 0, SEEK_CUR);,它显然没有做任何事情。你可能想要不可移植的SEEK_END,但是/sys/也不是。(当然,使用#include <stdio.h>)
但是考虑到它已经是特定于Linux的,为什么不使用stat呢?
https://stackoverflow.com/questions/25202393
复制相似问题