C++ 程序无法打开文件,但该文件已存在

C++ 程序无法打开文件,但该文件已存在,c++,visual-studio-2010,file,fopen,C++,Visual Studio 2010,File,Fopen,我正在写一个程序,将打开一个图像文件,但奇怪的事情发生了。这是cmd的输出: C:\Users\Karl\Pictures>testcvconsole mypic.jpg argv[0]==testcvconsole argv[1]==mypic.jpg fopen is null strerror(errno)==No such file or directory 当文件在同一目录中与可执行文件并排时,当FOPEN无法打开文件时,我应该考虑什么?p> 这是在Windows7上的Visu

我正在写一个程序,将打开一个图像文件,但奇怪的事情发生了。这是cmd的输出:

C:\Users\Karl\Pictures>testcvconsole mypic.jpg
argv[0]==testcvconsole
argv[1]==mypic.jpg
fopen is null
strerror(errno)==No such file or directory
当文件在同一目录中与可执行文件并排时,当FOPEN无法打开文件时,我应该考虑什么?p> 这是在Windows7上的Visual Studio Express 2010。C++,

编辑:下面的代码

#include "stdafx.h"
#include <string.h>
#include <errno.h>

int goMain(int argc, char** argv);

int _tmain(int argc, _TCHAR* argv[])
{
 goMain(argc, (char**)argv);
 return 0;
}

int goMain( int argc, char** argv ){

 if (argv[1] != NULL){
  printf("argv[0]==%S\nargv[1]==%S\n", argv[0], argv[1]);

  if (fopen(argv[1], "r") == NULL){
   printf("fopen is null\n");
   printf(strerror(errno));
  }

 }

 return 0;
}
正如TomK所建议的,它返回:

workingDir ==
什么都没有。嗯

EDIT3: 我得到了一些东西。我试过了

argv[1] = "C:/Users/Karl/Pictures/mypic.jpg";
fopen可以打开它。上述语句插入到fopen的正前方

  • 你能检查一下工作目录是否正确吗

    #include <direct.h>
    
    char *workingDir =_getcwd(NULL, 0);
    
    #包括
    char*workingDir=_getcwd(NULL,0);
    
  • 你能用管理员权限运行你的应用程序吗

  • 绝对确保它们位于同一目录中。我这样说是因为您使用的是Visual Studio,对于它,“相同”的目录并不总是那么清晰,因为它取决于您如何通过IDE执行可执行文件

    C:\Users\Karl\Pictures>testcvconsole mypic.jpg
    

    你确定
    mypic.jpg
    位于
    C:\Users\Karl\Pictures

    通常.exe是在Debug或Release子目录中创建的-尝试给出图像的绝对路径…

    我遇到了这个问题,结果是Visual Studio的运行时没有设置当前目录。我从未发现问题所在:相反,我只是使用了一条绝对路径。如果没有绝对路径,程序将在
    C:\
    中查找。您也可以尝试使用
    “\\mypic.jpg”
    或。

    1。我们怎么知道照片真的在那里?2.给我一些代码!请向我们展示您的源代码。按要求更新@比利·奥尼尔:因为我把picture和.exe都放在同一个目录下。我建立了.exe,复制它并把它放在Pictures文件夹中,所以它不应该在同一个目录下吗?是的。
    mypic.jpg
    也在吗?你能用其他方式打开它吗(比如说,一些脚本或批处理文件)?简单的测试,@unknownthreat。当您在
    C:\Users\Karl\Pictures
    目录中的命令提示符下时,只需键入
    mypic.jpg
    并按ENTER键。如果文件在那里,它应该在IE或Firefox或任何绑定到JPG文件的应用程序中打开。@unknownthreat:作为另一个测试,您可以编写一个没有unicode内容(
    main
    ,而不是
    \u tmain
    ),也没有
    stdafx
    的简单main吗(只需创建一个空的控制台项目,并使用
    main
    中的代码向其中添加一个新的.c文件)啊哈,在工作中使用main而不是_tmain。所以unicode是问题的原因,不是吗?这很公平,但是,有时我需要打开没有用英语命名的文件;我需要unicode。那么在这种情况下,我应该寻找什么呢?
    C:\Users\Karl\Pictures>testcvconsole mypic.jpg