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

  • Home
  • SEARCH
  • 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 742311
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T08:44:02+00:00 2026-05-14T08:44:02+00:00

The following code: char filename[64]; ifstream input; cout << Please enter the filename: <<

  • 0

The following code:

char filename[64];
ifstream input;

cout << "Please enter the filename: " << endl;
cin >> filename;

input.open(filename);

if (!input.is_open())
{
    cout << "Opening file " << filename << " failed." << endl;
    exit(1);
}

fails, it enters the if() and exits. What could possibly be the cause for this? I’m using Microsoft Visual C++. When I hardcoded the filename as a constant it instead ended up garbled:

http://pici.se/pictures/CNQEnwhgo.png

Suggestions?

[Edit]

I managed to condense it into this minimal test case that fails:

#include <iostream>
#include <fstream>

using namespace std;

int main(int argc, char *argv[]){

    ifstream input;

    input.open("C:\\test.txt");

    if (!input.is_open())
    {
        cout << "Failed." << endl;
        exit(1);
    }

return 0;
}

I was wondering if there might be some discrepancy with the keymaps? That I’m inputting the filename in some charset while the filesystem knows it under some other name? I’m using Windows, by the way.

[Edit] Thanks for all your help but I give up now. I’ll use C style fopen instead. 🙂

[Edit] Oh my god. Now I feel so stupid. Turns out the file was actually named test.txt.txt and Windows hid the second .txt Once again, thanks for all your help…

  • 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-14T08:44:02+00:00Added an answer on May 14, 2026 at 8:44 am

    Can you make sure that the filename is what you think it is?

    cin >> filename; 
    cout << filename; 
    
    ifstream myFile(filename); 
    if ( myFile.is_open() ) { 
       // ... 
    }
    

    On Unix/Linux systems, remember that file names are case sensitive.

    ThisIsMyFile 
    thisIsMyFile 
    

    Are two distinct and separate files.

    [EDIT]

    ifstream::open is defined as:

    void open ( const char * filename, ios_base::openmode mode = ios_base::in );
    

    Opens a file whose name is s, associating its content with the stream object to perform input/output operations on it. The operations allowed and some operating details depend on parameter mode.

    The function effectively calls rdbuf()->open(filename,mode).

    If the object already has a file associated (open), the function fails.

    On failure, the failbit flag is set (which can be checked with member fail), and depending on the value set with exceptions an exception may be thrown.


    Try changing “C:\test.txt” to simply “test.txt” and run this program from the “C:\” directory.

    Here is an exact similar sample:

    // ifstream::is_open
    #include <iostream>
    #include <fstream>
    using namespace std;
    
    int main () {
    
      ifstream infile;
      infile.open ("test.txt");
      if (infile.is_open())
      {
        while (infile.good())
          cout << (char) infile.get();
        infile.close();
      }
      else
      {
        cout << "Error opening file";
      }
      return 0;
    }
    

    If something this obvious isn’t working, it’s time to fire up the debugger.

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

Sidebar

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.