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......