#include #include #include #include #include #include #define FILE "data" #define LINESIZE 80 int main( ) { struct stat statbuf; char *linebuf, *p, *found; int fh, price; if( ! stat( FILE, &statbuf ) && ( p = calloc( statbuf.st_size, sizeof( char ) ) ) && ( fh = open( FILE, O_RDONLY|O_BINARY, S_IREAD ) ) && ( read( fh, p, statbuf.st_size ) > 0 ) && ! close( fh ) ) { printf( "%s", "Fruit? " ); linebuf = calloc( LINESIZE, sizeof( char ) ); fgets( linebuf, LINESIZE, stdin ); *( linebuf + strlen( linebuf ) -1 ) = 0; if( (found = strstr( p, linebuf )) ) { sscanf( found + strlen( linebuf ), "%i", &price ); printf( "%s costs %d", linebuf, price ); } else { printf( "No price for %s available\n", linebuf ); } free( p ); free( linebuf ); } else { printf( "Error: %d", errno ); } return (0); }