我想使用 C++ 来获取机器的 CPU 信息,我的代码是:
ifstream cpu_info_file("/proc/cpuinfo", ifstream::in|ifstream::binary);
streampos sp = cpu_info_file.tellg();
cout << sp << endl;
cpu_info_file.seekg(0, ifstream::end);
sp = cpu_info_file.tellg();
cout << sp << endl;
cpu_info_file.seekg(-10, ifstream::cur);
sp = cpu_info_file.tellg();
cout << sp << endl;
cpu_info_file.seekg(0, ifstream::beg);
sp = cpu_info_file.tellg();
cout << sp << endl;
但我得到了输出:
0
-1
-1
-1
之后我将 /proc/cpuinfo 复制到我的项目路径,并修改代码:
ifstream cpu_info_file("cpuinfo",ifstream::in|ifstream::binary);
我得到了正确的输出:
0
10424
10414
0
我花了一整天的时间试图找出原因,但是如果我打开任何文件/proc
并使用file.seekg(n, ifstream::end)
,则返回的file.tellg()
将始终为-1,并且我无法再移动streampos。
那么为什么我无法将 /proc 下的文件作为我的项目目录下的文件读取呢?