Hello,
I am very new in writing low level programming in C. I am trying to get an output in Linux 2.6.17.6 gentoo platform, but I don't get any output.
I am trying to do the following:
I am trying to scan a word and print its content at the standard output by using sscanf and printf.
I am trying to read the cpuinfo at the /proc directory, which looks like as following, and then, I am trying to print the highlighted string.
cat cpuinfo
processor : 0
vendor_id : GenuineIntel
cpu family : 15
model : 2
model name : Intel(R) Celeron(R) CPU 2.60GHz
stepping : 9
cpu MHz : 2591.654
cache size : 128 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 apic sep mtrr pge mca cmov pat pse36 clflush dts acpi mmx fxsr sse sse2 ss ht tm pbe up cid xtpr
bogomips : 5189.98
But, I can only print the model name but cannot print its concent.
My C file looks as below.
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
main(int argc,char **argv) {
char mybuf[4256];
char vernum[116];
int fd;
int errs;
int count;
if (argc != 2) {
printf("Bad command line argument\n");
exit(-1);
}
if ((fd = open (argv[1],O_RDONLY))< 0){
perror("Bad open");
exit(-1);
}
while ((count = read(fd, &mybuf[0], sizeof(mybuf))) > 0) {
count = read(fd,&mybuf[0],sizeof(mybuf));
sscanf(&mybuf[0],"model name %s",&vernum[0]);
printf("The model name is:%s\n", &vernum[0]);
}
if(count < 0) {
perror ("Bad read");
exit (-1);
}
}
Please tell me what I shouls fix at sccaf( ) and at the printf( ) method. I tried a lot by reading the man page to read new line or empty space or \t and by putting %c or *. But I never cannot print anything after model name.
If you need, you can see the file "cpuinfo" at /proc directory. Type cd /proc
then cat cpuinfo.
Please help me; I am here for help as it is a help forum for new and unexperienced like me.
Please tell me what to do in the code and what to write....so that I can print the whole string "model name : Intel(R) Celeron(R) CPU 2.60GHz"
Thank you.
Sharmin