2013-04-05 3 views
0

내 문제는 텍스트 파일을 읽으려고 할 때 "failbit"이 직접 설정된다는 것입니다. 적어도 이상한 것은, 적어도 내 프로그램을 만들고 실행하면 작동한다는 것입니다. 그러나 그것을 디버깅하려고 할 때 failbit이 설정됩니다.ifstream이 텍스트 파일을 읽으려고 시도 할 때 failbit을 설정합니다.

"Steg1_1A.exe에서 0x7740c41f에서 처리되지 않은 예외 : 마이크로 소프트 C++ 예외 : 메모리 위치 0x003bf8e4에서 표준 : : ios_base :: 실패 ..."

내가 얻는 실제 오류 메시지는 이것이다

그리고 콘솔 프로그램에 대한 텍스트는 "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

Mr Bo Persson에게 감사드립니다. Hehe, gissar att du är svensk. :-) 당신은 정확했습니다! – user2130009

+0

@BoPersson이 답변을해야한다고 생각합니다. – 0x499602D2

답변

0

:

많은 컴파일러는하지만, 디버그 및 다른 디렉토리의 실행 파일의 버전을 발표 할 예정이다

여기 내 기능입니다. file.open("templog.txt");이 릴리스 모드에서는 작동하지만 디버그 모드에서는 작동하지 않으면 입력 파일이 해당 버전의 실행 파일과 동일한 디렉토리에 있기 때문일 수 있습니다.

파일을 항상 찾으려면 open() 함수를 호출 할 때 전체 경로 이름을 사용하십시오.

관련 문제