【问题标题】:Printing a NumericUpDown value out of a ofstream从 ofstream 中打印 NumericUpDown 值
【发布时间】:2013-03-18 19:24:21
【问题描述】:

这应该是一个简单的任务,但我收到的文件包含数字“1”而不是 numericUpDown 控件的内容。使用断点我可以看到值 ta[i]->Value 是我期望的值,但是在转换之后,我在文件中得到 1 而不是值。

     private: void storePreviousSettings()
{
    ofstream settings("prev_settings.txt");
    if(settings.is_open())
    {
        settings << "#ta" << endl;
        for(int i = 0; i < 16; i++)
        {
            settings << ta[i]->Value.ToString() << endl;
        }
        settings << "End" << endl;
        settings.close();
    }
}

注意:ta 是这样定义的:

private: NumericUpDown * ta[];

为什么我使用 ofstream 而不是 numericUpDown 组件中的值在文件中打印“1”?我怎样才能解决这个问题?我可以执行其他写入文件的方法吗?

更新/当前失败的尝试

如果我添加这一行:

System::String * temp = ta[i]->Value.ToString();

在“设置 Value ...”行之前,使用断点我可以看到“temp”保持预期值,而 ta[i]->Value.ToString() 是在职的。因此,当 ta[i]->Value.ToString() 与

感谢任何帮助或指导。谢谢。

【问题讨论】:

    标签: c++ visual-c++ io ofstream


    【解决方案1】:

    问题已解决,使用以下对 Decimal 的转换使其变为 double。

        private: void storePreviousSettings()
    {
        ofstream settings("prev_settings.txt");
        if(settings.is_open())
        {
            settings << "#ta" << endl;
            for(int i = 0; i < 16; i++)
            {
                settings << System::Decimal::ToDouble(ta[i]->Value) << endl;
            }
            settings << "End" << endl;
            settings.close();
        }
    }
    

    如果有人知道使用 .ToString() 最初不起作用的原因,请发布。我正在回答这个问题,希望它可以帮助处于相同情况的其他人。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-11-21
      • 2022-01-25
      • 2023-04-08
      • 2022-12-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多