Solved

C++ question - using ifstream

Posted on 2001-06-09
1
1,089 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

[Webinar] How Hackers Steal Your Credentials

Do You Know How Hackers Steal Your Credentials? Join us and Skyport Systems to learn how hackers steal your credentials and why Active Directory must be secure to stop them. Thursday, July 13, 2017 10:00 A.M. PDT

Question has a verified solution.

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

This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

705 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