| | |
c program to extract system info
Please support our C advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: Oct 2004
Posts: 29
Reputation:
Solved Threads: 0
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
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
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
C Syntax (Toggle Plain Text)
#include <stdio.h> #include <stdlib.h> struct CpuInfo { char vendor_id[50]; int family; char model[50]; float freq; char cache[20]; } int main() { struct CpuInfo info = {"", 0, "", 0.0, ""}; FILE *cpuInfo; if( ( cpuInfo = fopen("/proc/cpuinfo", "rb") == NULL ) ) { printf("ERRORE! Impossibile aprire il file relativo alla CPU."); } else { while (!feof(cpuInfo)) { fread(&info, sizeof(struct CpuInfo), 1, cpuInfo); if(info.family !=0) { printf("%s\n%d\n%s\n%.2f\n%s\n", info.vendor_id, info.family, info.model, info.freq, info.cache); } } } return 0; }
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
First, end the structure definition with a semicolon: [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:
struct CpuInfo {
char vendor_id[50];
int family;
char model[50];
float freq;
char cache[20];
};Here you need to carefully watch how you use parentheses:
if( ( cpuInfo = fopen("/proc/cpuinfo", "rb") == NULL ) ) {
if( ( cpuInfo = fopen("/proc/cpuinfo", "rb")) == NULL ) {
"One of the methods used by statists to destroy capitalism consists in establishing controls that tie a given industry hand and foot, making it unable to solve its problems, then declaring that freedom has failed and stronger controls are necessary." --Ayn Rand
•
•
Join Date: Oct 2004
Posts: 29
Reputation:
Solved Threads: 0
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
i want my software to copy the vendor_id, cpu family, model name, cpu Mhz , cache size and so i defined the struct
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
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
•
•
•
•
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
C Syntax (Toggle Plain Text)
struct CpuInfo { char vendor_id[50]; int family; char model[50]; float freq; char cache[20]; };
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
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
•
•
Join Date: Oct 2004
Posts: 29
Reputation:
Solved Threads: 0
•
•
•
•
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
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...)
![]() |
Similar Threads
- How to Find System Info Through Java (Java)
- how to update data in mysql table using data from an excel table (MySQL)
- Extract header info from image file (Python)
- system info and hardware troubleshooting (Windows NT / 2000 / XP)
Other Threads in the C Forum
- Previous Thread: xhtml parser c code
- Next Thread: Null Pointer? will this code work
| Thread Tools | Search this Thread |
#include adobe api array arrays asterisks binarysearch char cm copyimagefile copypdffile cprogramme creafecopyofanytypeoffileinc createcopyoffile csyntax database directory dynamic execv feet fflush fgets file fork forloop framework frequency getlasterror givemetehcodez global grade graphics gtkgcurlcompiling hacking hardware highest homework i/o include incrementoperators infiniteloop input interest kernel keyboard kilometer linked linkedlist linux linuxsegmentationfault list lists locate logical_drives looping loopinsideloop. match matrix meter microsoft motherboard mqqueue mysql number odf opensource owf pattern pdf performance pointer posix probleminc process program programming radix recursion recv repetition research scanf scripting segmentationfault sequential shape socket socketprograming stack standard string systemcall testing threads turboc unix user voidmain() wab windows.h






