Chapter 12

I l @ ve RuBoard

Chapter 12

  1. It should have #include <stdio.h> for its file definitions. It should declare fp a file pointer: FILE *fp; . The function fopen() requires a mode: fopen("gelatin", "w") or perhaps the "a" mode. The order of the arguments to fputs() should be reversed . For clarity, the output string should have a newline because fputs() doesn't add one automatically. The fclose() function requires a file pointer, not a filename: fclose(fp); . Here is a corrected version:

     #include <stdio.h> int main(void) {    FILE * fp;    int k;    fp = fopen("gelatin", "w");    for (k = 0; k < 30; k++)        fputs("Nanette eats gelatin.\n", fp);    fclose(fp);    return 0; } 
  2. It would open , if possible, the file whose name is the first command-line argument, and it would display onscreen each digit character in the file.

    1. ch = getc(fp1);

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

    3. putc(ch,fp2);

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

      Note

      fp1 is used for input operations because it identifies the file opened in the read mode. Similarly, fp2 was opened in the write mode, so it is used with output functions.


  3. Here is one approach:

     #include <stdio.h> #include <stdlib.h> /* #include <console.h> */  /* for Macs */ int main(int argc,char * argv[]) {    FILE * fp;    double n;    double sum = 0.0;    int ct = 0; /*   argc = ccommand(&argv);  */  /* For Macs */    if (argc == 1)       fp = stdin;    else if (argc == 2)    {       if ((fp = fopen(argv[1], "r")) == NULL)       {          fprintf(stderr, "Can't open %s\n", argv[1]);          exit(EXIT_FAILURE);       }    }    else    {       fprintf(stderr, "Usage: %s [filename]\n", argv[0]);       exit(EXIT_FAILURE);    }    while (fscanf(fp, "%lf", &n) == 1)    {       sum += n;           ++ct;    }    if (ct > 0)          printf("Average of %d values = %f\n", ct, sum / ct);    else          printf("No valid data.\n");                 return 0; } 

    Macintosh C users, remember to use console.h and ccommand() .

  4. Here is one approach:

    Macintosh C users, remember to use console.h and ccommand() .

     #include <stdio.h> #include <stdlib.h> /*  #include <console.h>  */   /* for Mac */ #define BUF 256 int has_ch(char ch, const char * line); int main(int argc,char * argv[]) {    FILE * fp;    char ch;    char line [BUF];  /*  argc = ccommand(&argv); */  /* for Mac */    if (argc != 3)    {       printf("Usage: %s character filename\n", argv[0]);       exit(1);    }    ch = argv[1][0];    if ((fp = fopen(argv[2], "r")) == NULL)    {       printf("Can't open %s\n", argv[2]);       exit(1);    }    while (fgets(line,BUF,fp) != NULL)    {       if (has_ch(ch,line))          fputs(line,stdout);    }    fclose(fp);    return 0; } int has_ch(char ch, const char * line) {    while (*line)       if (ch == *line++)          return(1);    return 0; } 

    The fgets() and fputs() functions work together because fgets() leaves the \n produced by Enter in the string, and fputs() does not add a \n the way that puts() does.

  5. The distinction between a binary file and a text file is a system-dependent difference between file formats. The distinction between a binary stream and a text stream consists of translations performed by the program as it reads or writes streams. (A binary stream has no translations; a text stream may convert newline and other characters .)

    1. When 8238201 is saved using fprintf() , it's saved as seven characters stored in 7 bytes. When saved using fwrite() , it's saved as a 4-byte integer using the binary representation of that numeric value.

    2. No difference; in each case it's saved as a 1-byte binary code.

  6. The first is just a shorthand notation for the second; the third writes to the standard error. Normally, the standard error is directed to the same place as the standard output, but the standard error is not affected by standard output redirection.

  7. The "r+" mode lets you read and write anywhere in a file, so it's best suited. The "a+" only lets you append material to the end of the file, and the "w+" starts with a clean slate, discarding previous file contents.

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