开发者

Program cannot open file, but the file is already there

I a开发者_运维问答m 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

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?

This is on Windows 7, Visual Studios Express 2010. C++.

EDIT: code below

#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:

I have tried

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

as TomK has suggested and it returned:

workingDir ==

Nothing at all. Hmm...

EDIT3: I am getting something. I tried

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

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


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

Are you sure mypic.jpg is located in C:\Users\Karl\Pictures ?


  1. Can u check whether the working directory is correct?

    #include <direct.h>
    
    char *workingDir =_getcwd(NULL, 0);
    
  2. Can you run your application with admin privileges?


Usually the .exe is created in sub-directory either Debug or Release - try giving the absolute path to the image ...


I've had this problem, and it turned out that Visual Studio's runtime wasn't setting the current directory. I never figured out the problem: instead I simply used an absolute path. Without the absolute path, your program is looking in C:\. You can also try using ".\\mypic.jpg" or GetCurrentDirectory().

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜