//******************************************************************************************************************************************************
//******************************************************************************************************************************************************
int WriteFile(std::fstream& fileHandle, BOOL* fieldSelections, int extractionMode, double* modeParameters, int numModeParameters, int numRecs, const mxArray *prhs[], int prhsIndex)
{
    EventRec buf;
    memset(&buf, 0, mRecordSize );
    int index = 0;

    for( int i = 0; i < numRecs; i++ ) {

        index = 0;
        if( fieldSelections[IndexTimestamp] ) {
            buf.qwTimeStamp = (unsigned __int64)(mInputFields[index][i]);
            index++;
        }

        if( !mGeneralOps.ValidRecToExtract(i, buf.qwTimeStamp, extractionMode, modeParameters, numModeParameters) ) {
            continue;
        }

        if( fieldSelections[IndexEventId] ) {
            buf.nevent_id = (short)(mInputFields[index][i]);
            index++;
        }
        if( fieldSelections[IndexTtl] ) {
            buf.nttl = (short)(mInputFields[index][i]);
            index++;
        }
        if( fieldSelections[IndexExtras] ) {
            for( int j = 0; j < EVENT_NUM_EXTRAS; j++) {
                buf.dnExtra[j] = (__int32)(mInputFields[index][(i*EVENT_NUM_EXTRAS)+j]);
            }
            index++;
        }
        if( fieldSelections[IndexEventString] ) {
            memset( buf.EventString, 0, sizeof(char)*NLX_EventRecStringSize);
            mxGetString( mxGetCell( prhs[prhsIndex], i ), buf.EventString, NLX_EventRecStringSize );
        }

        fileHandle.write( (const char*)&buf, mRecordSize );
    }
    return(Nlx2MatOK);
}
//******************************************************************************************************************************************************
//******************************************************************************************************************************************************
int WriteFile(std::fstream & fileHandle, BOOL* fieldSelections, int extractionMode, double* modeParameters, int numModeParameters, int numRecs)
{
	CRRec buf;
	memset(&buf, 0, mRecordSize );
	int index = 0;

	for( int i = 0; i < numRecs; i++ ) {

		index = 0;
		if( fieldSelections[IndexTimestamp] ) {
			buf.qwTimeStamp = (unsigned __int64)(mInputFields[index][i]);
			index++;	
		}

		if( !mGeneralOps.ValidRecToExtract(i, buf.qwTimeStamp, extractionMode, modeParameters, numModeParameters) ) { continue; }

		if( fieldSelections[IndexChannelNumbers] ) {
			buf.dwChannelNum = (unsigned __int32)(mInputFields[index][i]);
			index++;	
		}
		if( fieldSelections[IndexSampleFrequency] ) {
			buf.dwSampleFreq = (unsigned __int32)(mInputFields[index][i]);
			index++;	
		}
		if( fieldSelections[IndexNumberValidSamples] ) {
			buf.dwNumValidSamples = (unsigned __int32)(mInputFields[index][i]);
			index++;	
		}
		if( fieldSelections[IndexSamples] ) {
			for( int j = 0; j < MAX_CSC_SAMPLES; j++) {
				buf.snSamples[j] = (signed __int16)(mInputFields[index][(i*MAX_CSC_SAMPLES)+j]);
			}
		}

		fileHandle.write( (const char*)&buf, mRecordSize );
	}
	return(Nlx2MatOK2);
}