Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ - Can't open file from network path in Windows

Tags:

c++

windows

I'm having problems using native C++ to open a file located on a network drive on a Windows box. My code works fine if the file is local, but fails if the file is on a network share. I can read the file from Windows explorer perfectly fine.

  ifstream ifs(cFilename);
  if(ifs.is_open())
  {
    // Read file here. (This never works for a network path)
  }

I've also tried this:

struct stat sb;
if (stat(cFilename, &sb) == 0)
{
  // Read file here. (This never works for a network path)
}

My path is formatted correctly (e.g. "\\server\filename.ext"), but I still can't open it. Any ideas?

like image 837
Kenji Avatar asked Dec 19 '25 13:12

Kenji


1 Answers

If the name is in the form \\server\filename, then it seems that might not be correct. I believe that typically it needs a share name as well:

\\server\share\filename

Also, make sure that in the code, you escape the backslashes (e.g., \\\\server\\share\\filename).

like image 57
Mark Wilkins Avatar answered Dec 22 '25 03:12

Mark Wilkins



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!