this is my program
#include <ncurses.h>
int main( int argc, char *argv[] )
{
initscr();
FILE *fd;
char *ProgFile;
ProgFile = argv[1];
printw(ProgFile);
refresh();
fd = fopen(ProgFile,"rb");
if( fd==NULL )
{
printw("error");
perror ("The following error occurred");
refresh();
}
else
{
printw("bin file loaded: '%s'",ProgFile);
refresh();
}
getch();
endwin();
return 0;
}
when run it given this error message: No such file or directory.
but if i hardcode ProgFile = "filemname.bin"; then the program wor开发者_开发知识库ks perfectly.
when the program is run both versions print filemname.bin when asked the value of ProgFile.
I have been trying to solve this for 2 days and have no idea what is happening. can anyone tell me what is wrong ?
this is c++ on linux centos
First, this is C and not C++. I don't see any C++ in your code.
This
#include <stdio.h>
#include <stdlib.h>
int main( int argc, char *argv[] )
{
FILE *fd;
char *ProgFile;
ProgFile = argv[1];
printf(ProgFile);
fd=fopen(ProgFile,"rb");
if( fd==NULL )
{
printf("error");
}
else
{
printf("bin file loaded: '%s'",ProgFile);
}
return 0;
}
Works perfectly fine for me. Make sure you're passing the right argument and the correct path. I suggest you passing the whole path, and not just filemname.bin.
A program looks for relative filenames (those not starting with /
) in its present working directory; this is inherited from the parent, which is the shell if you execute the program from the command line, and can be set explicitly when you run it from a starter. So, you need to make sure the program is started in the same directory that the file is in.
(You could also use an absolute path or do an explicit chdir
system call the directory where the file is, but both are ugly and make it pretty much impossible to move the program around.)
精彩评论