Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/129.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ CString在MFC中编辑控件_C++_Vector_Mfc_Cstring - Fatal编程技术网

C++ CString在MFC中编辑控件

C++ CString在MFC中编辑控件,c++,vector,mfc,cstring,C++,Vector,Mfc,Cstring,所以我正在从一个.txt文件中检索So数据到一个myclass(类),包括 public: vector<int> ID vector<string> name vector<string> add for循环中编辑控制框中使用的代码 int s1; CString s2, s3; s1.Format(_T("\r\nID: %d"),myclass.ID[i]); s2.Format(_T("\r\nName: %s"),myclass.name[

所以我正在从一个.txt文件中检索So数据到一个myclass(类),包括

public:
vector<int> ID
vector<string> name
vector<string> add
for循环中编辑控制框中使用的代码

int s1;    
CString s2, s3;
s1.Format(_T("\r\nID: %d"),myclass.ID[i]);
s2.Format(_T("\r\nName: %s"),myclass.name[i]);
s3.Format(_T("\r\nAddress: %s"),myclass.add[i]);
Edi_box += s1 + s2 + s3;
因此它读取整数向量而不是字符串向量

您不能(或至少不应该)使用
%s
格式化
std::string
。试试这个:

s2.Format( _T("\r\nName: %s"), myclass.name[i].c_str() );

并对其他
std::string
变量执行类似操作。

根据VS项目的字符集设置,
\u T
宏将创建wchar\u T字符串或字符字符串。要将
std::string
(基于字符)格式化为这些格式之一,必须使用正确的转换。Microsoft提供了对许多函数支持的“normal”
printf()
样式语法的扩展:使用
%ls
插入wchar\t字符串,使用
%hs
插入char字符串

注:

  • 从wchar\u t到char的转换可能会失败,因此除非您已经有了受限的输入,否则不要期望在那里得到高质量的结果
  • 不能通过省略号传递任何类类型对象,因此需要C_str()函数中以C样式、NUL结尾的字符串
  • 如果您有多个向量,其中每个元素对应于另一个元素中的一个元素,请将单个向量与结构一起使用,这会使代码更加清晰
  • 将行尾字符(“\r\n”)放在末尾。;)
由于
格式的可变参数列表
无法判断何时使用错误。在大多数无法传递原始
std::string
的地方,如果您尝试,会出现错误,但不是在这里。我已经尝试过这一点,但不是1个乱码字符,而是更多乱码字符使用.c_str与%hs结合使用,它终于起作用了。非常感谢你们两位。这很有效,但只是为了配合。c_str()非常感谢我能帮上忙的朋友,@FranciscoMatias。事实上,我很惊讶编译器没有告诉您,因为通过省略号传递任何类对象都是不正确的——也许您停用了相应的警告?
s2.Format( _T("\r\nName: %s"), myclass.name[i].c_str() );