Skip to main content

IAR Embedded Workbench for Arm 9.70.x

MISRAC2012-Rule-22.4

In this section:
Synopsis

(Mandatory) There shall be no attempt to write to a stream which has been opened as read-only

Enabled by default

Yes

Severity/Certainty

Medium/Medium

mediummedium.png
Full description

A file opened as read-only is written to. This check is identical to RESOURCE-write-ronly-file.

Coding standards
MISRA C:2012 Rule-22.4

(Mandatory) There shall be no attempt to write to a stream which has been opened as read-only

Code examples

The following code example fails the check and will give a warning:

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

void example(void) {
  FILE *f1;
  f1 = fopen("test-file.txt", "r");
  fprintf(f1, "Hello, World!");
  fclose(f1);
}

The following code example passes the check and will not give a warning about this issue:

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

void example(void) {
  FILE *f1;
  f1 = fopen("test-file.txt", "r+");
  fprintf(f1, "Hello, World!");
  fclose(f1);
}