程序无法打开文件,但是文件已经存在 [英] Program cannot open file, but the file is already there

查看:77
本文介绍了程序无法打开文件,但是文件已经存在的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am writing a program that will open an image file, but strange thing happened. This is the output from 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根本无法打开文件,我是否应该考虑一些事项?

Are there something I should consider when fopen simply failed to open my file when the file is right there along side with the executable file in the same directory?

这是在Windows 7,Visual Studios Express2010.C++上.

This is on 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所建议,它返回了:

as TomK has suggested and it returned:

workingDir ==

一无所有.嗯...

EDIT3 :我得到一些东西.我尝试过

EDIT3: I am getting something. I tried

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

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

And fopen can open it. This statement above is inserted right before the fopen.

推荐答案

绝对确保它们在同一目录中.我之所以这样说,是因为您使用的是Visual Studio,对于它来说相同"目录并不总是那么清晰,因为它取决于您如何通过IDE执行可执行文件.

Make absolutely sure they are in the same directory. I'm saying this because you're using Visual Studio, for which the "same" directory isn't always so clear, because it depends on how you execute the executable through the IDE.

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

确定 mypic.jpg 是否位于 C:\ Users \ Karl \ Pictures 中?

这篇关于程序无法打开文件,但是文件已经存在的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆