c++ - ifstream sets failbit when trying to read a textfile -


my problem "failbit" set directly when trying read textfile. strange thing, me @ least, if build program , run it, works. when try debug failbit sets.

the actual error message this:

"unhandled exception @ 0x7740c41f in steg1_1a.exe: microsoft c++ exception: std::ios_base::failure @ memory location 0x003bf8e4.."

and text console program this: "ios_base::failbit set".

the question is, how fix failbit doesn't "crash program" when debugging?

here function:

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 latest 24 temperature values:\n\n";         break;      // "view maximum , minimum temperatures"     case 2:         cout << "\ncalculating maximum , 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 problem. throws exception directly when debugging     file.open("templog.txt"); // filnamnet      //file.fail();      // "view maximum , minimum temperatures"     if (choice == 2)     {         initvalue = 1;         file >> numfromfile;         max = min = numfromfile;     }      // loopar igenom filen dock baserat på ett konstantvärde.     (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 , 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(); } 

expanding on earlier comment:

many compilers debug , release versions of executable in different directories. if file.open("templog.txt"); works in release mode not in debug mode, that's because input file located in same directory version of executable.

to make sure find file, use full path name when calling open() function.


Comments

Popular posts from this blog

monitor web browser programmatically in Android? -

Shrink a YouTube video to responsive width -

wpf - PdfWriter.GetInstance throws System.NullReferenceException -