简体   繁体   中英

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. 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.

     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:

private: NumericUpDown * ta[];

Why am I printing a "1" to the file with the ofstream instead of the value in the numericUpDown component? 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. 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.

Any help or direction is appreciated. Thanks.

The problem has been solved using the following conversion on the Decimal to make it a 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. I am answering this in hopes that it might aid someone else in the same situation.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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