rugrag
rugrag

Reputation: 163

Find a line in a file and extract informations

I have to find within a text file the specific line that starts with a key word and then I have to analyze this line to extract informations. I'll make it clear by an example:

processor       : 0 
vendor_id       : GenuineIntel 
cpu family      : 6 
model           : 5 
model name      : Pentium II (Deschutes) 
stepping        : 2 
cpu MHz         : 400.913520 
cache size      : 512 KB 
fdiv_bug        : no 
hlt_bug         : no 

this is the text file (/proc/cpuinfo from Linux). I have to write a function that parses the file until it finds "model name : " and then it has to store in a char Array the information "Pentium II (Deschutes)". This is what I coded until now:

int get_cpu(char* info)
{
    FILE *fp; 
    char buffer[1024]; 
    size_t bytes_read; 
    char *match; 

    /* Read the entire contents of /proc/cpuinfo into the buffer.  */ 
    fp = fopen("/proc/cpuinfo", "r"); 

    bytes_read = fread(buffer, 1, sizeof (buffer), fp); 

    fclose (fp); 

    /* Bail if read failed or if buffer isn't big enough.  */ 
    if (bytes_read == 0 || bytes_read == sizeof (buffer)) 
        return 0; 

    /* NUL-terminate the text.  */ 
    buffer[bytes_read] == '\0'; 

    /* Locate the line that starts with "model name".  */ 
    match = strstr(buffer, "model name"); 

    if (match == NULL) 
        return 0; 

    /* copy the line */
    strcpy(info, match);
}

it says that the buffer is always not big enough......

Upvotes: 0

Views: 242

Answers (2)

user2371524
user2371524

Reputation:

Well going beyond the simple fact that /proc/cpuinfo is typically bigger than 1024 bytes:

> wc -c </proc/cpuinfo
3756

and such, of course your buffer is to small to read the whole file at once ...

What you try here is to process a text file and the natural way to do it would be line by line.

Try something like

(edit: finally replacing the whole thing with tested code ... it's not so easy to get strtok() right ... hehe)

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

int main(void)
{
    char buf[1024];
    char *val = 0;
    FILE *fp = fopen("/proc/cpuinfo", "r");
    if (!fp)
    {
        perror("opening `/proc/cpuinfo'");
        return 1;
    }

    while (fgets(buf, 1024, fp))        /* reads one line */
    {
        char *key = strtok(buf, " ");   /* gets first word separated by space */
        if (!strcmp(key, "model"))
        {
            key = strtok(0, " \t");     /* gets second word, separated by
                                         * space or tab */
            if (!strcmp(key, "name"))
            {
                strtok(0, " \t");         /* read over the colon */
                val = strtok(0, "\r\n");  /* read until end of line */
                break;
            }
        }
    }

    fclose(fp);

    if (val)
    {
        puts(val);
    }
    else
    {
        fputs("CPU model not found.\n", stderr);
    }
    return 0;
}

usage:

> gcc -std=c89 -Wall -Wextra -pedantic -o cpumodel cpumodel.c
> ./cpumodel
AMD A6-3670 APU with Radeon(tm) HD Graphics

Upvotes: 2

BobRun
BobRun

Reputation: 754

Please try this, it works, there are different ways to do it.

#include <usual.h>

int get_cpu( char *info )
{
  FILE *fp;
  char buffer[1024];
  size_t bytes_read;
  char *match;
  char *matchend;
  /* Read the entire contents of /proc/cpuinfo into the buffer.  */
  fp = fopen( "/proc/cpuinfo", "r" );
  bytes_read = fread( buffer, 1, sizeof( buffer ), fp );
  fclose( fp );
  /* Bail if read failed or if buffer isn't big enough.  */
  if ( bytes_read == 0 || bytes_read == sizeof( buffer ) )
    return 0;
  /* NUL-terminate the text.  */
  buffer[bytes_read] == '\0';
  // match=buffer;
  /* Locate the line that starts with "model name".  */
  match = strstr( buffer, "model name" );
  if ( match == NULL )
    return 0;
  /* copy the line */
  strncpy( info, match, 41 );
}

int main(  )
{
  char info[255];
  memset( info, '\0', 255 );
  get_cpu( info );
  printf( "\nthe data we extracted: %s ", info );
  getchar(  );
}

Upvotes: -1

Related Questions