Const char*参数只给出第一个字符(在python3上) 我在C++中创建了一个非常简单的函数,使用了AIoAuthWrand。在参数中,我获得了创建文件的路径及其大小。要创建新文件,我使用intopen(constchar*pathname,int标志,mode\u t mode)
然后我使用:Const char*参数只给出第一个字符(在python3上) 我在C++中创建了一个非常简单的函数,使用了AIoAuthWrand。在参数中,我获得了创建文件的路径及其大小。要创建新文件,我使用intopen(constchar*pathname,int标志,mode\u t mode),python,c++,posix,aio,Python,C++,Posix,Aio,然后我使用:g++-Wall-g-Werror aio_calls.cpp-shared-o aio_calls.so-fPIC-lrt将其编译为共享对象 在Python2.7.5上,一切都很完美,但在Python3.4上,我只得到路径的第一个字符。有没有什么线索可以让它工作,这样它就可以走完整条路 以下是功能代码: #include <sys/types.h> #include <aio.h> #include <fcntl.h> #include <
g++-Wall-g-Werror aio_calls.cpp-shared-o aio_calls.so-fPIC-lrt
将其编译为共享对象
在Python2.7.5上,一切都很完美,但在Python3.4上,我只得到路径的第一个字符。有没有什么线索可以让它工作,这样它就可以走完整条路
以下是功能代码:
#include <sys/types.h>
#include <aio.h>
#include <fcntl.h>
#include <errno.h>
#include <iostream>
#include <string.h>
#include <unistd.h>
#include <stdio.h>
#include <fstream>
#include "aio_calls.h"
#define DLLEXPORT extern "C"
using namespace std;
DLLEXPORT int awrite(const char *path, int size)
{
// create the file
cout << path << endl;
int file = open(path, O_WRONLY | O_CREAT, 0644);
if (file == -1)
return errno;
// create the buffer
char* buffer = new char[size];
// create the control block structure
aiocb cb;
memset(buffer, 'a', size);
memset(&cb, 0, sizeof(aiocb));
cb.aio_nbytes = size;
cb.aio_fildes = file;
cb.aio_offset = 0;
cb.aio_buf = buffer;
// write!
if (aio_write(&cb) == -1)
{
close(file);
return errno;
}
// wait until the request has finished
while(aio_error(&cb) == EINPROGRESS);
// return final status for aio request
int ret = aio_return(&cb);
if (ret == -1)
return errno;
// now clean up
delete[] buffer;
close(file);
return 0;
}
这就是python 3上发生的情况:
Python 3.4.5 (default, May 29 2017, 15:17:55)
[GCC 4.8.5 20150623 (Red Hat 4.8.5-11)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> from ctypes import cdll
>>> m=cdll.LoadLibrary('/home/administrator/Documents/aio_calls.so')
>>> m.awrite('aa.txt', 40)
a
0
你说得对。它与Python3.x中的字符串编码和解码有关。我用谷歌搜索了一下,这个网站帮我找到了答案: 我将字符串转换为字节,如下所示:
>>> filename=bytes('aa.txt', 'utf-8')
现在我的函数也可以在Python3中使用
>>> m.awrite(filename, 40)
aa.txt
0
非常感谢@molbdnilo 你说得对。它与Python3.x中的字符串编码和解码有关。我用谷歌搜索了一下,这个网站帮我找到了答案: 我将字符串转换为字节,如下所示:
>>> filename=bytes('aa.txt', 'utf-8')
现在我的函数也可以在Python3中使用
>>> m.awrite(filename, 40)
aa.txt
0
非常感谢@molbdnilo 我相信Python 3采用了Unicode格式,因此前两个
char
s是第一个字符,第二个char
是零。我相信Python 3采用了Unicode格式,所以前两个char
s是第一个字符,第二个char
是零。