2017-02-13 131 views
2

請耐心等待,因爲我仍然是編程新手。我需要閱讀/proc/cpuinfo確定路由器的型號,文件看起來像這樣:C程序 - 如何讀取文件並將其文本存儲在變量中?

system type    : bcm63xx/HW553 (0x6358/0xA1) 
machine     : Unknown 
processor    : 0 
cpu model    : Broadcom BMIPS4350 V1.0 
BogoMIPS    : 299.26 
wait instruction  : yes 
microsecond timers  : yes 
tlb_entries    : 32 
extra interrupt vector : yes 
hardware watchpoint  : no 
isa      : mips1 mips2 mips32r1 
ASEs implemented  : 
shadow register sets : 1 
kscratch registers  : 0 
core     : 0 
VCED exceptions   : not available 
VCEI exceptions   : not available 

我需要一個變量來存儲是這部分bcm63xx/HW553 (0x6358/0xA1)。它的型號,它在不斷的變化,這是我迄今爲止嘗試:

#include <stdio.h> 
int main (void) 
{ 
    char filename[] = "/proc/cpuinfo"; 
    FILE *file = fopen (filename, "r"); 

    if (file != NULL) { 
    char line [1000]; 
    while(fgets(line,sizeof line,file)!= NULL) /* read a line from a file */ { 
     fprintf(stdout,"%s",line); //print the file contents on stdout. 
    } 

    fclose(file); 
    } 
    else { 
    perror(filename); //print the error message on stderr. 
    } 

    return 0; 
} 

但該腳本只打印文件,我不知道如何將路由器的模型存儲在一個變量,應該如何我這麼做?

PS: 將路由器模型存儲在一個變量中後,我需要比較它是否與預定義的變量相匹配。

UPDATE

我想使它成爲一個功能,我的代碼是:

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

char* model() { 
    char filename[] = "/proc/cpuinfo"; 
    char* key = "system type"; 
    char* value; 
    FILE *file = fopen(filename, "r"); 

    if (file != NULL) { 
     char line[1000]; 
     char* router_model = NULL; 

     while (fgets(line, sizeof line, file) != NULL) /* read a line from a file */ { 
      fprintf(stdout, "%s", line); //print the file contents on stdout. 
      if (strncmp(line, key, strlen(key)) == 0) { 
       char* value = strchr(line, ':'); 
       value += 2; 
       router_model = strdup(value); 
       break; // once the key has been found we can stop reading 
      } 
     } 
     fclose(file); 

     if (router_model != NULL) { 
      printf("The model is %s\n", router_model); // print router model 
     } 
     else { 
      printf("No %s entry in %s\n", key, filename); // key not found, print some error message 
     } 
     free(router_model); 
    } 
    else { 
     perror(filename); //print the error message on stderr. 
    } 
    return router_model; 

} 
int main(void) 
{ 
    char* ret; 
    ret = model(); 
    printf("Model: %s\n", ret); 
    return 0; 
} 

但我得到一個錯誤:

test.c: In function ‘model’: 
test.c:37:9: error: ‘router_model’ undeclared (first use in this function) 
    return router_model; 
     ^~~~~~~~~~~~ 
test.c:37:9: note: each undeclared identifier is reported only once for each function it appears in 

如何解決呢?

+0

1.讀通過line.2線。使用strtok使其成爲關鍵值對。通過將鍵與預定義鍵進行比較來查找所需的值。 – rajesh6115

+0

在你的文章的第三個版本中,我看到'router_model'聲明瞭,但是你也包含了關於這個的錯誤文本,這不在你的文章的第一個版本中。你能否確認錯誤信息仍然發生,在'line'聲明之後立即執行'router_model'聲明? – donjuedo

回答

1

一旦你有你的生產線,檢查它開始你正在尋找的關鍵字符串。

char* key = "system type"; 
... 
if (strncmp(line, key, strlen(key)) == 0) { 
    /* this is the line you want */ 
} 

一旦你確定了行,找到第一個冒號。

char* value = strchr(line, ':'); 

現在你已經擁有了價值,雖然它會包含領先的冒號和空格,所以你可以迭代這些值。

value += 2; 

然後你應該可以使用這個結果。請注意,我沒有分配任何新的空間。如果你想在某處保存這個值,那麼你需要複製字符串。最簡單的方法是重複它。

char* router_model = strdup(value); 

一旦你完成它,你將不得不free()這個字符串。

+0

你能再看看我的文章嗎?我仍然無法把它整理出來 – Squidward

+0

@Squidward你需要在有條件的內部設置'router_model',就在你獲得'value'之後。這意味着'router_model'必須被聲明爲循環外部的變量。 (並且一定要在調試時在每行之後放置'printf()'語句) – chrisaycock

0

你想這樣的:

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

int main(void) 
{ 
    char filename[] = "/proc/cpuinfo"; 
    char* key = "system type"; 
    char* value; 
    FILE *file = fopen(filename, "r"); 

    if (file != NULL) { 
    char line[1000]; 
    char* router_model = NULL; 

    while (fgets(line, sizeof line, file) != NULL) /* read a line from a file */ { 
     fprintf(stdout, "%s", line); //print the file contents on stdout. 
     if (strncmp(line, key, strlen(key)) == 0) { 
     char* value = strchr(line, ':'); 
     value += 2; 
     router_model = strdup(value); 
     break; // once the key has been found wen can stop reading 
     } 
    } 
    fclose(file); 

    if (router_model != NULL) 
     printf("The model is %s\n", router_model); // print router model 
    else 
     printf("No %s entry in %s\n", key, filename); // key not found, print some error message 

    free(router_model); 
    } 
    else { 
    perror(filename); //print the error message on stderr. 
    } 
    return 0; 
} 

這是輕微的修改和一些評論你的代碼。

請注意,仍有改進的餘地。

+0

我試圖將它作爲一個函數存儲,但是出現錯誤,請問我可以看看我的文章嗎? – Squidward

+0

建議很好。要求他再看一次是不正確的。你應該再看看他的代碼。而且,沒有存儲功能。有存儲作爲變量,因爲你正確地寫在你的文章中。去做。 – donjuedo

0

使用strtok和strip,修剪以實現上述程序的鍵值對。所以易於比較關鍵和明確的邏輯問題。

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

char *strip(char *str){ 
     if(str == NULL){ 
       return str; 
     } 
     int j = 0; 
     while(str[0] != '\0' && isspace(str[0])){ 
       j=0; 
       while(str[j] != '\0'){ 
         str[j] = str[j+1]; 
         j++; 
       } 
     } 
     return str; 
} 

char *trim(char *str){ 
     if(str == NULL){ 
       return str; 
     } 
     int len = strlen(str)-1; 
     while(isspace(str[len])){ 
       str[len]='\0'; 
       len--; 
     } 
     return str; 
} 
int process_line(char *str, char *res_key, char *res_value){ 
     char *key, *value; 
     key = strtok(str, ":"); 
     value = strtok(NULL, ":"); 
     if(key && value){ 
       key = strip(key); 
       key = trim(key); 
       value = strip(value); 
       value = trim(value); 
       //printf("%s:%s\n", key, value); 
       strcpy(res_key, key); 
       strcpy(res_value, value); 
     } 
} 
int main(int argc, char *argv[]) 
{ 
     char filename[] = "/proc/cpuinfo"; 
     char* key = "system type"; 
     char* value; 
     FILE *file = fopen(filename, "r"); 
     if (file != NULL) { 
       char line[2048]={0x00}; 
       char temp_key[1024]={0x00}; 
       char temp_value[1024]={0x00}; 
       while (fscanf(file, " %[^\n]s", line) != EOF) /* read a line from a file */ { 
         process_line(line, temp_key, temp_value); 
         if(strcmp(temp_key, key) == 0){ 
           break; 
         } 
         memset(line, 0x00, sizeof(line)); 
         memset(temp_key, 0x00, sizeof(temp_key)); 
         memset(temp_value, 0x00, sizeof(temp_value)); 
       } 
       fclose(file); 
       printf("model:%s\n", temp_value); 
     } 
     else { 
       perror(filename); 
     } 
     return 0; 
} 

我想這應該解決您的問題

相關問題