Solved

C++ question - using ifstream

Posted on 2001-06-09
1
1,087 Views
Last Modified: 2012-08-14
If I use this statement:

cout<<"File Name >";
cin>>filename;
ifstream input("filename.dat", ios::nocreate);

and the user inputs a file name that doesn't exist
is there a way to allow the user to enter the name again? The program halts if the file name is incorrect.  I have tried putting this in a loop, but it seems like the program fails so it halts.  Do I have to use exception handling?
0
Comment
Question by:fmora060901
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
1 Comment
 
LVL 8

Accepted Solution

by:
mnashadka earned 50 total points
ID: 6172606
Try something like:

ifstream input;

while(true)
{
  cout << "File Name >";
  cin >> filename;
  input.clear(); // Clear the last failure flags
  input.open(filename, ios::nocreate);
  if(! input)
    cout << "There was an error opening that file";
  else
    break;
}
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

733 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question