Here is the c code:-

#include<stdio.h>
#include<stdlib.h>
#include<string.h>

int main(int argc,char *argv[]){
if(argc==1){    //default 
  printf("first version\n");
  int i,j,k;
  char line[1024],*str1,*str,str2[1024],ch;
  FILE *in=fopen("/proc/cpuinfo","r");
  i=0;
  while((fgets(line,1024,in))!=NULL){
        //  printf("%s",line);
            str=strtok(line, " :");
            if(str!=NULL){                                          //point 1
                if(strcmp(str,"model")==0){
                   printf("%d %s\n",i,str);                         //point 2
                   str=strtok(NULL, " :");
                   printf("%s\n",str);                              //printing "name"
                   if(strcmp(str,"name")==0)                        //point 3
                      printf("CPU Model:=%s\n",strtok(NULL,":"));
                }
                else 
                continue;
                }
    i++;
    }
    fclose(in);
    }
}   

At point 1 string str is "model" but at point 2 it is not printing str and also at point 3 it is not going into if sentence.
Can anyone figure out what's the problem with this code ?
Any help should be appreciated.

At point 1 string str is "model"

How do you know it's not "model\n" or "model "? Neither of those will match "model". For that matter, how do you really know what is in str?

since at line 14 it is printing "model".

Line 14 is a strtok(). Since when does strtok() print?

And I repeat:

How do you know it's not "model\n" or "model "?

I mean at line 14 I have checked that "str".Since strtok() is giving str ending at the character which is not either whitespace or ':' .Do you get it?

Here's a hint for asking questions: if reproduction of your problem depends on the content of a file, it's a good idea to post a small part of that content along with your code. This way we aren't forced to imagine what your file looks like and devise test cases in the hope that we'll hit your problem. Most helpers will just pass this question by because it implies too much work for what promises to be a very simple issue.

Her is the small part of the file "cpuinfo"

processor : 0
vendor_id : GenuineIntel
cpu family : 6
model : 37
model name : Intel(R) Core(TM) i5 CPU M 460 @ 2.53GHz
stepping : 5
cpu MHz : 2533.604
cache size : 3072 KB
physical id : 0
siblings : 4
core id : 0
cpu cores : 2
apicid : 0
initial apicid : 0
fdiv_bug : no
hlt_bug : no

It's the cpuinfo file(Read Only).

Your code runs fine on my end (with minimal tweaks to make it compile as C90). What compiler are you using, what output are you getting (paste it), and what output do you expect (paste it too)?

Actually in file "cpuinfo" there is not only whitespace but also '\t' character before ':'
So I have to insert \t in delimiters in strtok() call.
Thanks for drawing my attention towards this things.

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.