c++ - std::fstream on windows network share -


i struggling reading files windows network shares. have network share mounted s: code looks this:

std::fstream in; in.open("s:/adir/new folder/afile.txt", std::fstream::in); if (in.fail())     throw "failed"; ...  //use in reading 

when first run program, throws "failed". keeps failing until navigate windows explorer directory, after that, works.

how can fix problem?

in order open file, system executing code should have access share or mapped network drive @ time of execution.

the wnetaddconnection2 function makes connection network resource , can redirect local device network resource.

dword dwretval; netresource nr; dword dwflags;  // 0 out netresource struct memset(&nr, 0, sizeof (netresource));  nr.dwtype = resourcetype_any; nr.lplocalname = <name of local device>; nr.lpremotename = <network resource connect to>; nr.lpprovider = null;  dwflags = connect_update_profile;  // call wnetaddconnection2 function assign // drive letter share. dwretval = wnetaddconnection2(&nr,                               <password used>,                               <a user name>,                                dwflags);  if (dwretval == no_error)     // success ...     // can open file else     // failed ... 

Comments

Popular posts from this blog

Spring Boot + JPA + Hibernate: Unable to locate persister -

go - Golang: panic: runtime error: invalid memory address or nil pointer dereference using bufio.Scanner -

c - double free or corruption (fasttop) -