Code:
#include <stdio.h>
#include <string.h>
// Something here (Varies from 100-500 lines)
// 2001.12.25 (time) Start Time
// Weather for 2001.12.25 (number)
// Something here (Varies from (10-50 lines)
int main ( ) {
char buff[BUFSIZ];
FILE *fp;
int stime, wthr; // surely they're more than just ints?
// open the file, check for errors
fp = fopen( "file.txt", "r" );
if ( fp == NULL ) {
perror( "Cannot open file" );
exit( 1 );
}
// read each line
while ( fgets(buff,BUFSIZ,fp) != NULL ) {
// check each line, to see if it contains our magic words
if ( strstr(buff,"Start Time") != NULL ) {
// a start time, extract the data
// %*s means skip a string - RTM for more details
if ( sscanf(buff, "%*s %d Start Time",&stime) == 1 ) {
printf( "Time Value=%d\n", stime );
}
}
if ( strstr(buff,"Weather for") != NULL ) {
if ( sscanf(buff, "Weather for %*s %d",&wthr) == 1 ) {
printf( "Weather Value=%d\n", wthr );
}
}
}
fclose( fp );
return 0;
}