Sams Teach Yourself C in 21 Days

(singke) #1
3.BUG BUSTER:Is anything wrong with the following code?
FILE *fp;
int c;

if ( ( fp = fopen( oldname, “rb” ) ) == NULL )
return -1;

while (( c = fgetc( fp)) != EOF )
fprintf( stdout, “%c”, c );

fclose ( fp );
Because of the many possible solutions, answers are not provided for the following
exercises.


  1. Write a program that displays a file to the screen.

  2. Write a program that opens a file and prints it to the printer (stdprn). The program
    should print only 55 lines per page.

  3. Modify the program in exercise 5 to print headings on each page. The headings
    should contain the filename and the page number.

  4. Write a program that opens a file and counts the number of characters. The pro-
    gram should print the number of characters when finished.

  5. Write a program that opens an existing text file and copies it to a new text file with
    all lowercase letters changed to uppercase and all other characters unchanged.

  6. Write a program that opens any disk file, reads it in 128-byte blocks, and displays
    the contents of each block on-screen in both hexadecimal and ASCII formats.

  7. Write a function that opens a new temporary file with a specified mode. All tempo-
    rary files created by this function should automatically be closed and deleted when
    the program terminates. (Hint: Use the atexit()library function.)


474 Day 16

26 448201x-CH16 8/13/02 11:13 AM Page 474

Free download pdf