【发布时间】:2012-01-24 18:56:49
【问题描述】:
我已经能够编写一个可以读取任何文本文件的程序...除了 /proc 中的文件。我尝试从 /proc 读取的任何文件都显示为空。
但每当我输入时
cat /proc/cpuinfo
在终端上,我看到了我的 CPU 信息。
当我使用文本编辑器(例如 gedit 或 Leafpad)打开文件时,我也可以看到该文件。
所以看起来 /proc 文件确实是文本文件,但是我的 C 程序很难读取它们。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
char* readFileString( char* loc ) {
char *fileDat;
FILE * pFile;
long lsize;
pFile = fopen( loc, "r" );
// Grab the file size.
fseek(pFile, 0L, SEEK_END);
lsize = ftell( pFile );
fseek(pFile, 0L, SEEK_SET);
fileDat = calloc( lsize + 1, sizeof(char) );
fread( fileDat, 1, lsize, pFile );
return fileDat;
}
int main( void ) {
char *cpuInfo;
cpuInfo = readFileString( "/proc/cpuinfo" );
printf( "%s\n", cpuInfo );
return 0;
}
知道为什么吗?
【问题讨论】: