如何解决如何将单字符转换为系统字符串C ++?
我正在开发一个在控制台应用程序中显示以下字符/整数的程序。
我写的代码可以在控制台应用程序中使用,但不能在表单中使用...
我也想在我的表单(textBox-> text)中显示这些值。
我的myfunctions.h文件:
Binary Reader.H (form)
PackedContent t;
BinaryReader();
textBox1->Text = t.Header; // doesnt work...
表格:
textBox1->Text = Convert::ToString(t.Header); //doesn't work...
我也尝试过:import tkinter as tk
from tkinter import ttk
import matplotlib
matplotlib.use("TkAgg") # this is the backend of matplotlib
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg,NavigationToolbar2Tk
from matplotlib.figure import Figure
import matplotlib.animation as animation
from matplotlib import style
root = tk.Tk()
root.title("Tab Widget")
root.geometry("1300x950")
tabControl = ttk.Notebook(root)
tab1 = ttk.Frame(tabControl)
tab2 = ttk.Frame(tabControl)
tab3 = ttk.Frame(tabControl)
tab4 = ttk.Frame(tabControl)
tabControl.add(tab1,text ='Tab 1')
tabControl.add(tab2,text ='Tab 2')
tabControl.add(tab3,text ='Tab 3')
tabControl.add(tab4,text ='Tab 4')
tabControl.pack(expand = 1,fill ="both")
ttk.Label(tab1,text ="This is a tab\n (1)").grid(column = 0,row = 0,padx = 30,pady = 30)
ttk.Label(tab2,text ="This is another tab\n (2)").grid(column = 0,pady = 30)
ttk.Label(tab3,text ="This is another tab\n (3)").grid(column = 0,pady = 30)
ttk.Label(tab4,text ="This is another tab\n (4)").grid(column = 0,pady = 30)
label_b1 = ttk.Label(tab1,text = "Button label 0,1").grid(column = 0,row = 1,pady = 30)
label_b1 = ttk.Label(tab1,text = "Button label 1,1").grid(column = 1,2").grid(column = 2,pady = 30)
def plot():
# the figure that will contain the plot
fig = Figure(figsize = (5,5),dpi = 100)
y = [i**2 for i in range(101)] # list of squares
# adding the subplot
plot1 = fig.add_subplot(111)
# plotting the graph
plot1.plot(y)
canvas = FigureCanvasTkAgg(fig,master = tab2) # creating the Tkinter canvas containing the Matplotlib figure
canvas.draw()
canvas.get_tk_widget().grid() # placing the canvas on the Tkinter window
plot_button = tk.Button(master = tab2,command = plot,height = 2,width = 10,text = "Plot")
plot_button.grid() # place the button in main window
root.mainloop()
解决方法
如果您的char数组像C字符串一样以null结尾,那么您可以将其原样传递给std :: string c'tor:
textBox1->Text = std::string(t.Header)
您的char数组不是以null结尾的,因此您还应该提供大小,如下所示:
int headerSize = 1; // This variable is just for the example. Instead you can pass the size right in the function below
textBox1->Text = std::string(t.Header,headerSize)
或者:
textBox1->Text = std::string(t.Header,std::size(t.Header))
,
您忘了终止零,这就是为什么您遇到问题的原因。
如果要在长度为23的c数组中存储文本,则还需要char[24]
(23 + 1)包括尾随零。
无论如何,使用此C数组存储文本都是C风格的代码,而不是C ++,因此应避免这种情况。
如果您不包括以零结尾,那么此调用extBox1->Text = t.Header;
将尝试找到它以执行到String
的转换。这将导致未定义的行为,结果字符串的末尾将包含一些垃圾,否则将导致崩溃。
如果您的代码在此结构中记录的终止符为零,但达到大小限制,则将出现缓冲区溢出错误。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。