3_Answers_7-1 - CIS 15AG Answers Chapter 7: Text Files 1 1....

Info iconThis preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon
CIS 15AG Answers Chapter 7: Text Files 1. Write a function named myFileOpen that opens "MOVIES.TXT" in "w" mode; if there is a file with this name the function displays "Do you want to overwrite "MOVIES.TXT"? [Y/N]: " and gets the user's input. If the user disagrees, NULL should be returned, otherwise the file pointer; also if fopen() fails to open the file in "w" mode, NULL should be returned (do not use exit() at all). #include <stdio.h> #include <ctype.h> // HINT: use toupper() #define FLUSH while ( getchar() != '\n' ) FILE * myFileOpen( void ); int main ( void ) { FILE * fpMovies; fpMovies = myFileOpen(); if ( fpMovies != NULL ) printf( "\"MOVIES.TXT\" – successfully opened\n" ); else printf( "\"MOVIES.TXT\" - not available\n" ); return 0; } FILE * myFileOpen( void ) { FILE * fpMovies; char userChoice; fpMovies = fopen("MOVIES.TXT", "r" ); if ( fpMovies == NULL ) // the file does not exist { fpMovies = fopen( "MOVIES.TXT", "w" ); } else { // if "MOVIES.TXT" exists" …more code } return fpMovies; } 1
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
CIS 15AG Answers Chapter 7: Text Files // if "MOVIES.TXT" exists" …more code fclose( fpMovies ); printf("Do you want to overwrite \"MOVIES.TXT\"? [Y/N]: "); scanf( " %c", &userChoice ); FLUSH; if( toupper(userChoice) == 'Y' ) fpMovies = fopen( "MOVIES.TXT", "w" ); else fpMovies = NULL; 2. Write a program that reads movies from "MOVIES.TXT" and displays all movies that begin with the letter 'T'. The file is not alphabetized. Assume the file is valid: one movie per line (title and year), and each line has a '\n', including the last one. MOVIES.TXT Output #include <stdio.h> #include <stdlib.h> int main( void ) { FILE * fpMovies; int c; fpMovies = fopen( "MOVIES.TXT", "r" ); if( fpMovies == NULL ) { printf ( "error opening the file\n" );
Background image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 6

3_Answers_7-1 - CIS 15AG Answers Chapter 7: Text Files 1 1....

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online