britanicus 1 Newbie Poster

This is a part the extension module im writing for python using c++. ( The complete source is available as an attachment )

#include <python2.6/Python.h>
#include <iostream>
#include <fstream>
#include "structmember.h"

using namespace std;

typedef struct {
	PyObject_HEAD
	fstream file;
} CppFileObject;

static int CppFile_init( CppFileObject *self, PyObject *args ) {

	PyObject *fn;
	if ( !PyArg_UnpackTuple( args, "s:CppFile", 1, 1, &fn ) ) {
		return -1;
	}

	char *filename = PyString_AS_STRING( fn );
	self->file.open( filename, ios::in | ios::out );

	return 0;
}

This compiles perfectly. However the problem is when I use the module, it throws a Segmentation Fault.

>>> import cppfile
>>> print cppfile
<module 'cppfile' from 'cppfile.so'>
>>> cf = cppfile.CppFile( "file" )
Segmentation Fault

I figured out that the problem is with line 21

self->file.open( filename, ios::in | ios::out );

though i do not know what it is. Could someone please guide me as to what exactly the problem is?

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.