来自 Windows 网络位置的 fopen 文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3308245/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 14:53:16  来源:igfitidea点击:

fopen file from windows network location

c++cwindowsfopen

提问by Frank Bollack

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

我可以从安装的网络驱动器打开文件,但不能从未安装的驱动器打开文件,例如 \\mycomp\folder2\hi.bmp

Any work around for this?

有什么解决办法吗?

回答by Frank Bollack

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 文档 (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.
...

...

fopen 将接受在执行点文件系统上有效的路径;只要执行代码的系统在执行时可以访问共享或映射网络驱动器,就可以接受 UNC 路径和涉及映射网络驱动器的路径。为 fopen 构建路径时必须特别小心,以避免对执行环境中的可用驱动器、路径或网络共享做出假设。
...

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 fopento fail.

您还需要考虑,您运行程序的帐户需要对文件具有适当的访问权限。当您将共享安装为网络驱动器时,您可能使用不同的凭据进行连接。那可能会导致fopen失败。

回答by Siddiqui

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

or

或者

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