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

#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:

struct CpuInfo {
	char vendor_id[50];
	int family;
	char model[50];
	float freq;
	char cache[20];
}[B];[/B]

[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  ) {

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

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

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

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...)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.