简体   繁体   English

从流中打印NumericUpDown值

[英]Printing a NumericUpDown value out of a ofstream

This should be a simple task, but I am recieving a file containing the number "1" instead of the contents of the numericUpDown control. 这应该是一个简单的任务,但是我收到的文件中包含数字“ 1”,而不是numericUpDown控件的内容。 Using breakpoints I can see the value from ta[i]->Value is the value I would expect it to be, but then after the conversion I get a 1 in the file instead of the value. 使用断点,我可以看到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();
    }
}

Note: ta is defined like so: 注意:ta的定义如下:

private: NumericUpDown * ta[];

Why am I printing a "1" to the file with the ofstream instead of the value in the numericUpDown component? 为什么我要使用ofstream而不是numericUpDown组件中的值在文件上打印“ 1”? How can I fix this? 我怎样才能解决这个问题? Is there an alternative method for writing to a file can I perform? 是否可以执行写入文件的替代方法?

Update/Current Failed Attempts 更新/当前失败的尝试

If I add this line: 如果我添加以下行:

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

Before the "settings << ta[i]->Value ..." line, using break points I can see that "temp" holds the expected value, and ta[i]->Value.ToString() is working. 在“设置<< ta [i]-> Value ...”行之前,使用断点,我可以看到“ temp”保存了预期值,并且ta [i]-> Value.ToString()正在工作。 So when ta[i]->Value.ToString() is used with the << operator something must be changing in order for me to recieve a "1" in the file instead of the value I am seeing at my break points before it is wrote to the file. 因此,当ta [i]-> Value.ToString()与<<运算符一起使用时,必须进行一些更改,以便我在文件中接收到“ 1”,而不是在断点之前看到的值被写入文件。

Any help or direction is appreciated. 任何帮助或指导表示赞赏。 Thanks. 谢谢。

The problem has been solved using the following conversion on the Decimal to make it a double. 使用以下对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();
    }
}

If anyone knows the reason why using .ToString() did not work originally, please post. 如果有人知道使用.ToString()最初不起作用的原因,请发表。 I am answering this in hopes that it might aid someone else in the same situation. 我正在回答这个问题,希望它可以帮助处于相同情况的其他人。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM