By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,823 Members | 756 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,823 IT Pros & Developers. It's quick & easy.

can't open file - says no file or directory

P: 2
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.

Expand|Select|Wrap|Line Numbers
  1.    main()
  2.    {
  3.    char * inputFileName;
  4.    char * optarg;
  5.    inputFileName = optarg;  //doesn't work.
  6.    //inputFileName = "abc.bin"; //Works
  7.     fprintf(stderr, "inputFileName:%s \n",inputFileName);
  8.    }
  9.  
  10. Another function:
  11.     FILE *inFile;
  12.     if ( inFile != NULL )
  13.     {
  14.         inFile = fopen(inputFileName, "r+b");
  15.         if ( inFile == NULL )
  16.         {
  17.             fprintf(stderr, 
  18.                     "%s: can't open file %s: %s\n",
  19.                     ProgName, inputFileName, strerror(errno));
  20.             return NULL;
  21.         }
  22.     }
  23.  
Thank you
Jul 2 '10 #1
Share this Question
Share on Google+
1 Reply


weaknessforcats
Expert Mod 5K+
P: 9,197
optarg is an uninitialized char*.

You need to get it pointed to a string containing the file name and path.
Jul 3 '10 #2

Post your reply

Sign in to post your reply or Sign up for a free account.