943,515 Members | Top Members by Rank

Ad:
  • C Discussion Thread
  • Unsolved
  • Views: 10071
  • C RSS
Oct 7th, 2005
0

c program to extract system info

Expand Post »
hi everybody.
i am trying to develop a program that will be able to read and export informations (hw, sw, kernel, netstats..ecc) from a linux computer.
my idea was to use the files in the /proc directory but i can't get the script to work - this is what i got so far

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. struct CpuInfo {
  5. char vendor_id[50];
  6. int family;
  7. char model[50];
  8. float freq;
  9. char cache[20];
  10. }
  11.  
  12. int main()
  13. {
  14. struct CpuInfo info = {"", 0, "", 0.0, ""};
  15.  
  16. FILE *cpuInfo;
  17.  
  18.  
  19. if( ( cpuInfo = fopen("/proc/cpuinfo", "rb") == NULL ) ) {
  20. printf("ERRORE! Impossibile aprire il file relativo alla CPU.");
  21. }
  22. else {
  23. while (!feof(cpuInfo)) {
  24. fread(&info, sizeof(struct CpuInfo), 1, cpuInfo);
  25.  
  26. if(info.family !=0) {
  27. printf("%s\n%d\n%s\n%.2f\n%s\n", info.vendor_id, info.family, info.model, info.freq, info.cache);
  28. }
  29. }
  30. }
  31. return 0;
  32. }

and this is the compiler output:


dude@gnixbox:~$ gcc -Wall -o cpuinfo cpuinfo.c
cpuinfo.c:13: error: two or more data types in declaration specifiers
cpuinfo.c:14: warning: return type of ‘main’ is not ‘int’
cpuinfo.c: In function ‘main’:
cpuinfo.c:20: warning: assignment makes pointer from integer without a cast
cpuinfo.c:32: warning: control reaches end of non-void function


anyone got ideas?suggestions?

thanks
Similar Threads
Reputation Points: 11
Solved Threads: 0
Light Poster
trashed is offline Offline
30 posts
since Oct 2004
Oct 7th, 2005
1

Re: c program to extract system info

First, end the structure definition with a semicolon:
struct CpuInfo {
	char vendor_id[50];
	int family;
	char model[50];
	float freq;
	char cache[20];
};
[Dani - here's where I wish I had the ability to make the size bigger.]

Here you need to carefully watch how you use parentheses:
if( ( cpuInfo = fopen("/proc/cpuinfo", "rb") == NULL ) ) {
if( ( cpuInfo = fopen("/proc/cpuinfo", "rb")) == NULL  ) {
Team Colleague
Reputation Points: 2780
Solved Threads: 312
long time no c
Dave Sinkula is offline Offline
4,790 posts
since Apr 2004
Oct 8th, 2005
0

Re: c program to extract system info

thanks!that worked.
but now my problem is how to deal with the input file, in my code i tryed to select the needed infos but the output looks pretty awful.
this is a /proc/cpuinfo formatting

Quote ...
processor : 0
vendor_id : GenuineIntel
cpu family : 6
model : 11
model name : Intel(R) Celeron(TM) CPU 1066MHz
stepping : 1
cpu MHz : 1063.599
cache size : 256 KB
fdiv_bug : no
hlt_bug : no
f00f_bug : no
coma_bug : no
fpu : yes
fpu_exception : yes
cpuid level : 2
wp : yes
flags : fpu vme de pse tsc msr pae mce cx8 sep mtrr pge mca cmov pat pse36 mmx fxsr sse
bogomips : 2105.34
i want my software to copy the vendor_id, cpu family, model name, cpu Mhz , cache size and so i defined the struct
  1. struct CpuInfo {
  2. char vendor_id[50];
  3. int family;
  4. char model[50];
  5. float freq;
  6. char cache[20];
  7. };

but when i try to run the program this is what i get.seems obvious to me it is some kind of mistake with the chars&ints but i'm unable to solve it.

dude@gnixbox:~$ ./cpuinfo
processor : 0
vendor_id : GenuineIntel
cpu family : 6
model : 11
model name : Intel(R) Celeron(TM) CPU 1066MHz
step\uffff\uffffH¿\uffff~\u07b7
1869417014
del : 11
model name : Intel(R) Celeron(TM) CPU 1066MHz
step\uffff\uffffH¿\uffff~\u07b7
0.00
1066MHz
step\uffff\uffffH¿\uffff~\u07b7
ing : 1
cpu MHz : 1063.599
cache size : 256 KB
fdiv_bug : no
hlt_bug : no
f00f_bug : no
coma_bug : no
fpu : yes
fpu_exception : \uffff\uffffH¿\uffff~\u07b7
1735746143
: no
hlt_bug : no
f00f_bug : no
coma_bug : no
fpu : yes
fpu_exception : \uffff\uffffH¿\uffff~\u07b7
51996536886002927864281668446584832.00
es
fpu_exception : \uffff\uffffH¿\uffff~\u07b7
es
cpuid level : 2
wp : yes
flags : fpu vme de pse tsc msr pae mce cx8 sep mtrr pge mca cmov pat pse36 mmx fxsr sse
bogomips : 21\uffff\uffffH¿\uffff~\u07b7
1668510752
msr pae mce cx8 sep mtrr pge mca cmov pat pse36 mmx fxsr sse
bogomips : 21\uffff\uffffH¿\uffff~\u07b7
19680190460165560588342417948672.00
r sse
bogomips : 21\uffff\uffffH¿\uffff~\u07b7
5.34

id level : 2
wp : yes
flags : fpu vme de pse tsc msr pae mce cx8 sep mtrr pge mca cmov pat pse36 mmx fxsr sse
bogomips : 21\uffff\uffffH¿\uffff~\u07b7
1668510752
msr pae mce cx8 sep mtrr pge mca cmov pat pse36 mmx fxsr sse
bogomips : 21\uffff\uffffH¿\uffff~\u07b7
19680190460165560588342417948672.00
r sse
bogomips : 21\uffff\uffffH¿\uffff~\u07b7


thanks again guys
Reputation Points: 11
Solved Threads: 0
Light Poster
trashed is offline Offline
30 posts
since Oct 2004
Oct 8th, 2005
0

Re: c program to extract system info

You are not checking the return code of fread. In addition, you are checking for feof in the wrong spot. Your problem is similar to what's explained in this FAQ: http://www.eskimo.com/~scs/C-faq/q12.2.html
Reputation Points: 114
Solved Threads: 1
Newbie Poster
shric is offline Offline
5 posts
since Sep 2005
Oct 8th, 2005
0

Re: c program to extract system info

Quote originally posted by shric ...
You are not checking the return code of fread. In addition, you are checking for feof in the wrong spot. Your problem is similar to what's explained in this FAQ: http://www.eskimo.com/~scs/C-faq/q12.2.html
ok that solved that problem.
the thing i don't really get is how to tell it to read lines 2+3 then skip line 4 and read line 5 (ecc...)
Reputation Points: 11
Solved Threads: 0
Light Poster
trashed is offline Offline
30 posts
since Oct 2004

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C Forum Timeline: xhtml parser c code
Next Thread in C Forum Timeline: Null Pointer? will this code work





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC