我正在编写一个程序来打开一个图像文件,但奇怪的事情发生了。这是 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/