Review Questions

I l @ ve RuBoard

Review Questions

  1. What's wrong with this program?

     int main(void) {    int * fp;    int k;    fp = fopen("gelatin");    for (k = 0; k < 30; k++)        fputs(fp, "Nanette eats gelatin.");    fclose("gelatin");    return 0;     } 
  2. What would the following program do? (Mac users can assume the program correctly uses console. h and the ccommand() function.)

     #include <stdio.h> #include <stdlib.h> #include <ctype.h> int main(int argc, char *argv[]) {     int ch;     FILE *fp;     if ( (fp = fopen(argv[1], "r")) == NULL)          exit(1);     while ( (ch= getc(fp)) != EOF )         if( isdigit(ch) )             putchar(ch);     fclose (fp);     return 0;      } 
  3. Suppose you have these statements in a program:

     #include <stdio.h> FILE * fp1,* fp2; char ch; fp1 = fopen("terky", "r"); fp2 = fopen("jerky", "w"); 

    Also, suppose that both files were opened successfully. Supply the missing arguments in the following function calls:

    1. ch = getc();

    2. fprintf( ,"%c\n", );

    3. putc( , );

    4. fclose(); /* close the terky file*/

  4. Write a program that takes zero command-line arguments or one command-line argument. If there is one argument, it is interpreted as the name of a file. If there is no argument, the standard input ( stdin ) is to be used for input. Assume that the input consists entirely of floating-point numbers. Have the program calculate and report the arithmetic mean (the average) of the input numbers .

  5. Write a program that takes two command-line arguments. The first is a character, and the second is a filename. The program should print only those lines in the file containing the given character.

    Note

    Lines in a file are identified by a terminating '\n' . Assume that no line is more than 256 characters long. You might want to use fgets() .


  6. What's the difference between binary files and text files on the one hand versus binary streams and text streams on the other?

  7. What is the difference between

    1. Saving 8238201 by using fprintf() and saving it by using fwrite() ?

    2. Saving the character S by using putc() and saving it by using fwrite() ?

  8. What's the difference among the following?

     printf("Hello, %s\n", name); fprintf(stdout, "Hello, %s, name); fprintf(stderr, "Hello, %s, name); 
  9. The "a+" , "r+" , and "w+" modes all open files for both reading and writing. Which one is best suited for altering material already present in a file?

I l @ ve RuBoard


C++ Primer Plus
C Primer Plus (5th Edition)
ISBN: 0672326965
EAN: 2147483647
Year: 2000
Pages: 314
Authors: Stephen Prata

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net