can't open file - says no file or directory

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • sdsjohnny
    New Member
    • Jul 2010
    • 2

    #1

    can't open file - says 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.

    Code:
       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;
            }
        }
    Thank you
  • weaknessforcats
    Recognized Expert Expert
    • Mar 2007
    • 9214

    #2
    optarg is an uninitialized char*.

    You need to get it pointed to a string containing the file name and path.

    Comment

    Working...