cant open file - says no file or directory

This is a discussion on cant open file - says no file or directory within the C Programming forums, part of the General Programming Boards category; I have a weird issue. Can some one help. When I hardcode abc.bin to inputFileName - I can open the ...

  1. #1
    Registered User
    Join Date
    Jul 2010
    Posts
    3

    cant 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

  2. #2
    Epy
    Epy is offline
    Fortran lover Epy's Avatar
    Join Date
    Sep 2009
    Location
    California, USA
    Posts
    960
    1. optarg has just been declared, not defined, so who knows what that pointer points to
    2. since inputfilename is assigned to optarg, it also points to who knows what

    try something like char *optarg = "abc.bin";

  3. #3
    Registered User jeffcobb's Avatar
    Join Date
    Dec 2009
    Location
    Henderson, NV
    Posts
    875
    actually if you are on a non-Microsoft platform, optarg is part of getopt as exemplified here. In any event you can't just assign it to a string and go for it. Here is how to use it (notice some bits missing in your code?)
    Code:
    /*************************************************
     * File: timer_main.cpp
     *************************************************/
    #include "timer.hpp"
    #include <iostream>
    #include <string>
    using namespace std;
    #include <stdlib.h>
    #include <getopt.h>
    
    #define CMDOPTS "hi:"
    
    
    
    void Usage()
    {
        cout << "Usage: Blah blah" << endl;
        exit(-1);
    }
    
    int main(int argc, char *argv[])
    {
       cout << "timer_test 1.0" << endl;
       string inputFile = "";
       int nRC = 0;
       opterr = 0;
       int c;
       while((c = getopt(argc, argv, CMDOPTS)) != EOF)
       {
          switch(c)
          {
    	 case 'h':
    	 {
    	    Usage();
    	    break;
    	 }
    	 case 'i':
    	 {
    	    inputFile = optarg;
    	    break;
    	 }
          }
       }
    
       /// ignore all this; from another experiment...
       //timer(60);
       int *board = create_board(3,3);
       int nValue = 0;
       for(int x=1; x<4; x++)
       {
          for(int y = 1; y < 4; y++)
          {
    	 for(int z = 1; z < 10; z++)
    	 {
    	    board[x][y][z] = nValue++;
    	    
    	 }
    	
          }
    
       }
       cout << "End processing." << endl;
    
       return nRC;
    }
    C/C++ Environment: GNU CC/Emacs
    Make system: CMake
    Debuggers: Valgrind/GDB

  4. #4
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    Am I in wrong forum? jeffcobb?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Memory Leak in AppWizard-Generated Code
    By jrohde in forum Windows Programming
    Replies: 4
    Last Post: 05-19-2010, 04:24 PM
  2. Can you help me about tolower() in file
    By nctar in forum C Programming
    Replies: 7
    Last Post: 05-12-2010, 10:04 AM
  3. Newbie homework help
    By fossage in forum C Programming
    Replies: 3
    Last Post: 04-30-2009, 04:27 PM
  4. File transfer- the file sometimes not full transferred
    By shu_fei86 in forum C# Programming
    Replies: 13
    Last Post: 03-13-2009, 12:44 PM
  5. C++ std routines
    By siavoshkc in forum C++ Programming
    Replies: 33
    Last Post: 07-28-2006, 12:13 AM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21