Main Content

CERT C++: FIO50-CPP

Do not alternately input and output from a file stream without an intervening positioning call

Description

Rule Definition

Do not alternately input and output from a file stream without an intervening positioning call.1

Polyspace Implementation

The rule checker checks for Alternating input and output from a stream without flush or positioning call.

Examples

expand all

Issue

Alternating input and output from a stream without flush or positioning call occurs 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.

Risk

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 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.

Fix

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

You can reposition a file stream using methods such as std::fstream<>::seekp() or std::fstream<>::seekg().

Example – Input and Output Operations in Succession Without Intermediate Repositioning

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: 07. Input Output (FIO)

Version History

Introduced in R2019a

expand all


1 This software has been created by MathWorks incorporating portions of: the “SEI CERT-C Website,” © 2017 Carnegie Mellon University, the SEI CERT-C++ Web site © 2017 Carnegie Mellon University, ”SEI CERT C Coding Standard – Rules for Developing safe, Reliable and Secure systems – 2016 Edition,” © 2016 Carnegie Mellon University, and “SEI CERT C++ Coding Standard – Rules for Developing safe, Reliable and Secure systems in C++ – 2016 Edition” © 2016 Carnegie Mellon University, with special permission from its Software Engineering Institute.

ANY MATERIAL OF CARNEGIE MELLON UNIVERSITY AND/OR ITS SOFTWARE ENGINEERING INSTITUTE CONTAINED HEREIN IS FURNISHED ON AN "AS-IS" BASIS. CARNEGIE MELLON UNIVERSITY MAKES NO WARRANTIES OF ANY KIND, EITHER EXPRESSED OR IMPLIED, AS TO ANY MATTER INCLUDING, BUT NOT LIMITED TO, WARRANTY OF FITNESS FOR PURPOSE OR MERCHANTABILITY, EXCLUSIVITY, OR RESULTS OBTAINED FROM USE OF THE MATERIAL. CARNEGIE MELLON UNIVERSITY DOES NOT MAKE ANY WARRANTY OF ANY KIND WITH RESPECT TO FREEDOM FROM PATENT, TRADEMARK, OR COPYRIGHT INFRINGEMENT.

This software and associated documentation has not been reviewed nor is it endorsed by Carnegie Mellon University or its Software Engineering Institute.