|
|
|
![]() |
|
Strumenti |
![]() |
#1 |
Member
Iscritto dal: Aug 2005
Città: pescara in abruzzo
Messaggi: 45
|
C: trovare riga in un file che inizia in un certo modo
Salve a tutti,
![]() ho un file di testo ad esempio: Codice:
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 Codice:
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 |
Senior Member
Iscritto dal: Nov 2005
Città: Texas
Messaggi: 1722
|
Il buffer non e' piccolo. Dovresti mettere qualche controllo, per esempio
- in apertura del file Codice:
fp = fopen("/proc/cpuinfo", "r"); if (fp == NULL) { perror ("Error opening /proc/cpuinfo"); return 0; } - controllerei anche questa condizione: Codice:
if (bytes_read == 0 || bytes_read == sizeof (buffer)) return 0;
__________________
In God we trust; all others bring data |
![]() |
![]() |
![]() |
Strumenti | |
|
|
Tutti gli orari sono GMT +1. Ora sono le: 21:33.