Results 1 to 3 of 3
I have a weird issue. Can some one help.
When I hardcode abc.bin to inputFileName - I can open the file in cpp. But when I pass the same using ...
- 07-02-2010 #1Just Joined!
- Join Date
- Jul 2010
- Posts
- 6
can't open file - No file or directory
I have a weird issue. Can some one help.
When I hardcode abc.bin to inputFileName - I can open the file in cpp. But when I pass the same using an optional argument, it fails. Note that in both cases, fprintf, prints the inputFileName as abc.bin properly.
I get can't open - no file or directory.
Thank youCode:main() { char * inputFileName; char * optarg; inputFileName = optarg; //doesn't work. //inputFileName = "abc.bin"; //Works fprintf(stderr, "inputFileName:%s \n",inputFileName); } Another function: FILE *inFile; if ( inFile != NULL ) { inFile = fopen(inputFileName, "r+b"); if ( inFile == NULL ) { fprintf(stderr, "%s: can't open file %s: %s\n", ProgName, inputFileName, strerror(errno)); return NULL; } }
- 07-02-2010 #2
Does
char * optarg;
point to valid memory? Its not clear in your posting.Make mine Arch Linux
- 07-03-2010 #3Just Joined!
- Join Date
- Jul 2010
- Posts
- 6
Sorry.
extern char * optarg; declared in xyz.h file.
#include "xyz.h"
main(int argc, **argv)
{
char * inputFileName;
c = getopt_long_only(argc, argv, "aBc:Cd:e:E::GH::iIlL:m:Op:P:r:t:T:uUv::VZ",
long_options, &option_index); //some open source that gets optargs given argv
inputFileName = optarg; //doesn't work.
//inputFileName = "abc.bin"; //Works
fprintf(stderr, "inputFileName:%s \n",inputFileName); //It prints as abc.bin in both cases. That is weird, but does not work for inputFileName=optarg!!
}
Another function:
FILE *inFile;
if ( inFile != NULL )
{
inFile = fopen(inputFileName, "r+b");
if ( inFile == NULL )
{
fprintf(stderr,
"%s: can't open file %s: %s\n",
ProgName, inputFileName, strerror(errno));
return NULL;
}
}


Reply With Quote