/* video.c */ #include #include #include #include #define VIDEO_INIT #include "video.h" /*---------------------------------------------------------- main() ----------------------------------------------------------*/ int main( int argc,char **argv ) { /* declare variables */ int i, more = 1; /* read movies (if any) from the data file */ readFile( "movies.dat" ); /* display the movies */ if ( nmovies > 0 ) { printf( "\nhere are the movies from the file:\n" ); for ( i=0; i 0 ) { printf( "\nhere are the movies:\n" ); for ( i=0; iname = (char *)malloc( MAX*sizeof( char )); printf( "enter title: " ); /*scanf( "%s",tmp->name );*/ tmp->name = fgets( tmp->name,MAX,stdin ); if (( tmp->name == NULL ) || ( ! strncmp( "quit",tmp->name,4 ))) { free( tmp ); return NULL; } p = strchr( tmp->name,'\n' ); *p = '\0'; tmp->number = nmovies; return( tmp ); } /* end of readTitle() */ /*---------------------------------------------------------- printTitle() this function displays on the screen the content of the TITLE_TYPE structure argument. ----------------------------------------------------------*/ void printTitle( TITLE_TYPE *title ) { printf( "#%d [%s]\n",title->number,title->name ); } /* end of printTitle() */ /*---------------------------------------------------------- writeFile() this function writes the contents of the "movies" global array into a data file, which is named according to the function argument. ----------------------------------------------------------*/ void writeFile( char *filename ) { int i; /* open data file for writing (which will erase any contents that might exist in that file */ FILE *fp = fopen( filename,"w" ); if ( fp == NULL ) { fprintf( stderr,"error opening file: %s %s\n", filename,strerror(errno)); return; } /* loop through the array, printing each TITLE_TYPE record */ for ( i=0; inumber,movies[i]->name ); } /* close the data file */ fclose( fp ); } /* end of writeFile() */ /*---------------------------------------------------------- readFile() this function reads the contents of a data file, which is named according to the function argument and stores the contents in the "movies" global array. ----------------------------------------------------------*/ void readFile( char *filename ) { int more = 1; FILE *fp; char *p; TITLE_TYPE *tmp; /* open the data file for reading */ fp = fopen( filename,"r" ); if ( fp == NULL ) { fprintf( stderr,"error opening file: %s %s\n", filename,strerror(errno)); return; } /* loop through the data file, reading one record at a time and storing each record in the "movies" global array */ while( more ) { tmp = (TITLE_TYPE *)malloc( sizeof( TITLE_TYPE )); fscanf( fp,"%d",&(tmp->number)); tmp->name = (char *)malloc( MAX*sizeof( char )); tmp->name = fgets( tmp->name,MAX,fp ); if ( tmp->name == NULL ) { free( tmp ); more = 0; } else { while ( tmp->name[0] == ' ' ) { strcpy( &(tmp->name[0]),&(tmp->name[1]) ); } p = strchr( tmp->name,'\n' ); *p = '\0'; movies = (TITLE_TYPE **)realloc( movies,(nmovies+1)*sizeof( TITLE_TYPE * )); movies[nmovies] = tmp; nmovies++; } } /* close data file */ fclose( fp ); } /* end of readFile() */