I am using ctypes
in Python to open a file for writing in C++.
My C++ code:
extern "C" {
void openfile(const char *filename) {
cout<<"File to open for writing = " <<filename<<endl;
FILE *fp = fopen(filename,"w");
fprintf(fp,"writing into file");
fclose(fp);
}
}
My Python code:
>>> import ctypes
>>> lib = ctypes.cdll.LoadLibrary('/in/vrtime/mahesh/blue/rnd/software/test/test.so')
>>> outfile = "myfirstfile.txt"
>>> lib.openfile(outfile)
File to open for writing = m
I am getting the file name as m
, which is the first char
charater of my file.
How to pass whole string to the C side?