问题描述
我已经能够写一个程序,可以读取任何文本文件......除了在/ proc中找到的。我试着从读取任何文件/ proc显示了空。
I have been able to write a program that can read any text files... except the ones found in /proc. Any file that I try to read from /proc shows up empty.
但每当我键入
cat /proc/cpuinfo
在终端,我与我的CPU信息psented $ P $。
on terminal, I am presented with my CPU info.
我也可以看到该文件时,我用文本编辑器打开它,如gedit或leafpad。
I can also see the file when I open it with a text editor, such as gedit or leafpad.
如此看来,/ proc中的文件确实是文本文件,但我的C程序有一个很难阅读。
So it seems that /proc files are indeed text files, but my C program is having a hard time reading them.
#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;
}
任何想法,为什么?
Any idea why?
推荐答案
从的/ proc
该文件有一个大小为0字节,因为它们是由实时生成内核。
The files from /proc
have a size of 0 byte because they are generated on the fly by the kernel.
在这里看到更多信息,proc文件系统:
See here for more information on proc filesystem:
http://tldp.org/LDP/Linux-Filesystem-Hierarchy/html/proc.html
这篇关于任何想法,为什么我的C code不能从/ proc读?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!