【问题标题】:ifstream sets failbit when trying to read a textfileifstream 在尝试读取文本文件时设置故障位
【发布时间】:2013-04-05 12:29:45
【问题描述】:

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

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

“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();
}

【问题讨论】:

  • 非常感谢 Bo Persson 先生!呵呵,gissar att du är svensk。 :-) 你是对的!!
  • @BoPersson 我认为你应该回答这个问题。

标签: c++


【解决方案1】:

扩展之前的评论:

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-03-29
    • 2023-03-10
    • 2014-06-12
    • 1970-01-01
    • 2011-09-17
    • 2012-06-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多