This might bewilder you some more because C++ is a tricky language for a beginner. It''s a simple thin wrapper class around C stdio that provides RAII and some return value checks that throw exception when errors occur. If you can work this out you're well on your way to being competent. It's mainly meant as a demonstrator for constructors/destructors which are fundamental to C++ programming.

#include <iostream>
#include <string>
#include <stdexcept>
#include <cstdio>

class File
{
public:
    // default constructor
    File() : m_file( 0 ) {}

    // constructor - opens the file
    File( const std::string & filename, const std::string & mode )
        : m_file( 0 )
    {
        std::cout << "constructor\n";
        open( filename, mode );
    }

    // move constructor - takes ownership of the underlying file object
    File( File && rhs ) : m_file(0)
    {
        std::cout << "move constructor\n";
        m_file = rhs.m_file;
        rhs.m_file = 0;
    }

    // destructor
    ~File()
    {
        close();
    }

public:
    // opens a file
    void open( const std::string & filename, const std::string & mode )
    {
        std::cout << "opening file " << filename << "\n";
        m_file = fopen( filename.c_str(), mode.c_str() );
if (!m_file) throw std::runtime_error( "Error opening file: " + std::string( strerror( errno ) ) );
    }

    // closes the files
    void close()
    {
        if (m_file)
        {
            std::cout << "closing file\n";
            fclose( m_file );
            m_file = 0;
        }
    }

    // reads from the file
    int read( void * buffer, size_t size )
    {
        return fread( buffer, 1, size, m_file );
    }

    // writes to the file
    int write( const void * buffer, size_t size )
    {
        int bytesWritten = fwrite( buffer, 1, size, m_file );
        if (bytesWritten == 0) // I/O error
        {
throw std::runtime_error( std::string( "Error writing to file: " + std::string( strerror( errno ) ) ) );
        }
        return bytesWritten;
    }

private:
    FILE * m_file;  // file handle
};

// factory function to demonstrate change of ownership
File openFile( const std::string filename, const std::string & mode )
{
    File file( filename, mode );
    return file;
}

int main( int argc, char * argv[] )
{
    try
    {
        // open the files
        File input = openFile( "DD:INPUT", "rb, type=record, noseek" );
        File output( "DD:OUTPUT", "wb, type=record" );
        // copy the input file to the output file
        size_t bytesRead;
        char buffer[32768];
        while ( ( bytesRead = input.read( buffer, sizeof buffer ) ) )
        {
            output.write( buffer, bytesRead );
        }
        // <<<<< destructors run here when the file objects go out of scope
    }
    catch (std::exception & e)
    {
        std::cout << e.what() << "\n";
    }
    return 0;
}



On 30/05/2017 4:32 AM, Steve Beaver wrote:
Does anyone have a complete piece of C++ code that runs under MVS or Linux that 
I can study?  99% of the stuff I write is HLASM and to a point I find C++ 
bewildering.

TIA

Steve

----------------------------------------------------------------------
For IBM-MAIN subscribe / signoff / archive access instructions,
send email to [email protected] with the message: INFO IBM-MAIN

----------------------------------------------------------------------
For IBM-MAIN subscribe / signoff / archive access instructions,
send email to [email protected] with the message: INFO IBM-MAIN

Reply via email to