Tutorial :fopen file from windows network location



Question:

I can open files from a mounted network drive, but not from an unmounted one e.g \\mycomp\folder2\hi.bmp

Any work around for this?


Solution:1

The following snippet works for me:

char buffer[1000];      FILE* file;  size_t bytesRead;    file = fopen("\\\\server\\share\\test.dat", "rb");    if (file != NULL)  {      bytesRead = fread(buffer, sizeof(char), sizeof(buffer), file);      fclose(file);  }  

Also note this excerpt from the fopen docs (MSDN):

...

fopen will accept paths that are valid on the file system at the point of execution; UNC paths and paths involving mapped network drives are accepted by fopen as long as the system executing the code has access to the share or mapped network drive at the time of execution. Special care must be taken when constructing paths for fopen to avoid making assumptions about available drives, paths or network shares in the execution environment.
...

You also need to consider, that the account you are running your program under, needs to have the appropriate access rigths to the file. When you mount the share as a network drive, maybe you are using different credentials to connect. That could cause fopen to fail.


Solution:2

fopen("\\\\192.168.1.4\\SharedFolder\\Configfile.txt","r");   

or

 fopen("\\\\ServerName\\SharedFolder\\Configfile.txt","r");   

Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Previous
Next Post »