Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

Code Block
bgColor#ffcccc
const char *filename = "test.txt";
FILE *fptr;

int c = getc(fptr = fopen(filename, "r"));

This non-compliant code example also violaties FIO33-C. Detect and handle input output errors resulting in undefined behavior.

Compliant Solution: getc()

...

Code Block
bgColor#ccccff
const char *filename = "test.txt";
FILE *fptr = fopen(filename, "r");
if (fptr == NULL) {
  /* CheckHandle fptrError for validity */
}

int c = getc(fptr);

Non-Compliant Code Example: putc()

...

Code Block
bgColor#ffcccc
const char *filename = "test.txt";
FILE *fptr = fopen(filename, "w");
if (fptr == NULL) {
  /* Handle Error */
}

int c = 'a';

while (c <= 'z') {
  putc(c++, fptr);
}

...

Code Block
bgColor#ccccff
const char *filename = "test.txt";
FILE *fptr = fopen(filename, "w");
if (fptr == NULL) {
  /* Handle Error */
}

int c = 'a';

while (c <= 'z') {
  putc(c, fptr);
  c++;
}

...