使用char *所需的字符串 [英] Using string where a char* required
问题描述
我正在使用C API,它通常需要char *或char缓冲区。如果一个
C函数返回一个char *我不能使用字符串?或者我可以吗?我意识到我可以使用c_str()传递一个char *,但是接收一个char缓冲区到一个
字符串呢?
我问的原因是否则我要猜测要创建什么尺寸的缓冲区?
然后将缓冲区复制到字符串。看起来并不理想。
" Angus"写道:
>我正在使用C API,它通常需要char *或char缓冲区。如果
C函数返回一个char *我不能使用字符串?或者我可以吗?我意识到我可以使用c_str()传递一个char *,但是接收一个char缓冲区到一个
字符串呢?
我问的原因是否则我要猜测要创建什么尺寸的缓冲区?
然后将缓冲区复制到字符串。看起来不太理想。
std :: string的构造函数之一实际上会接受一个char *作为
参数。
因此:
char * foo {/ * stuff * /}
std :: string s;
s = foo();
或者至少类似的东西。
" osmium" < r1 ******** @ comcast.netwrote in message
news:5h ************* @ mid.individual.net ...
" Angus"写道:
我正在使用C API,它通常需要char *或char缓冲区。
如果
a
C函数返回一个char *我不能用字符串?或者我可以吗?我意识到我
可以
通过char *使用c_str()但是接收一个char缓冲区到
字符串怎么样?
原因我问的是否则我要猜测缓冲区的大小创建?
然后将缓冲区复制到字符串。看起来不太理想。
std :: string的一个构造函数实际上会接受一个char *作为
参数。
因此:
char * foo {/ * stuff * /}
std :: string s;
s = foo();
或者至少类似的东西。
For例如在Windows中有一个GetUserName函数,如下所示:
BOOL WINAPI GetUserName(LPWSTR lpBuffer,LPDWORD nSize)
如果你这样做:
std :: string str;
DWORD bufsize = 256;
GetUserName(str,& bufsize);
你得到编译错误:
错误C2664:''GetUserNameA'':无法从''类转换参数1
std :: basic_string< char,struct std :: char_traits< char>,class
std :: allocator< char''to''char *''
没有可用的用户定义转换运算符可以执行此
con版本,或操作员不能被调用
我想我可以演员?但想知道最好的解决方法是什么。
Angus写道:
" osmium" ; < r1 ******** @ comcast.netwrote in message
news:5h ************* @ mid.individual.net ...
>" Angus"写道:
>>我正在使用一个C API,它通常需要一个char *或char
缓冲区。如果一个
C函数返回一个char *我不能使用字符串?或者我可以吗?我知道我可以使用c_str()传递一个char *但是接收一个字符串的
char缓冲区怎么样?
我问的原因是否则我必须猜测是什么大小缓冲区来创建?然后将缓冲区复制到字符串。看起来不太理想。
std :: string的一个构造函数实际上会接受一个
char *作为参数。
因此:
char * foo {/ * stuff * /}
std :: string s;
s = foo();
或者在至少类似的东西。
例如在Windows中有一个像这样的GetUserName函数:
BOOL WINAPI GetUserName(LPWSTR lpBuffer,LPDWORD nSize)
如果你这样做:
std :: string str;
DWORD bufsize = 256;
GetUserName(str,& bufsize);
你得到编译错误:
错误C2664:''GetUserNameA'':无法从''类转换参数1
std :: basic_string< char,struct std :: char_traits< char>,class
std :: allocator< char''to''char *''
没有可用的用户定义转换运算符,可以执行此转换,或者运算符不能呼叫ed
我想我可以演员?但想知道最好的解决方法是什么。
最好的方法是分配TCHAR的临时(本地)数组(或者什么类型是LPWSTR指向的b / b
)然后_after_你调用
函数来填充该数组,从中创建一个字符串。
V
-
请在通过电子邮件回复时删除资金''A'
我没有回复最热门的回复,请不要问
I am working with a C API which often requires a char* or char buffer. If a
C function returns a char* I can''t use string? Or can I? I realise I can
pass a char* using c_str() but what about receiving a char buffer into a
string?
Reason I ask is otherwise I have to guess at what size buffer to create?
And then copy buffer to a string. Doesn''t seem ideal.
"Angus" writes:
>I am working with a C API which often requires a char* or char buffer. If
a
C function returns a char* I can''t use string? Or can I? I realise I can
pass a char* using c_str() but what about receiving a char buffer into a
string?
Reason I ask is otherwise I have to guess at what size buffer to create?
And then copy buffer to a string. Doesn''t seem ideal.One of the constructors for std::string will, in effect, accept a char* as
an parameter.
Thus:
char* foo { /*stuff*/ }
std::string s;
s= foo();
Or at least something similar to that.
"osmium" <r1********@comcast.netwrote in message
news:5h*************@mid.individual.net..."Angus" writes:
I am working with a C API which often requires a char* or char buffer.
If
a
C function returns a char* I can''t use string? Or can I? I realise I
can
pass a char* using c_str() but what about receiving a char buffer into a
string?
Reason I ask is otherwise I have to guess at what size buffer to create?
And then copy buffer to a string. Doesn''t seem ideal.
One of the constructors for std::string will, in effect, accept a char* as
an parameter.
Thus:
char* foo { /*stuff*/ }
std::string s;
s= foo();
Or at least something similar to that.
For example in Windows there is a GetUserName function like this:
BOOL WINAPI GetUserName ( LPWSTR lpBuffer, LPDWORD nSize )
if you do this:
std::string str;
DWORD bufsize = 256;
GetUserName(str, &bufsize);
you get compile error:
error C2664: ''GetUserNameA'' : cannot convert parameter 1 from ''class
std::basic_string<char,struct std::char_traits<char>,class
std::allocator<char'' to ''char *''
No user-defined-conversion operator available that can perform this
conversion, or the operator cannot be called
I suppose I could cast? But wondering what best way to tackle is.
Angus wrote:"osmium" <r1********@comcast.netwrote in message
news:5h*************@mid.individual.net...>"Angus" writes:
>>I am working with a C API which often requires a char* or char
buffer. If a
C function returns a char* I can''t use string? Or can I? I
realise I can pass a char* using c_str() but what about receiving a
char buffer into a string?
Reason I ask is otherwise I have to guess at what size buffer to
create? And then copy buffer to a string. Doesn''t seem ideal.
One of the constructors for std::string will, in effect, accept a
char* as an parameter.
Thus:
char* foo { /*stuff*/ }
std::string s;
s= foo();
Or at least something similar to that.
For example in Windows there is a GetUserName function like this:
BOOL WINAPI GetUserName ( LPWSTR lpBuffer, LPDWORD nSize )
if you do this:
std::string str;
DWORD bufsize = 256;
GetUserName(str, &bufsize);
you get compile error:
error C2664: ''GetUserNameA'' : cannot convert parameter 1 from ''class
std::basic_string<char,struct std::char_traits<char>,class
std::allocator<char'' to ''char *''
No user-defined-conversion operator available that can perform
this conversion, or the operator cannot be called
I suppose I could cast? But wondering what best way to tackle is.The best way is to allocate a temporary (local) array of TCHAR (or
whatever type is LPWSTR points to) and then _after_ you call the
function that would fill in that array, create a string from it.
V
--
Please remove capital ''A''s when replying by e-mail
I do not respond to top-posted replies, please don''t ask
这篇关于使用char *所需的字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!