Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7621147
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T04:07:30+00:00 2026-05-31T04:07:30+00:00

In a C++ program, using std::ifstream, I’m attempting to open a user-specified file —

  • 0

In a C++ program, using std::ifstream, I’m attempting to open a user-specified file — so far so good. However, I accidentally entered a filename that’s actually a directory, and I was quite surprised to see that attempting to open() that directory didn’t generate any errors.

Here’s a minimal example:

std::ifstream f;
f.open("..");
if(!f.is_open() || !f.good() || f.bad() || f.fail()) {
    std::cout << "error bit set on open" << std::endl;
    return 1;
}

No sign of error here. If I go on and attempt to getline(), getline() sets an error bit all right.

std::string str;
getline(f, str);

if(f.eof()) std::cout << "getline set eofbit" << std::endl;
else if(f.bad()) std::cout << "getline set badbit" << std::endl;
else if(f.fail()) std::cout << "getline set failbit" << std::endl;

This outputs “getline set badbit”, which is reasonable. Using the >> operator throws an underflow exception, which is also okay.

Now, my question is, how could I detect that the user entered a directory name instead of a proper filename? Is there any way to do that? Getting and ungetting bytes from the stream seem tedious and error-prone.

Also, why is this so? I realize that it’s all just the same data from the point of view of the program, but I’d assume the OS would also send some “hey, this is a directory” kind of message.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T04:07:31+00:00Added an answer on May 31, 2026 at 4:07 am

    You don’t say what your system is, so it’s hard to say, but generally,
    filebuf::open will only return an error if your system level open
    fails. And I’ve worked on Unix systems where you could open() a
    directory; I’ve even worked on some where you could read it after the
    open (at least if it was a locally mounted filesystem).

    As to what to do about it: about all I can think of is to try to get
    the first character, then put it back. But this fails if the file is
    empty, so it’s not really a solution either. At the system level (and
    from a QoI point of view, I’d expect filebuf::open to do this if the
    system did allow opening a directory), you can use a system level call
    (stat in Unix) to determine whether the file is a directory or not.
    (There’s a race condition, of course: between the moment you detect that
    it’s a normal file, and the moment you do the open, another process
    could delete the file and create a directory. It’s probably not a
    frequent occurance, however.)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a program that load data from a file using std::ifstream and store
For this program #include <iostream> using std::cout; struct C { C() { cout <<
I created this program: #include <iostream> #include <fstream> using namespace std; int main ()
I have this program in c++: #include <iostream> using namespace std; int main() {
I created a program using dev-cpp and wxwidgets which solves a puzzle. The user
I have some trouble reading from an ini file using boost program options. The
#include <iostream> #include <fstream> using namespace std; int main() { ifstream stream1(source.txt); string line
I wrote a program using AutoIT to fetch information from a number of websites
Suppose I write a program using immutable data structures in Java. Even though it
When programming an OO program using a SQL database back-end, do objects' attributes correspond

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.