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
Solved

C++ question - using ifstream

Posted on 2001-06-09
1
1,085 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
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: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

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

Title # Comments Views Activity
SetCurrentDirectory path limit 7 131
Path to  STL Map header file 1 69
FMX TCameraComponent Problem 2 82
object oriented programming comparison 5 77
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…
Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
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 learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

839 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