Friday, February 4, 2022

[SOLVED] How to get the model name from /proc/cpuinfo in linux?

Issue

I am working in C and trying to get specific information from /proc files. I know in linux when I do the following I get the model name.

cd /proc
cat cpuinfo | grep 'model name'

but if I'm trying to do this in C it results in a core dumped

thisfile = fopen("/proc/cpuinfo | grep 'model name' ", "r");

How can I get the model name when opening a file?


Solution

fopen("/proc/cpuinfo | grep 'model name' ", "r"); will return a NULL pointer because the file /proc/cpuinfo | grep 'model name' certainly doesn't not exist

fopen allows to open a file, not to execute commands

Use popen :

FILE * fp = popen("grep 'model name'  /proc/cpuinfo", "r");

if (fp != NULL) {
  ...read in 
  pclose(fp);
}


Answered By - bruno
Answer Checked By - Clifford M. (WPSolving Volunteer)