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();
}