Reading a stream in C ++

I have the following code:

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

      // Process file

    }

File does not open. The file exists on drive D :. Is there any way to find out why this particular file cannot be found? How is errno?

+3
source share
4 answers

You should be able to use the OS error reporting mechanism to get a reason (since the standard library is built on OS primitives). The code will not be portable, but it should help you understand the problem.

Since you seem to be using Windows, you should use GetLastError to get the raw code and FormatMessage to convert it to a text description.

+1
source
+1

STL . , :

  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;
    }

, , , () .

0

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

Try temporarily changing the permissions in the root directory D: \ to "All → Full Control" and see if this fixes the problem.

0
source

Source: https://habr.com/ru/post/1738080/


All Articles