Find the answer to your Linux question:
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 ...
  1. #1
    Just 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.

    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

  2. #2
    Linux Enthusiast gerard4143's Avatar
    Join Date
    Dec 2007
    Location
    Canada, Prince Edward Island
    Posts
    714
    Does

    char * optarg;

    point to valid memory? Its not clear in your posting.
    Make mine Arch Linux

  3. #3
    Just 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;
    }
    }

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...