Main Content

MISRA C++:2023 Rule 30.0.2

Reads and writes on the same file stream shall be separated by a positioning operation

Since R2024b

Description

Rule Definition

Reads and writes on the same file stream shall be separated by a positioning operation.

Rationale

If you use a file stream for both reading and writing, between read and write operations on the stream, you must perform a stream repositioning operation.

When you open a file stream using a function such as std::fstream(), an std::basic_filebuf object is created to track the position in the file stream where the next read or write operation will occur. The implementation of this object uses the C FILE* abstraction. According to the C standard, a single FILE* pointer can be used to perform both input and output operations on a file stream. However, before you perform an output operation following an input (or vice versa), you must first reposition the pointer in the file stream; otherwise, the behavior is undefined.

Polyspace Implementation

The rule checker reports a violation when you perform an output operation on a file stream following an input operation or vice versa, without an intermediate repositioning of the file stream. The checker detects issues in file operations using one of these file pointers:

  • A FILE* pointer (for files opened using functions such as fopen()).

  • An std::basic_filebuf object (for files opened using functions such as std::fstream())

Depending on whether you use a FILE* pointer or an std::basic_filebuf object, you can reset the position in the file stream using one of these functions:

  • For std::basic_filebuf objects, you can reposition a file stream using methods such as std::fstream<>::seekp() or std::fstream<>::seekg().

  • For FILE* pointers, you can call a file positioning function such as fseek() or fsetpos() between output and input operations (or call the fflush() function to flush the output buffer between an output and a subsequent input operation)..

Troubleshooting

If you expect a rule violation but Polyspace® does not report it, see Diagnose Why Coding Standard Violations Do Not Appear as Expected.

Examples

expand all

In this example, in the function getSomeLineFromFile(), the file stream object file is used for both input and output operations:

  • The input operation is done using the std::fstream<>::operator<<() method.

  • The output operation is done using the std::getline() method.

However, there is no file stream repositioning operation between the output and input operation. This issue is remedied in the function getOtherLineFromFile(), which repositions the file stream using the std::fstream<>::seekg() method.

#include <fstream>
#include <string>

void getSomeLineFromFile() {
    std::fstream file { "hello.txt" };
    file << "Some line\n" << std::flush; 
    std::string line {};
    std::getline( file, line ); //Noncompliant
}

void getOtherLineFromFile() {
    std::fstream file { "hello.txt" };
    file << "Some line\n" << std::flush; 
    std::string line {};
    file.seekg( 0, std::ios_base::beg );
    std::getline( file, line ); //Compliant
}

Check Information

Group: Input/Output Library
Category: Required

Version History

Introduced in R2024b