【发布时间】: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 字符返回