RTR3DECL(int)  RTFileSeek(RTFILE hFile, int64_t offSeek, unsigned uMethod, uint64_t *poffActual)
{
    static ULONG aulSeekRecode[] =
    {
        FILE_BEGIN,
        FILE_CURRENT,
        FILE_END,
    };

    /*
     * Validate input.
     */
    if (uMethod > RTFILE_SEEK_END)
    {
        AssertMsgFailed(("Invalid uMethod=%d\n", uMethod));
        return VERR_INVALID_PARAMETER;
    }

    /*
     * Execute the seek.
     */
    if (MySetFilePointer(hFile, offSeek, poffActual, aulSeekRecode[uMethod]))
        return VINF_SUCCESS;
    return RTErrConvertFromWin32(GetLastError());
}
RTR3DECL(int)  RTFileSetSize(RTFILE hFile, uint64_t cbSize)
{
    /*
     * Get current file pointer.
     */
    int         rc;
    uint64_t    offCurrent;
    if (MySetFilePointer(hFile, 0, &offCurrent, FILE_CURRENT))
    {
        /*
         * Set new file pointer.
         */
        if (MySetFilePointer(hFile, cbSize, NULL, FILE_BEGIN))
        {
            /* set file pointer */
            if (SetEndOfFile((HANDLE)RTFileToNative(hFile)))
            {
                /*
                 * Restore file pointer and return.
                 * If the old pointer was beyond the new file end, ignore failure.
                 */
                if (    MySetFilePointer(hFile, offCurrent, NULL, FILE_BEGIN)
                    ||  offCurrent > cbSize)
                    return VINF_SUCCESS;
            }

            /*
             * Failed, try restoring the file pointer.
             */
            rc = GetLastError();
            MySetFilePointer(hFile, offCurrent, NULL, FILE_BEGIN);
        }
        else
            rc = GetLastError();
    }
    else
        rc = GetLastError();

    return RTErrConvertFromWin32(rc);
}
/**
 * This is a helper to check if an attempt was made to grow a file beyond the
 * limit of the filesystem.
 *
 * @returns true for file size limit exceeded.
 * @param   hFile       Filehandle.
 * @param   offSeek     Offset to seek.
 * @param   uMethod     The seek method.
 */
DECLINLINE(bool) IsBeyondLimit(RTFILE hFile, uint64_t offSeek, unsigned uMethod)
{
    bool fIsBeyondLimit = false;

    /*
     * Get the current file position and try set the new one.
     * If it fails with a seek error it's because we hit the file system limit.
     */
/** @todo r=bird: I'd be very interested to know on which versions of windows and on which file systems
 * this supposedly works. The fastfat sources in the latest WDK makes no limit checks during
 * file seeking, only at the time of writing (and some other odd ones we cannot make use of). */
    uint64_t offCurrent;
    if (MySetFilePointer(hFile, 0, &offCurrent, FILE_CURRENT))
    {
        if (!MySetFilePointer(hFile, offSeek, NULL, uMethod))
            fIsBeyondLimit = GetLastError() == ERROR_SEEK;
        else /* Restore file pointer on success. */
            MySetFilePointer(hFile, offCurrent, NULL, FILE_BEGIN);
    }

    return fIsBeyondLimit;
}
RESLT COutputReader::OpenOutputFile(TCHAR *FileName)
{
	DWORD bytes;
	//int i;
	//SPECIESBINOUTINF speInf;
	CEnvironmentData bath;
	CEnvironmentData salt;
	CEnvironmentData temp;
	//DWORD filePointer;
	//RESLT res;
	//BOOL rslt;

	if(m_hdl != NULL && m_hdl != INVALID_HANDLE_VALUE)
		CloseOutputFile();

	m_hdl = CreateFile(FileName, GENERIC_READ, NULL, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL | FILE_FLAG_RANDOM_ACCESS, 0);
	if(m_hdl == INVALID_HANDLE_VALUE)
		return OPENFILEREAD_ERROR;


	if(FALSE == ReadFile(m_hdl, &m_sceParams, sizeof(_fSCENARIOPARAMS), &bytes, NULL))
	{
		CloseOutputFile();
		return FILEREAD_ERROR;
	}

	//CloseOutputFile();
	return OK;

	// Not sure about what follows
# if 0

	m_binOutputConfig = CFileManager::TranslateBinFileOutConfiguration(m_sceParams.binOutStateItemConfig);

	// Read in the bathymetry file (if the scenario was configured to save it).
	if(m_binOutputConfig.headerInf.bathyMap == TRUE && OK != (res = bath.LoadFromBinFile(m_hdl)))
	{
		CloseOutputFile();
		return res;
	}

	// Read in the salinity file (if the scenario was configured to save it).
	if(m_binOutputConfig.headerInf.salinityMap == TRUE && OK != (res = salt.LoadFromBinFile(m_hdl)))
	{
		CloseOutputFile();
		return res;
	}

	// Read in the temperature file (if the scenario was configured to save it).
	if(m_binOutputConfig.headerInf.temperatureMap == TRUE && OK != (res = temp.LoadFromBinFile(m_hdl)))
	{
		CloseOutputFile();
		return res;
	}

	// Skip past statistical data if scenario was configured to save it.
	if(m_binOutputConfig.headerInf.postRunAnalysis == TRUE)
	{
		filePointer = MySetFilePointer(m_hdl, sizeof(TAKE), FILE_CURRENT);
		filePointer = MySetFilePointer(m_hdl, sizeof(TAKESTATS)*m_sceParams.numSpecies, FILE_CURRENT);
	}

	// Read in species model information (if the scenario was configured to save it).
	if(m_binOutputConfig.headerInf.speInfAndAnimatAsscn == TRUE)
	{
		_ASSERT(m_speInf == NULL);
		_ASSERT(m_aniAssoc == NULL);
		m_speInf = new SPECIESBINOUTINF[m_sceParams.numSpecies];
		m_aniAssoc = new ANIMATASSCN[m_sceParams.totalNumAnimats];

		// Skip over minor species information
		for(i=0; i<(int)m_sceParams.numSpecies && rslt == TRUE; i++)
		{
			if(FALSE == (rslt = ReadFile(m_hdl, &m_speInf[i], sizeof(SPECIESBINOUTINF), &bytes, NULL)))
				return FILEREAD_ERROR;

			// Skip past the behavior names that belong to this species.
			filePointer = MySetFilePointer(m_hdl, __int64(m_speInf[i].description.numBehaviors) * sizeof(BEHAVIOR_NAME), FILE_CURRENT);
			if(INVALID_SET_FILE_POINTER == filePointer)
				return SETFILEPOINTER_ERROR;
		}

		// Read in animat summaries/association to species.
		rslt &= ReadFile(m_hdl, m_aniAssoc, m_sceParams.totalNumAnimats * sizeof(ANIMATASSCN), &bytes, NULL);
			return FILEREAD_ERROR;
	}

	// Skip over acoustic exposures, get the file pointer for animat states (- 1 
	// to number of iterations because because an initial AE isn't saved).




	m_filePointerAnimats =
		MySetFilePointer(m_hdl, __int64(m_sceParams.duration-1) * sizeof(ACST_SRC_STATE), FILE_CURRENT);
	if(INVALID_SET_FILE_POINTER == filePointer)
		return SETFILEPOINTER_ERROR;

	return OK;
#endif
}