Primjer rada s datotekom
/* Primjer rada s datotekom za PR101 */ /* stvaranje ASCII datoteke zapisa */ /* Napisao: B. Brown, Travanj 1994 */ /* stvaranje datoteke goods i ispisivanje na ekran */ /* svi proizvodi su izmjereni */ /* razina pregrupiranja. */ #include <stdio.h>
#include <ctype.h> #include <string.h> #include <stdlib.h> /* definicija zapisa tipa goods */ struct goods { char name[20]; /* ime proizvoda */ float price; /* cijena proizvoda */ int quantity; /* mjera */ int reorder; /* razina pregrupiranja */ }; /* prototipovi funkcija */ void myexit( int ); void processfile( void ); void printrecord( struct goods ); int getrecord( struct goods * ); /* globalne varijable */ FILE *fopen(), *input_file; /* pokazivač na ulaznu datoteku */ /* osigurava elegantan izlaz iz programa */ void myexit( int exitcode ) { if( input_file != NULL ) fclose( input_file ); exit( exitcode ); } /* ispisuje zapis */ void printrecord( struct goods record ) { printf("\nIme proizvoda\t%s\n", record.name ); printf("Cijena proizvoda\t%.2f\n", record.price ); printf("Mjera proizvoda\t%d\n", record.quantity ); printf("Razina pregrupiranja proizvoda\t%d\n", record.reorder ); } /* učitava jedan zapis iz ulazne datoteke u 'record', vraća 1 ako je sve u redu */ int getrecord( struct goods *record ) { int loop = 0, ch; char buffer[40];
ch = fgetc( input_file ); /* idi na početak zapisa */ while( (ch == '\n') || (ch == ' ') && (ch != EOF) ) ch = fgetc( input_file ); if( ch == EOF ) return 0; /* učitaj ime proizvoda */ while( (ch != '\n') && (ch != EOF)) { buffer[loop++] = ch; ch = fgetc( input_file ); } buffer[loop] = 0; strcpy( record->name, buffer ); if( ch == EOF ) return 0; /* idi na početak slijedećeg polja */ while( (ch == '\n') || (ch == ' ') && (ch != EOF) ) ch = fgetc( input_file ); if( ch == EOF ) return 0; /* učitaj cijenu proizvoda */ loop = 0; while( (ch != '\n') && (ch != EOF)) { buffer[loop++] = ch; ch = fgetc( input_file ); } buffer[loop] = 0; record->price = atof( buffer ); if( ch == EOF ) return 0;
/* idi na početak slijedećeg polja */ while( (ch == '\n') || (ch == ' ') && (ch != EOF) ) ch = fgetc( input_file ); if( ch == EOF ) return 0; /* učitaj mjeru proizvoda */
loop = 0; while( (ch != '\n') && (ch != EOF)) { buffer[loop++] = ch; ch = fgetc( input_file ); } buffer[loop] = 0; record->quantity = atoi( buffer ); if( ch == EOF ) return 0; /* idi na početak slijedećeg polja */ while( (ch == '\n') || (ch == ' ') && (ch != EOF) ) ch = fgetc( input_file );
if( ch == EOF ) return 0;
/* učitaj razinu pregrupiranja proizvoda */ loop = 0; while( (ch != '\n') && (ch != EOF)) { buffer[loop++] = ch; ch = fgetc( input_file ); } buffer[loop] = 0; record->reorder = atoi( buffer ); if( ch == EOF ) return 0; return 1; /* označeni zapis je uspješno učitan */ } /* stvara datoteku za zapis */ void processfile( void ) { struct goods record; /* sadrži zapis učitan iz ulazne datoteke */ while( ! feof( input_file )) { if( getrecord( &record ) == 1 ) { if( record.quantity <= record.reorder ) printrecord( record ); } else myexit( 1 ); /* greška pri primanju zapisa */ } }
main() { char filename[40]; /* ime datoteke s bazom podataka */ printf("Primjer programa za pregrupiranje datoteke proizvoda\n"); printf("Unesi datoteku s bazom podataka "); scanf(" %s", filename ); input_file = fopen( filename, "rt" ); if( input_file == NULL ) { printf("Datoteka %s se ne može otvoriti\n", filename ); myexit( 1 ); } processfile(); myexit( 0 ); }Molim vas nabavite podatke za ovaj primjer od vašeg profesora ili putem ftp-a.