3

I have the following code:

    ifstream initFile;
    initFile.open("D:\\InitTLM.csv");
    if(initFile.is_open())
    {

      // Process file

    }

The file is not opening. The file does exist on the D: drive. Is there a way to find out exactly why this file cannot be found? Like an "errno"?

Blade3
  • 4,140
  • 13
  • 41
  • 57
  • 1
    Can you first try to place InitTLM.csv in the program directory and do `initfile.open("inittlm.csv");` rather then referencing a drive letter. Then let us know if the error still persists. – JonH Mar 22 '10 at 18:55
  • 1
    Don't use '\' as the seporator it adds another level of obscurity to the code as it is the escape character. Did you mean '\\' or did you mean '\\\\' Windows has accepted the '/' as a directory seporator since Windows XL. By using it you remove a layer of questions that we don't need to answer. – Martin York Mar 22 '10 at 20:27

4 Answers4

1

Answered here I believe: Get std::fstream failure error messages and/or exceptions

Community
  • 1
  • 1
neuroguy123
  • 1,355
  • 10
  • 14
1

You should be able to use your OS's underlying error reporting mechanism to get the reason (because the standard library is built on the OS primitives). The code won't be portable, but it should get you to the bottom of your issue.

Since you appear to be using Windows, you would use GetLastError to get the raw code and FormatMessage to convert it to a textual description.

R Samuel Klatchko
  • 74,869
  • 16
  • 134
  • 187
0

The STL is not great at reporting errors. Here's the best you can do within the standard:

  ifstream initFile;
  initFile.exceptions(ifstream::eofbit|ifstream::failbit|ifstream::badbit);
  try
    { 
      initFile.open("D:\\InitTLM.csv");
      // Process File
    }
  catch(ifstream::failure e) 
    {
      cout << "Exception opening file:" << e.what() << endl;
    }

In my experience, the message returned by what() is usually useless.

swestrup
  • 4,079
  • 3
  • 22
  • 33
0

Check the permissions on the root of the D: drive. You may find that your compiled executable, or the service under which your debugger is running, does not have sufficient access privileges to open that file.

Try changing the permissions on the D:\ root directory temporarily to "Everyone --> Full Control", and see if that fixes the issue.

Mike Pollitt
  • 1,817
  • 16
  • 13