I'm trying to write a small C program that reads the Pi's serial number, just the serial number and not all the other information, to do this I'm using fseek
here is my code, based on a few C examples and tutorials:
int main()
{
FILE *fp = fopen("/proc/cpuinfo", "r");
fseek(fp, -10, SEEK_END);
int ch = getc(fp);
while (ch != EOF)
{
/* display contents of file on screen */
putchar(ch);
ch = getc(fp);
}
if (feof(fp))
printf("\n End of file reached.");
else
printf("\n Something went wrong.");
fclose(fp);
return 0;
}
I'm not getting any warnings or errors when compiling but it always seems to ignore the fseek and prints out the whole file. What am I doing wrong, or not doing right?
fseek(). The entries in/procaren't no real files. Usegrepandcutto get the serial number. – ott-- Jun 05 '16 at 09:52open()andread()into a small buffer, then parse the buffer however you like. That method is totally reliable (I've use it to do ongoing multiple per second reads for extended time periods, which is howtopand other system monitor tools work). – goldilocks Jun 05 '16 at 11:38