简体   繁体   English

读取/ proc / cpuinfo并在C中打印匹配行

[英]Read /proc/cpuinfo and print matching line in C

I am currently working on a C program to get specific CPU Information like the cpu model or how many cores the cpu has. 我目前正在使用C程序来获取特定的CPU信息,例如cpu模型或cpu具有多少个内核。

On Linux one way is to read "/proc/cpuinfo". 在Linux上,一种方法是读取“ / proc / cpuinfo”。 Problem: I don't want everything to be printed out. 问题:我不希望所有内容都被打印出来。 Only the lines containing "model name" and "cpu cores" is what I want to be printed out. 我只想打印出包含“模型名称”和“ cpu核心”的行。

  1. Open file and print its content: done 打开文件并打印其内容: 完成
#include <stdio.h>

int main() {
    char cpuinfo;
    FILE *fp = fopen("/proc/xcpuinfo", "r");

    if (fp == NULL) {
        fprintf(stderr, "Error opening file\n");
    } else {
        while ((cpuinfo = fgetc(fp)) != EOF) {
            printf("%c", cpuinfo);      
        }
        fclose(fp);
    }

    return 0;
}
  1. Print only the lines containing "model name" & "cpu cores": not done 只打印包含“模型名称”和“ cpu核心”的行: 未完成

A simple solution with POSIX getline : 使用POSIX getline简单解决方案:

#define _GNU_SOURCE
#include <stdio.h>
#include <string.h>
#include <assert.h>
#include <errno.h>
#include <stdlib.h>

int main() {
        FILE *fp = fopen("/proc/cpuinfo", "r");
        assert(fp != NULL);
        size_t n = 0;
        char *line = NULL;
        while (getline(&line, &n, fp) > 0) {
                if (strstr(line, "model name") || strstr(line, "cpu cores")) {
                        printf("%s", line);
                }
        }
        free(line);
        fclose(fp);
        return errno;
}

Without getline you can use big enough line buffer char buf[200]; 如果没有getline ,则可以使用足够大的行缓冲区char buf[200]; and use fgets(buf, sizeof(buf), fp) or you could implement a simple getline yourself. 并使用fgets(buf, sizeof(buf), fp)或自己实现一个简单的getline

Another option is to pass the filename and terms to search for as arguments to the program rather than hardcode the filename and search terms. 另一种选择是将文件名和术语作为搜索的参数传递给程序,而不是硬编码文件名和搜索词。 With your search terms, you can compare against the same number of leading characters in each string with strncmp and output any matching lines (or store them, etc..) 使用搜索字词,您可以使用strncmp与每个字符串中相同数量的前导字符进行比较,并输出任何匹配的行(或存储它们等)。

While fgetc will read a single character at a time, fgets (like POSIX getline ) are line-oriented input functions and will read an entire line at a time (provide with a sufficiently sized buffer -- don't skimp on buffer size ) 尽管fgetc只能读取一个字符,但fgets (如POSIX getline )是面向行的输入函数,并且一次可以读取整行(提供足够大小的缓冲区- 不要跳过缓冲区大小

Putting things together so that the program accepts the filename to read as the first argument, and then any number of search terms (to the limits of argv ) as subsequent arguments, you could do something similar to: 将所有内容放在一起,以便程序将要读取的文件名作为第一个参数,然后将任意数量的搜索词(达到argv的限制)作为后续参数,您可以执行以下操作:

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

#define MAXC 2048   /* if you need a constant, #define one (or more) */

int main (int argc, char **argv) {

    char buf[MAXC]; /* buffer to hold each line in file */ 
    FILE *fp = argc > 1 ? fopen (argv[1], "r") : NULL;  /* open file */

    if (!fp) {  /* validate file open for reading */
        perror ("file open failed");
        return 1;
    }

    while (fgets (buf, MAXC, fp)) {                 /* read each line */
        for (int i = 2; i < argc; i++) {            /* loop over terms */
            char *cmpstr = argv[i];                 /* compare string */
            size_t cmplen = strlen (cmpstr);        /* length to compare */
            if (strncmp (buf, cmpstr, cmplen) == 0) /* does start match? */
                fputs (buf, stdout);                /* output string */
        }
    }
    fclose (fp);                                    /* close file */

    return 0;
}

Example Use/Output 使用/输出示例

$ ./bin/cmpleadingstr2 /proc/cpuinfo "model name" "cpu cores"
model name      : Intel(R) Core(TM) i7-2620M CPU @ 2.70GHz
cpu cores       : 2
model name      : Intel(R) Core(TM) i7-2620M CPU @ 2.70GHz
cpu cores       : 2
model name      : Intel(R) Core(TM) i7-2620M CPU @ 2.70GHz
cpu cores       : 2
model name      : Intel(R) Core(TM) i7-2620M CPU @ 2.70GHz
cpu cores       : 2

or 要么

$ ./bin/cmpleadingstr2 /proc/cpuinfo "processor"
processor       : 0
processor       : 1
processor       : 2
processor       : 3

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM