Handling I/O Errors in C++ Programming

Handling I/O Errors in C++ Programming
Slide Note
Embed
Share

This tutorial covers the importance of error handling in C++ programming, specifically focusing on handling I/O-related errors. It discusses detecting input failures using cin.fail(), cin.eof(), and how to handle these failures using cin.clear() and cin.ignore(). Examples are provided to illustrate common scenarios such as file not found, empty input, and incorrect data types in input files.

  • C++
  • Error Handling
  • Input Failures
  • File Handling
  • Programming

Uploaded on Feb 27, 2025 | 0 Views


Download Presentation

Please find below an Image/Link to download the presentation.

The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author.If you encounter any issues during the download, it is possible that the publisher has removed the file from their server.

You are allowed to download the files provided on this website for personal or commercial use, subject to the condition that they are used lawfully. All files are the property of their respective owners.

The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author.

E N D

Presentation Transcript


  1. ECE 244 Programming Fundamentals Lec. 6: C++ I/O Error Handling Ding Yuan ECE Dept., University of Toronto http://www.eecg.toronto.edu/~yuan

  2. You learnt the normal I/O programming But in real software, you have to prepare for abnormal Anything that can go wrong, will go wrong. -- Murphy s law

  3. Handle I/O-related errors Detecting input failures cin.fail() cin.eof() Handling input failures cin.clear() cin.ignore() stringstream: treating string as streams Using << and >> on strings

  4. Running example int main() { int a; float b; ifstream inFile; inFile.open ( input.txt ); inFile >> a; inFile >> b; cout << a = << a << , b = << b << endl; return 0; } Question: what can go wrong?

  5. Exception #1: file doesnt exist int main() { int a; float b; ifstream inFile; inFile.open ( input.txt ); if (inFile.fail()){ /* fail() returns true if failure occurred! */ cout << Cannot open file: input.txt << endl; return 1; } inFile >> a; inFile >> b; cout << a = << a << , b = << b << endl; return 0; }

  6. Exception #2: cannot read a Simplest example: input.txt is empty! int main() { .. inFile >> a; if (inFile.fail()) { cout << Cannot read a! << endl; return 1; } inFile >> b; cout << a = << a << , b = << b << endl; return 0; }

  7. Exception #3: cannot read b Example: input.txt only contains an integer int main() { .. inFile >> a; if (inFile.fail()) { cout << Cannot read a! << endl; return 1; } inFile >> b; if (inFile.fail()) { cout << Cannot read b! << endl; return 1; } cout << a = << a << , b = << b << endl; return 0; } 2

  8. How does .fail() work? Return true when any error has occurred inFile is an object, it has an error state flag The flag is set whenever there is a failure You can also use it on cin, ofstream, cout

  9. 2 3.4 int main() { int a; float b; ifstream inFile; inFile.open ( input.txt ); if (inFile.fail()){ cout << Cannot open file: input.txt << endl; return 1; } inFile >> a; if (inFile.fail()) { cout << Cannot read a! << endl; return 1; } inFile >> b; if (inFile.fail()) { cout << Cannot read b! << endl; return 1; } cout << a = << a << , b = << b << endl; return 0; } 2.3 2..3

  10. But what if you want to reset & continue reading? Example: if error occurs, go to the next line and read again .ignore(int num_of_chars, char delimiter) Discard num_of_chars characters, up to first delimiter, which ever comes first Example: inFile.ignore(100, \n ); .clear() Clear the error state flag Resets the stream state to good

  11. Example int main () { int a; ifstream inFile; inFile.open ("input.txt"); if (inFile.fail()){ return 1; } while (1) { inFile >> a; if (inFile.fail()) { cout << "failed.." << endl; inFile.clear(); inFile.ignore(100, '\n'); continue; } cout << "a = " << a << endl; break; } return 0; } abc $23 78 96 What if inFile.clear() is removed? Any problem with this example? abc $23

  12. End-of-File (EOF) inFile.eof() A function that returns true if End-of-File is reached Used to read a file to the end Example: output a file content to the screen ifstream inFile( input.txt ); char c; inFile >> c; while (!inFile.eof()) { cout << c; inFile >> c; }

  13. stringstream The << and >> operators are quite powerful Easily format output Example: cout << a = << a << , b = << b << endl; Easily parse input Example: cin >> an_int >> a_float; Sometimes you want to use on string! E.g., format a string stringstream allows you to do that! It gives you a string, but allows you to use << and >> operator on it!

  14. stringstream example #include <string> // string #include <iostream> // cout #include <sstream> // stringstream using namespace std; int main () { stringstream ss; int foo = 100, bar = 200; ss << foo << ' ' << bar << endl; cout << ss.str(); ss >> bar >> foo; Output: 100 200 foo: 200 bar: 100 cout << "foo: " << foo << '\n'; cout << "bar: " << bar << '\n'; return 0; }

More Related Content