knking.com -- Programming Language Books and Training

 C C++ Java

Books by K. N. King

Home
Books by K. N. King
Short courses
Recommended books
Recommended links
FAQ

C Programming: A Modern Approach (Second Edition)

Chapter 22

Answers to Selected Exercises

2. [was #2]

(a) "rb+"
(b) "a"
(c) "rb"
(d) "r"

4. [was #4]

(a) 00000083.736
(b) 00000029749.
(c) 001.0549e+09
(d) 002.3522e-05

6. [was #6] printf(widget == 1 ? "%d widget" : "%d widgets", widget);

8. [was #8] No. The difference is that "%1s" will store a null character after it reads and stores a nonblank character; " %c" will store only the nonblank character. As a result, the two format strings must be used differently:

char c, s[2];

scanf(" %c", &c);  /* stores a nonblank character into c */
scanf("%1s", s);   /* stores a nonblank character into s[0]
                      and a null character into s[1] */

10. [was #10] Revise the program's while loop as follows:

while ((ch = getc(source_fp)) != EOF)
  if (putc(ch, dest_fp) == EOF) {
    fprintf(stderr, "Error during writing; copy terminated\n");
    exit(EXIT_FAILURE);
  }

14. [was #18]

(a)

char *fget_string(char *s, int n, FILE *stream)
{
  int ch, len = 0;

  while (len < n - 1) {
    if ((ch = getc(stream)) == EOF) {
      if (len == 0 || ferror(stream))
        return NULL;
      break;
    }
    s[len++] = ch;
    if (ch == '\n')
      break;
  }

  s[len] = '\0';
  return s;
}

(b)

int fput_string(const char *s, FILE *stream)
{
  while (*s != '\0') {
    if (putc(*s, stream) == EOF)
      return EOF;
    s++;
  }

  return 0;
}

15. [was #22]

(a) fseek(fp, n * 64L, SEEK_SET);
(b) fseek(fp, -64L, SEEK_END);
(c) fseek(fp, 64L, SEEK_CUR);
(d) fseek(fp, -128L, SEEK_CUR);

Answers to Selected Programming Projects

2. [was #12]

#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>

int main(int argc, char *argv[])
{
  FILE *fp;
  int ch;

  if (argc != 2) {
    fprintf(stderr, "usage: toupper file\n");
    exit(EXIT_FAILURE);
  }

  if ((fp = fopen(argv[1], "r")) == NULL) {
    fprintf(stderr, "Can't open %s\n", argv[1]);
    exit(EXIT_FAILURE);
  }

  while ((ch = getc(fp)) != EOF)
    putchar(toupper(ch));

  fclose(fp);
  return 0;
}

4. [was #14]

(a)

#include <stdio.h>
#include <stdlib.h>

int main(int argc, char *argv[])
{
  FILE *fp;
  int count = 0;

  if (argc != 2) {
    fprintf(stderr, "usage: cntchar file\n");
    exit(EXIT_FAILURE);
  }

  if ((fp = fopen(argv[1], "r")) == NULL) {
    fprintf(stderr, "Can't open %s\n", argv[1]);
    exit(EXIT_FAILURE);
  }

  while (getc(fp) != EOF)
    count++;

  printf("There are %d characters in %s\n", count, argv[1]);

  fclose(fp);
  return 0;
}

(b)

#include <ctype.h>
#include <stdbool.h>   /* C99 only */
#include <stdio.h>
#include <stdlib.h>

int main(int argc, char *argv[])
{
  FILE *fp;
  int ch, count = 0;
  bool in_word;

  if (argc != 2) {
    fprintf(stderr, "usage: cntword file\n");
    exit(EXIT_FAILURE);
  }

  if ((fp = fopen(argv[1], "r")) == NULL) {
    fprintf(stderr, "Can't open %s\n", argv[1]);
    exit(EXIT_FAILURE);
  }

  in_word = false;
  while ((ch = getc(fp)) != EOF)
    if (isspace(ch))
      in_word = false;
    else if (!in_word) {
      in_word = true;
      count++;
    }

  printf("There are %d words in %s\n", count, argv[1]);

  fclose(fp);
  return 0;
}

(c)

#include <stdio.h>
#include <stdlib.h>

int main(int argc, char *argv[])
{
  FILE *fp;
  int ch, count = 0;

  if (argc != 2) {
    fprintf(stderr, "usage: cntline file\n");
    exit(EXIT_FAILURE);
  }

  if ((fp = fopen(argv[1], "r")) == NULL) {
    fprintf(stderr, "Can't open %s\n", argv[1]);
    exit(EXIT_FAILURE);
  }

  while((ch = getc(fp)) != EOF)
    if (ch == '\n')
      count++;

  printf("There are %d lines in %s\n", count, argv[1]);

  fclose(fp);
  return 0;
}

6. [was #16; modified]

#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>

int main(int argc, char *argv[])
{
  FILE *fp;
  int i, n, offset;
  unsigned char buffer[10];

  if (argc != 2) {
    fprintf(stderr, "usage: viewfile file\n");
    exit(EXIT_FAILURE);
  }

  if ((fp = fopen(argv[1], "rb")) == NULL) {
    fprintf(stderr, "Can't open %s\n", argv[1]);
    exit(EXIT_FAILURE);
  }

  printf("Offset              Bytes              Characters\n");
  printf("------  -----------------------------  ----------\n");

  for (offset = 0;; offset += 10) {
    n = fread(buffer, 1, 10, fp);
    if (n == 0)
      break;

    printf("%6d  ", offset);
    for (i = 0; i < n; i++)
      printf("%.2X ", buffer[i]);
    for (; i < 10; i++)
      printf("   ");
    printf(" ");
    for (i = 0; i < n; i++) {
      if (!isprint(buffer[i]))
        buffer[i] = '.';
      printf("%c", buffer[i]);
    }
    printf("\n");
  }

  fclose(fp);
  return 0;
}

9. [was #20]

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define NAME_LEN 25

struct part {
  int number;
  char name[NAME_LEN + 1];
  int on_hand;
};

int main(int argc, char *argv[])
{
  FILE *in_fp1, *in_fp2, *out_fp;
  int num_read1, num_read2;
  struct part part1, part2;

  if (argc != 4) {
    fprintf(stderr, "usage: merge infile1 infile2 outfile\n");
    exit(EXIT_FAILURE);
  }

  if ((in_fp1 = fopen(argv[1], "rb")) == NULL) {
    fprintf(stderr, "Can't open %s\n", argv[1]);
    exit(EXIT_FAILURE);
  }

  if ((in_fp2 = fopen(argv[2], "rb")) == NULL) {
    fprintf(stderr, "Can't open %s\n", argv[2]);
    exit(EXIT_FAILURE);
  }

  if ((out_fp = fopen(argv[3], "wb")) == NULL) {
    fprintf(stderr, "Can't open %s\n", argv[3]);
    exit(EXIT_FAILURE);
  }

  num_read1 = fread(&part1, sizeof(struct part), 1, in_fp1);
  num_read2 = fread(&part2, sizeof(struct part), 1, in_fp2);
  while (num_read1 == 1 && num_read2 == 1)
    /* successfully read records from both files */
    if (part1.number < part2.number) {
      fwrite(&part1, sizeof(struct part), 1, out_fp);
      num_read1 = fread(&part1, sizeof(struct part), 1, in_fp1);
    } else if (part1.number > part2.number) {
      fwrite(&part2, sizeof(struct part), 1, out_fp);
      num_read2 = fread(&part2, sizeof(struct part), 1, in_fp2);
    } else {
      /* part numbers are equal */
      if (strcmp(part1.name, part2.name) != 0)
        fprintf(stderr,
                "Names don't match for part %d; using the name %s\n",
                part1.number, part1.name);
      part1.on_hand += part2.on_hand;
      fwrite(&part1, sizeof(struct part), 1, out_fp);
      num_read1 = fread(&part1, sizeof(struct part), 1, in_fp1);
      num_read2 = fread(&part2, sizeof(struct part), 1, in_fp2);
    }

  /* copy rest of file1 to output file */
  while (num_read1 == 1) {
    fwrite(&part1, sizeof(struct part), 1, out_fp);
    num_read1 = fread(&part1, sizeof(struct part), 1, in_fp1);
  }

  /* copy rest of file2 to output file */
  while (num_read2 == 1) {
    fwrite(&part2, sizeof(struct part), 1, out_fp);
    num_read2 = fread(&part2, sizeof(struct part), 1, in_fp2);
  }

  fclose(in_fp1);
  fclose(in_fp2);
  fclose(out_fp);
  return 0;
}

Copyright © 2008, 1996 W. W. Norton & Company, Inc. All rights reserved.

Google
 
Web knking.com