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.
But whenever I type
cat /proc/cpuinfo
on terminal, I am presented with my CPU info.
I can also see the file when I open it with a text editor, such as gedit or leafpad.
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?
The files from
/prochave a size of 0 byte because they are generated on the fly by the kernel.See here for more information on proc filesystem:
http://tldp.org/LDP/Linux-Filesystem-Hierarchy/html/proc.html