tags:

views:

74

answers:

2

Hi,

I tried to read the /proc/modules using standard c functions:

FILE *pfile;
int sz;
pfile = fopen( "/proc/modules", "r" );
fseek( pfile, 0, SEEK_END );
sz = ftell( pfile );
rewind( ftell );

But my problem is ftell give me 0 value. So I can't read the contents of the file since I have a zero length. Is there another way that I can get the size of the file that I want to read?

Many thanks.

+6  A: 

No, it does not have a size. However, you can read parts of it until you reach end-of-file.

Sjoerd
Exactly. A "file" in /proc will always return size=0 but if you go ahead and read it anyway you will get data.
joefis
$ find /proc -type f -size +0 2>/dev/null /proc/config.gz...
adobriyan
The app gives me Segmentation Fault when I am looking for the EOF using fgetc.
sasayins
Maybe it's better to open a new question about the segfault, with the code you have so far.
Sjoerd
+2  A: 

/proc files are dynamically created when you read them, so they cannot have a size.

I stand corrected. Some /proc files do indeed have a size, as adobriyan has noted on a comment to Sjoerd's answer. (Is that Alexey Dobriyan of Linux Kernel fame?)

As for how to read the file using fgetc, this works:

int c;
while ( (c = fgetc(pfile)) != EOF) {
    printf("%c",c);
}

And your program is segfaulting because you're trying to rewind ftell.

ninjalj
Is there any possible way that I can read the dynamic data of the procfs precisely. I did what Sjoerd suggested using fgetc but it gives me a segmentation fault.
sasayins