This is driving me mad. This program reads 8 records from an iseries data queue and then I get the segmentation fault. I can't work out why. Does anyone have any ideas? Please!!

#define WIN32_LEAN_AND_MEAN


// Include the necessary DQ Classes
#include <iostream>
#include <fstream>
#include <windows.h>
#include <cwbdq.h>
#include <cwbco.h>
#include <cwbsv.h>
#include <cstring>
#include <stdlib.h>

using namespace std;

void WriteToExcel(unsigned char* receivedData);

static unsigned char* buffer1;

/**********************************************************************/

int main()
{
    static cwbDQ_QueueHandle* queueHandle;
    queueHandle = new cwbDQ_QueueHandle;
    buffer1 = new unsigned char [101];
    static cwbDQ_Data* queueData;
    queueData = new cwbDQ_Data;
    int result = 0;
    static cwbCO_SysHandle* sysHandle;
    sysHandle = new cwbCO_SysHandle;

    result = cwbCO_CreateSystem(TEXT("172.22.1.200"),sysHandle);

    cwbCO_Signon(*sysHandle,0);
    result = cwbDQ_OpenEx(*sysHandle,TEXT("TESTDQ    "),TEXT("X2TSTLIB  "),queueHandle,0);
    *queueData = cwbDQ_CreateData();
    result = cwbDQ_SetConvert(*queueData,CWB_TRUE);

    result = cwbDQ_SetDataAddr(*queueData,buffer1,100);
    result = cwbDQ_Peek(*queueHandle,*queueData,1,0);

    while(result != CWBDQ_QUEUE_DESTROYED)
    {
        result = cwbDQ_Read(*queueHandle,*queueData,1,0);
        switch(result)
        {
        case CWB_OK:
            cout << "Buffer " << buffer1 << "\0" <<"\n";
            WriteToExcel(buffer1);
            break;
        case CWBDQ_INVALID_TIME:
            cout << "Invalid Time" << "\n";
            break;
        case CWBDQ_INVALID_QUEUE_HANDLE:
            cout << "Invalid Queue Handle" << "\n";
            break;
        case CWBDQ_INVALID_SEARCH:
            cout << "Invalid search" << "\n";
            break;
        case CWBDQ_DATA_TRUNCATED:
            cout << "Data truncated" << "\n";
            break;
        case CWBDQ_TIMED_OUT:
            cout << "Timed out" << "\n";
            break;
        case CWBDQ_REJECTED_USER_EXIT:
            cout << "Rejected user exit" << "\n";
            break;
        case CWBDQ_QUEUE_DESTROYED:
            cout << "Queue destroyed" << "\n";
            break;
        case CWBDQ_CANNOT_CONVERT:
            cout << "Can't convert" << "\n";
            break;
        }
    }

    return 0;
}

/**************************************************************************************/

void WriteToExcel(unsigned char* receivedData)
{

    string InputData;
    string FileOperation;
    string FileName;
    string OutPutData;

    InputData = (char*)receivedData;

    FileOperation = InputData.substr(0,6);
    FileName = InputData.substr(6,30);
    for(int z=0; z<30; z++)
    {
        if(FileName[z] == '-') FileName[z] = ' ';
    }
    OutPutData = InputData.substr(36,64);

    ofstream OutPutFile;

    if(FileOperation == "DELETE")
    {
        DeleteFile(FileName.c_str());
    }

    OutPutFile.open(FileName.c_str(),ios::app);

    OutPutFile << OutPutData << "\n";

    OutPutFile.close();

}

lines 18-31 -- why do all that memory allocations?? Just declare the variables normally and pass them to other functions by address, something like this.

static unsigned char buffer1[101];

/**********************************************************************/

int main()
{
    static cwbDQ_QueueHandle queueHandle;
    static cwbDQ_Data queueData;
    int result = 0;
    static cwbCO_SysHandle sysHandle;

    result = cwbCO_CreateSystem(TEXT("172.22.1.200"),&sysHandle);

It's just the way it ended up after lots of trial and error. After programming for 20+ years I decided it was time to learn c++ so I am still a newbie at the language. I'll make the changes tomorrow and let you know.
Thanks

Tried that and I get errors back from the data queue library. The only one I can safely change is queueData. I am getting the following error :

Program received signal SIGSEGV, Segmentation fault.
At c:/mingw32/bin/../lib/gcc/mingw32/4.5.0/include/c++/ostream:513

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.