ifstream question

K

Kitty

I use " ifstream input_file("input.txt") " to open a file. I want to show a
warning message if "input.txt" does not exist. How can I do for that?

Thanks.
 
B

Buster

Kitty said:
I use " ifstream input_file("input.txt") " to open a file. I want to show a
warning message if "input.txt" does not exist. How can I do for that?

#include <istream>

void show_message ()
{
// show a warning message
}

void f ()
{
std::istream input_file ("input.txt");
if (! input_file) show_message ();
}
 
M

Mike Wahler

Kitty said:
I use " ifstream input_file("input.txt") " to open a file. I want to show a
warning message if "input.txt" does not exist. How can I do for that?

Standard C++ does not provide a way to determine the
existence of a file. You'll need a platform-specific
solution for that. Check your compiler and/or OS
documentation. The best you can do with standard C++
is determine whether an attempt to open a file succeeded
or failed. The actual reason for failure cannot be known
by standard means.

std::ifstream input_file("input.txt");
if(!input_file)
std::cerr << "Could not open input file\n";
else
/* etc. */

-Mike
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,764
Messages
2,569,564
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top