在文件中查找一行并提取信息

时间:2015-10-19 21:45:59

标签: c stdio string.h

我必须在文本文件中找到以关键字开头的特定行,然后我必须分析此行以提取信息。我将通过一个例子说清楚:

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 

这是文本文件(来自Linux的/ proc / cpuinfo)。我必须编写一个解析文件的函数,直到找到“model name:”,然后它必须在char数组中存储信息“Pentium II(Deschutes)”。 这就是我现在编码的内容:

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);
}

它说缓冲区总是不够大......

2 个答案:

答案 0 :(得分:1)

超越/proc/cpuinfo通常大于1024字节的简单事实:

> wc -c </proc/cpuinfo
3756

等等,当然你的缓冲区很小,可以一次读取整个文件......

您在此处尝试处理文本文件,自然的方法是逐行

尝试类似

的内容

编辑:最后用经过测试的代码替换整个内容......它不是那么容易让strtok()正确...呵呵)

#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;
}

用法:

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

答案 1 :(得分:0)

请尝试这个,它有效,有不同的方法可以做到。

#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(  );
}