Click to See Complete Forum and Search --> : how to read /proc/cpuinfo
I use C function "fgets" to read a line from /proc/cpuinfo and then try to pick out string "cpu MHz" and "model name" , but I failed to get the strings I want out. However, fgets only reads in 3 characters in a line, which is different from what displays on the screen when I use command "cat /proc/cpuinfo". It seems the newline character displayed on the sreen is not the newline character identified by function "fgets". Why? How to get out the aforementioned strings using C code? Thank you very much!!!:)
bwkaz
01-19-2003, 10:44 AM
From man fgets:
fgets() reads in at most one less than size characters from stream and stores them into the buffer pointed to by s. Reading stops after an EOF or a newline. If a newline is read, it is stored into the buffer. A '\0' is stored after the last character in the buffer. So what does your code look like? Are you passing a 3 as the size argument to fgets?
Thanks for your reply!
I passes sizeof(line) to fgets(). Here is a snippet of my code:
#include <stdio.h>
#include <string.h>
#include <unistd.h>
int main(int argc, char *argv[])
{
char *string[]={"model name", "cpu MHz"};
FILE *in1, *out;
char *line;
if (argc!=3)
{
fprintf(stderr, "Usage: proc CPUinfo_file out_file\n");
exit(1);
}
if ((in1=fopen(argv[1],"r"))==NULL)
{
perror(argv[1]);
exit(1);
}
if ((out=fopen(argv[2],"w+"))==NULL)
{
perror(argv[2]);
exit(1);
}
/*
*original code
*/
/*
while (fgets(line, sizeof(line), in1)!=NULL)
{
if (strstr(line, string[0])!=NULL)
{
fputs(line,out);
}
if (strstr(line,string[1])!=NULL)
{
fputs(line,out);
}
}
*/
/************************************************** **/
/*
*test code
*/
if ((fgets(line, sizeof(line), in1)!=NULL)
fputs(line,out);
/************************************************** */
fclose(in1);
fclose(out);
exit(0);
}
You can try my test code to see what will happen. If you get a solution, please tell me.
:)
bwkaz
01-19-2003, 01:58 PM
OK, you can't just declare a string by saying "char *variable;". You HAVE to allocate space for that string somehow. In your case, malloc would work, or you could change the declaration to something like "char line[128];" or something.
Also, sizeof() doesn't report the number of characters in a char *. That's the job of strlen(), but even strlen() is dependent on having a string in there already. Nothing in C keeps track of how much memory you allocate with malloc()!
I don't know what sizeof() does with something like char line[128], because I've never tried using sizeof with a char array. You could try just printing the size of it.
When you say sizeof(char *), which is what you were effectively saying, you get 4. Always. Because any pointer's size is 4 -- 4 bytes for the address the pointer contains. So you were passing 4 into fgets, and it was reading 3 characters and making the fourth one a '\0' (string terminator). And storing those 4 bytes in a random memory location, which is why it's surprising you didn't get a segfault.
Try doing a "line=malloc(BUF_SIZ);", then using BUF_SIZ on the fgets line. You'll also have to #define BUF_SIZ to something, perhaps 128 or 256, higher up in the code (pref. at the top of the source file).
Hi, Dear Bwkaz, I have one more question. In file /proc/meminfo, there are a lot of data. Which one is the total amount of virtual memory, SwapTotal or Swap? (Actually these two data are somewhat different.) How is it counted? Thank you very much!!:)
bwkaz
01-20-2003, 10:26 AM
The "Swap" line has three columns after it: total, used, and free. These are measured in bytes.
The SwapTotal line is the exact same as the total column on the Swap line, except it is measured in kilobytes there. 1kB = 1024 bytes, so that's why the numbers look different. They represent the same value.
However, neither of these is the total amount of virtual memory. Virtual memory is MemTotal+SwapTotal. The total amount of swap space is SwapTotal, but virtual memory is both types of memory -- physical and on-disk.