N
N
Nikita Verdinsky2020-06-22 13:44:45
C++ / C#
Nikita Verdinsky, 2020-06-22 13:44:45

How to open a file correctly using try, catch, throw or how to make the program work correctly?

Program code
ifstream fin; 
    
    fin.exceptions(std::ios::badbit | std::ios::failbit);			
    
    try {
      cout<<"Открытие файла..."<<endl;			
      fin.open("E:\\product.txt");	
      cout<<"Файл успешно открыт!"<<endl;		
    }
    catch(const exception & ex) 
    {
      cout<<ex.what()<<endl;
      cout<<"Ошибка открытия файла!"<<endl;			
    }	
    while(!fin.eof()){
      line = "";
      getline(fin, line);
      cout<<line<<endl;
    }	
  fin.close();
  }


The file contains 3 lines with text and 4 lines "\n";

When launched, it shows the contents of the file and throws an error:
mistake
terminate called after throwing an instance of 'std::ios_base::failure'
what(): basic_ios::clear

I would like to get not a theoretical answer, but a practical one. Thank you!

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question