简体   繁体   English

Fopen返回null,除非在那里进行了探索?

[英]Fopen returns null unless explored there?

This is a very odd error. 这是一个非常奇怪的错误。

I am using Visual Studio 2012, and in a C++ project (as a container for a C project), I am loading a file in this way: 我正在使用Visual Studio 2012,并且在C ++项目(作为C项目的容器)中,我以这种方式加载文件:

const char* fname = "SomeFile.csv";
if(!(fp = fopen(fname, "r")))
{
    printf("Error! Could not open %s!\n",fname);
    return;
}

The CSV is in the same folder as the .EXE, and that is the intention for this program. CSV与.EXE位于同一文件夹中,这是此程序的目的。


1. When I run it in debug, it fails to read the CSV. 1.在调试中运行它时,它无法读取CSV。

2. When I put in the full pathname to the file, it works correctly and loads the CSV. 2.当我在文件中输入完整路径名时,它可以正常工作并加载CSV。

3. When I go to the output folder in Windows Explorer, and run the .exe, it correctly loads the CSV. 3.当我进入Windows资源管理器中的输出文件夹并运行.exe时,它将正确加载CSV。

4. Now here is the weird part. 4.现在这是奇怪的部分。 When I go to another folder (anywhere else), and I then paste in the full path including the .exe into Windows Explorer, it starts up the program, as it should, but it fails to read the CSV. 当我转到另一个文件夹(在其他任何地方)时,然后将包括.exe的完整路径粘贴到Windows资源管理器中,它将按原样启动该程序,但无法读取CSV。

As a caveat, if I have the folder loaded in explorer, but run it in the VS2012 debugger, it also fails to load the CSV. 需要说明的是,如果我在资源管理器中加载了文件夹,但在VS2012调试器中运行了该文件夹,则它也无法加载CSV。

What is going on here? 这里发生了什么? Why would it only find it if I am running it while the window is open in explorer? 为什么在资源管理器中打开窗口时仅在运行时才能找到它?

fopen , open , etc. will, given a bare filename with no path components, attempt to open the named file in the "current directory". 给定没有路径组件的裸文件名, fopenopen等将尝试在“当前目录”中打开命名的文件。 Given a filename without a filesystem root it will attempt to open the file in a directory relative to the current directory. 给定没有文件系统根目录的文件名,它将尝试在对于当前目录的目录中打开文件。 Only when given an absolute (full) path will it look exactly where you've told it. 仅当给出绝对(完整)路径时,它才会准确地显示在您告诉它的位置。

In a command-line based system, the current directory is pretty obvious - it's the directory you are in when you issue the command. 在基于命令行的系统中,当前目录非常明显-这是您发出命令时所在的目录。

In a Graphical User Interface the notion of a current directory is a bit more mushy: 在图形用户界面中,当前目录的概念有些糊涂:

  • Navigate in Windows explorer to the program (let's say WORD.EXE ) and double click it - the current directory is likely to be the directory where the program is - the place to which you navigated (but there's no standard that says this has to be the case) 在Windows资源管理器中导航到该程序(假设为WORD.EXE ),然后双击它-当前目录可能是该程序所在的目录-您导航到的位置(但没有标准说明必须将其案子)
  • Navigate to your documents for your project and double click a Word document, which launches WORD.EXE automatically - what is the current directory? 导航到项目的文档,然后双击Word文档,该文档将自动启动WORD.EXE-当前目录是什么? It's likely to be where the document is. 它可能在文档所在的位置。
  • Launch WORD.EXE from the Start menu - what the heck is the current directory now? 从“开始”菜单启动WORD.EXE-当前目录到底是什么?
  • Launch your program from the debugger - now what is the current directory? 从调试器启动程序- 现在的当前目录是什么?

This last point is why you have to tell the IDE / debugger what to use as the current directory when it launches your program. 最后一点是为什么您必须告诉IDE /调试器启动程序时将其用作当前目录的原因。

The fileOpenDialog doesn't exactly "default" to the current directory - it open to where you last opened it, without changing the current directory for the program - when you pick a file it then passes the full path of that file to the program. fileOpenDialog并不完全“默认”到当前目录-它会打开到您上次打开它的位置, 而不会更改程序的当前目录 -选择文件时,它将文件的完整路径传递给程序。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM