/Retreiving information from /proc

This is a discussion on /Retreiving information from /proc within the C Programming forums, part of the General Programming Boards category; Hi there, I'm working on a program to report the behaviour of a linux kernel by printing out variables in ...

  1. #1
    Registered User
    Join Date
    Sep 2009
    Posts
    2

    /Retreiving information from /proc

    Hi there, I'm working on a program to report the behaviour of a linux kernel by printing out variables in certain files which are in /proc/

    I currently have fgets reading it good just can't get strstr working properly. The current value of cpu_type is (null).
    Code:
    #include <stdio.h> 
    #include <string.h> 
    char get_cpu_model() 
    {
       FILE* fp;
       char buffer[4096]; 
       char* match; 
       char cpu_type;
       size_t bytes_read;
       
       fp = fopen("/proc/cpuinfo", "r");
       while(!feof(fp)){
        fgets(buffer,sizeof(buffer),fp);
        match = strstr(buffer, "model name"); 
        printf("%s",buffer);
       }
       fclose (fp);
       int counter = 0;
    
       printf("\nhere1\n");
       printf("%s",match);
       if (match == NULL){
         printf("\nNo Match\n");
         return 0; 
       } else {
       sscanf (match, "model name      : %s", &cpu_type); 
       return cpu_type;
       }
    } 
    
    char main() 
    {
       printf ("CPU Model Name: %s\n", get_cpu_model()); 
       return 0; 
    }

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    main should return an int, not a char. You shouldn't use feof to control loops. You aren't actually doing anything with strstr. You aren't using it to break your loop or anything, so what exactly do you expect it to magically do?


    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Sep 2009
    Posts
    2
    I expect it to read my mind and do what i'm thinking. But, in reality what should i use instead of feof?

  4. #4
    ... kermit's Avatar
    Join Date
    Jan 2003
    Posts
    1,528
    Quote Originally Posted by jack500 View Post
    I expect it to read my mind and do what i'm thinking. But, in reality what should i use instead of feof?
    For your question regarding feof, read this and this.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 5
    Last Post: 06-01-2009, 07:54 PM
  2. Replies: 6
    Last Post: 07-07-2008, 07:48 AM
  3. Assignment Help !! (Student information system)
    By ashb in forum C++ Programming
    Replies: 6
    Last Post: 03-12-2005, 04:32 AM
  4. Going out of scope
    By nickname_changed in forum C++ Programming
    Replies: 9
    Last Post: 10-12-2003, 06:27 PM
  5. Special Allegro Information
    By TechWins in forum Game Programming
    Replies: 12
    Last Post: 08-20-2002, 11:35 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21