如何解决将字符串转换为char *失败codecvt
我有以下代码:
std::wstring my_string = L"foo";
std::wstring_convert<std::codecvt_utf8<wchar_t> > my_conv;
const char *chars = my_conv.to_bytes( my_string ).c_str();
转换返回一个空指针(不是NULL
,只是""
)。
to_bytes
返回一个临时指针吗?
如何正确编写此代码?
解决方法
import discord
from discord.ext import commands
import re
class Counting(commands.Cog):
""" Users take turns incrementing a number. """
def __init__(self,client):
self.client = client
self.count_channel = 761981805862846525
self.num_regex = re.compile(r'(^\d+)')
self.cum_num = None
self.filename = "count.txt"
@commands.Cog.listener()
async def on_message(self,message):
with open(self.filename) as f:
read_data = f.read()
self.cum_num = int(read_data)
if message.channel.id == self.count_channel:
try:
match_obj = self.num_regex.search(message.content)
if int(match_obj.group(1)) != self.cum_num + 1:
await message.author.send(f'Your message must start with **{self.cum_num + 1}**!')
await message.delete()
else:
self.cum_num += 1
with open(self.filename,'w') as f:
f.write(str(self.cum_num))
except AttributeError:
if message.author.id != self.client.user.id:
await message.delete()
await message.author.send(f"Your message must be a number!")```
不返回临时的指针。它返回一个临时std::wstring_convert::to_bytes()
对象。您正在保存一个指向临时数据的指针,然后该临时超出范围并随后被销毁,留下悬挂指针到无效内存。
保存std::string
本身,而不是指向其数据的std::string
指针,例如:
char*
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。