c++ - 程序无法打开文件,但文件已经存在

标签 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 无法打开我的文件时,当文件与可执行文件一起位于同一目录中时,我是否应该考虑一些事情?

这是在 Windows 7、Visual Studios 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;
}

EDIT2:

我试过了

char *workingDir =_getcwd(NULL, 0);
printf("workingDir == %S", workingDir);

正如 TomK 所建议的那样,它返回了:

workingDir ==

什么都没有。嗯……

编辑 3: 我得到了一些东西。我试过了

argv[1] = "C:/Users/Karl/Pictures/mypic.jpg";

而fopen可以打开它。上面的语句插入在 fopen 之前。

最佳答案

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

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

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

关于c++ - 程序无法打开文件,但文件已经存在,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4720936/

相关文章:

mysql - 需要指导我的 Visual Basic 应用程序安装

python - 在Python中读取非标准文本文件

python - 如何读取通过附加行不断更新的文件?

python - 从文本文件中读取虚线

c++ - swap_if_null 的汇编代码

c++ - 为什么 std::extent 不能处理对像运算符 sizeof 这样的数组的引用?

c# - 将方法移动到新文件并将其保留在同一个类中(使其部分化)

c++:如何将字符串分解(不解析)为命令行参数?

c++ - “bzero”未在此范围内声明

visual-studio-2010 - 在 VS2010 中包含一个依赖项 - Visual Leak Detector