2013-04-05 91 views
0

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

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

“在Steg1_1A.exe在0x7740c41f未处理的异常:微软C++异常:性病::的ios_base ::故障存储器位置0x003bf8e4。”

而控制台程序的文本是这样的:“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(); 
} 
+0

非常感谢Bo Persson先生! Hehe,gissar att duärsvensk。 :-)你是对的! – user2130009 2013-04-05 13:00:37

+0

@BoPersson我认为你应该让这个答案。 – 0x499602D2 2013-04-05 16:12:06

回答

0

扩展在更早的评论:

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

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