Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to access network file?

Tags:

c++

windows

using namespace std;

ofstream myfile;
//myfile.open ("Z:\\ABC.TXT");                 // fails Z: is a network drive
//myfile.open("C:\\Temp\\ABC.TXT");            // OK
//myfile.open("Z:\\NETWORK\\02-010E.CHS");     // fails Z:\Network is a network folder

if (myfile.is_open())
    cout << "file is open" << endl;
else
    cout << "file fails to open" << endl;

myfile.close();

Question: It seems that ofstream.open doesn't support to open a file on a network drive. Is there a simply way to solve this issue?

like image 653
q0987 Avatar asked Apr 15 '13 20:04

q0987


People also ask

How do I access a network drive?

Open File Explorer from the taskbar or the Start menu, or press the Windows logo key + E. Select This PC from the left pane. Then, on the Computer tab, select Map network drive. In the Drive list, select a drive letter.

How do I view open files on a network?

View Open Files on a Shared Network Folder on Windows Server Open the Computer Management console on your file server (or connect to the server remotely from the management console running on your computer) and go to System Tools -> Shared Folders -> Open files.

How do I find my network folder?

The easiest way to find a list of network shared folders is to use File Explorer (Windows 10) or Windows Explorer (Windows 8 and Windows 7). Open Windows File Explorer, go to the Folders pane, and select Network. Select the computer that has the shared folders you want to browse.


1 Answers

Try this:

  using namespace std;

  ofstream myfile;
  myfile.open("\\\\servername\\filepath\\filename"); 
              //^^should follow this format, servername is not Z drive name

  if (myfile.is_open())
     cout << "file is open" << endl;
 else
     cout << "file fails to open" << endl;

 myfile.close();

I tried this to open a file on a shared server, it outputs

file is open

so it should work.

The Z drive is actually not real physical drive, it is just a mapping to real physical drive on the server.

like image 153
taocp Avatar answered Oct 05 '22 23:10

taocp