【问题标题】:How to convert a SINGLE char to system string C++?如何将 SINGLE char 转换为系统字符串 C++?
【发布时间】:2020-09-07 14:18:33
【问题描述】:

我正在开发一个在控制台应用程序中显示以下字符/整数的程序。
我编写的代码在控制台应用程序中有效,但在表单中无效...
我还想在我的表单中显示这些值(textBox->text)。

我的 myfunctions.h 文件:


    typedef struct{
    
        char Header         [23]; //the char I want to display is 23 characters long
        int  Version        [4];  //4 characters...
        char Projectname    [21];
        char Developer      [8];
        char email          [16];
        char Description    [132];
        char Unknown        [1]; 
    
    }PackedContent;
    
    void BinaryReader(){
    
    system("CLS");
    
    PackedContent t;
    
    fstream myFile;
    
    myFile.open("PackedContent.opf");
    
    if(!myFile){
    
    
        cout<<"An unknown error occured! Cannot read .opf file... exiting now... \n"<<endl;
        
    
    }else{
    
        cout    <<"Reading packed data content... done!\n"<<endl;
        
        myFile.read((char *)&t, sizeof(PackedContent));
    
    
        cout<<"\nHeader      :"         <<t.Header              <<endl; //  Header info [ok]
        
        //cout<<"\nVersion     :"           <<t.Version             <<endl; //  Project Version [err]
        
        cout<<"\nProject name:"         <<t.Projectname             <<endl; //  Project name
        
        cout<<"\nDeveloper name:"       <<t.Developer<<endl;
        cout<<"\nEmail       :"         <<t.email               <<endl; //  Developer email
        cout<<"\nDescription :"         <<t.Description         <<endl; //  Project description [ok]
        cout<<"Unknown"                 <<t.Unknown             <<endl;
    }

表格:

Binary Reader.H (form)
PackedContent t;

BinaryReader();
textBox1->Text = t.Header; // doesnt work...

我也试过了:
textBox1-&gt;Text = Convert::ToString(t.Header); //doesn't work...

【问题讨论】:

  • 我认为你需要为 c++-cli 和/或 winforms 标记
  • char 数组作为字符串的问题是它们需要以空值终止。通常在普通的标准 C++ 中(您的表单代码很可能不是,它可能是托管的 C++/CLI)然后您可以从任意 un 终止的数组创建一个字符串作为std::string(t.Header, 23)。但是考虑到您的程序可能具有 C++/CLI 的性质,您可能需要找到一种特定于 C++/CLI 的方法来做同样的事情。
  • 您的标题以及代码中的注释具有误导性,您没有“SINGLE char”,您有一个chararray。 “单个字符”将是单个 char,而不是数组。您可能在某些地方说“char” 的意思是 "string"
  • 您忘记了终止零,这就是您遇到问题的原因。如果您要将字符串存储在长度为 23 的 c-array 中,您需要 char[24] 来包含尾随零。无论如何,使用这个 c 数组来存储文本是 C 风格的代码而不是 C++。
  • 另外请提供minimal reproducible example并描述实际结果。 “不起作用”不是对问题的描述。

标签: c++ string char


【解决方案1】:

如果您的 char 数组是空终止的,例如 C 字符串,您可以将它按原样传递给 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))

【讨论】:

    【解决方案2】:

    您忘记了终止零,这就是您遇到问题的原因。

    如果您要在长度为 23 的 c 数组中存储文本,则需要 char[24] (23 + 1) 来包含尾随零。

    无论如何,使用这个 c 数组来存储文本是 C 风格的代码而不是 C++,应该避免这种情况。

    如果您不包括终止零,则例如此调用extBox1-&gt;Text = t.Header; 将尝试找到它以执行到String 的转换。这将导致未定义的行为,结果字符串最后会包含一些垃圾,否则会以崩溃告终。

    如果您的代码在此结构中记录以零结尾,但达到大小限制,那么您将遇到缓冲区溢出错误。

    【讨论】:

    • 有一个std::string c'tor 接受一个字符数组,后跟一个大小。这应该可以克服缺少空终止的问题。
    • 如果没有minimal reproducible example,就很难分辨。请注意,当他在此结构中存储数据时,可能会导致缓冲区溢出错误。
    猜你喜欢
    • 1970-01-01
    • 2018-03-27
    • 2013-05-13
    • 2012-01-16
    • 2016-02-02
    • 2018-09-02
    • 2016-04-20
    • 2012-03-07
    • 1970-01-01
    相关资源
    最近更新 更多