0

我的问题是尝试读取文本文件时直接设置了“故障位”。至少对我来说,奇怪的是,如果我构建并运行程序,它就可以工作。但是当我尝试调试它时,故障位设置。

我得到的实际错误信息是这样的:

“Steg1_1A.exe 中 0x7740c41f 处的未处理异常:Microsoft C++ 异常:内存位置 0x003bf8e4 处的 std::ios_base::failure ..”

控制台程序的文本是这样的:“ios_base::failbit set”。

问题是,我该如何解决,以便在调试时 failbit 不会“使程序崩溃”?

这是我的功能:

void selectedMenuChoice(int choice)
{
int index = 0, initValue = 0;
const int fileNumberCount = 24;
double numFromFile = 0.0, sum = 0.0, average = 0.0, max = 0.0, min = 0.0;

switch (choice)
{
    // "Display temperature values"
    case 1:
        cout << "\nDisplaying the latest 24 temperature values:\n\n";
        break;

    // "View maximum and minimum temperatures"
    case 2:
        cout << "\nCalculating the maximum and minimum temperature...\n";
        break;

    // "View average temperature"
    case 3:
        cout << "\nCalculating average temperature...\n";
        break;
}

ifstream file;

file.exceptions(ifstream::failbit | ifstream::badbit);
try 
{
    //////////////////////////////////////////////////////////////////////////
    //////////////////////////////////////////////////////////////////////////
    // Here is the problem. It throws an exception directly when debugging
    file.open("templog.txt"); // filnamnet 
    //file.fail();

    // "View maximum and minimum temperatures"
    if (choice == 2)
    {
        initValue = 1;
        file >> numFromFile;
        max = min = numFromFile;
    }

    // Loopar igenom filen dock baserat på ett konstantvärde.
    for (index = initValue; index < fileNumberCount; index++)
    {
        file >> numFromFile;

        switch (choice)
        {
            // "Display temperature values"
            case 1:
                if (index % 6 == 0)
                {
                    cout << endl;
                }
                cout << fixed << setprecision(2) << setw(8) << numFromFile;
                break;

            // "View maximum and minimum temperatures"
            case 2:
                if (numFromFile > max )
                {
                    max = numFromFile;
                }
                if (numFromFile < min)
                {
                    min = numFromFile;
                }
                break;

            // "View average temperature"
            case 3:
                sum += numFromFile;
                average = sum/24;
                break;
        }
    }


}
catch (ifstream::failure e) 
{
    //std::cerr << "Exception opening/reading file.";
    cout << e.what(); // Skriver ut vad felet egentligen är..
}

file.close(); 

// Skriver ut information till användaren baserat på valet som användaren har gjort
if (choice == 2)
{
    cout << "\nMaximum temperature: " << fixed << setprecision(2) << max <<" degrees Celcius\n";
    cout << "\nMinimum temperature: " << min << " degrees Celcius\n";
}

else if (choice == 3)
{
    cout << "\nAverage temperature: ";
    cout << fixed << setprecision(2) << average << " degrees Celcius\n";
}

continueOnKeyPressed();
}
4

1 回答 1

0

扩展之前的评论:

许多编译器会将可执行文件的调试和发布版本放在不同的目录中。如果file.open("templog.txt");在发布模式下工作但不在调试模式下工作,那可能是因为输入文件与该版本的可执行文件位于同一目录中。

为确保始终可以找到该文件,请在调用open()函数时使用完整路径名。

于 2013-04-05T16:39:10.310 回答