/* movie.c */ #include #include #include #include #define MOVIE_INIT #include "movie.h" /*---------------------------------------------------------- copyMovie() this function copies the contents of t1 into t2. ----------------------------------------------------------*/ void copyMovie( MOVIE_TYPE *t1, MOVIE_TYPE *t2 ) { strcpy( t2->name,t1->name ); t2->number = t1->number; } /* end of copyMovie() */ /*---------------------------------------------------------- readMovie() this function prompts the user and reads data from the keyboard and stores it in a MOVIE_TYPE structure. it allocates storage for one MOVIE_TYPE structure and returns a pointer to that structure. "scanf" will read only one word -- terminating at the first whitespace character (newline, blank, tab). "fgets" will read until the first newline. ----------------------------------------------------------*/ MOVIE_TYPE *readMovie() { char *p; MOVIE_TYPE *tmp = (MOVIE_TYPE *)malloc( sizeof( MOVIE_TYPE )); tmp->name = (char *)malloc( MAX*sizeof( char )); printf( "enter movie: " ); /*scanf( "%s",tmp->name );*/ tmp->name = fgets( tmp->name,MAX,stdin ); if ( tmp->name == NULL ) { free( tmp ); return NULL; } p = strchr( tmp->name,'\n' ); *p = '\0'; tmp->number = ++lastnumber; return( tmp ); } /* end of readMovie() */ /*---------------------------------------------------------- printMovie() this function displays on the screen the content of the MOVIE_TYPE structure argument. ----------------------------------------------------------*/ void printMovie( MOVIE_TYPE *movie ) { printf( "#%04d [%s]\n",movie->number,movie->name ); } /* end of printMovie() */ /*---------------------------------------------------------- writeMovieFile() this function writes the contents of the "movies" global array into a data file, which is named according to the function argument. ----------------------------------------------------------*/ void writeMovieFile( 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; } /* write lastnumber to the file first */ fprintf( fp,"%d\n",lastnumber ); /* loop through the array, printing each MOVIE_TYPE record */ for ( i=0; inumber,movies[i]->name ); } /* close the data file */ fclose( fp ); } /* end of writeMovieFile() */ /*---------------------------------------------------------- readMovieFile() 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 readMovieFile( char *filename ) { int more = 1; FILE *fp; char *p; MOVIE_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; } /* read lastnumber from the file first */ fscanf( fp,"%d",&lastnumber ); /* loop through the data file, reading one record at a time and storing each record in the "movies" global array */ while( more ) { tmp = (MOVIE_TYPE *)malloc( sizeof( MOVIE_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 = (MOVIE_TYPE **)realloc( movies,(nmovies+1)*sizeof( MOVIE_TYPE * )); movies[nmovies] = tmp; nmovies++; } } /* close data file */ fclose( fp ); } /* end of readMovieFile() */