Thread: Command line args

  1. #1
    Registered User
    Join Date
    Mar 2004
    Posts
    8

    Command line args

    First time using command line args in C, and can't get it to work. Here's what I got:

    this should get the one and only command line argument, which is a filename, then it calls openFile with that. If there is no command line arguments, it gets it from within the program.

    Code:
    if(argc == 2){		
    		if(openFile(argv[1]) == 1){
    			countWords();
    			showResults();
    			closeFile();	
    		}
    		else{
    			printf("Does not exist\n");
    			return EXIT_FAILURE;
    		}
    	}
    	else{
    		getFileName();
    		if(openFile(fileName) == 1){
    			countWords();
    			showResults();
    			closeFile();	
    		}
    		else{
    			printf("Does not exist\n");
    			return EXIT_FAILURE;
    		}
    	}
    and here is my open file

    Code:
    int openFile(char* filename){
    	/*Open the file*/
    	if((inFile = fopen(fileName, "r")) == NULL){
    		return 0;
    	}
    	return 1;
    }
    The code for getting the filename within the program works fine, it just tells me the file does not exist whenever it gets run with the command line arg. Any suggestions?

  2. #2
    Ultraviolence Connoisseur
    Join Date
    Mar 2004
    Posts
    555
    does the filename have spaces or anything? try using quotes around it on the command line.

    also i assume "inFile" is declared globally, and as FILE *?

    *EDIT* how are you declaring argc and argv?
    Last edited by nonpuz; 04-12-2004 at 04:40 PM.

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Yeah, you have a global variable with a near identical name to your parameter
    Code:
    int openFile(char* filename){
    	/*Open the file*/
    	if((inFile = fopen(fileName, "r")) == NULL){
    		return 0;
    	}
    	return 1;
    }
    The parameter you pass is being ignored.

    Since you don't refer to the global var when using the command line, you end up with the situation which you have now.

    Better get rid of inFile being a global as well.

    You may as well get rid of openFile, and actually do something like
    Code:
    if( (inFile=fopen(argv[1],"r")) != NULL){
    because your openFile() is adding almost no value
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #4
    Registered User
    Join Date
    Mar 2004
    Posts
    8
    thanks salem

  5. #5
    coder
    Join Date
    Apr 2004
    Posts
    6
    This one works for me.

    static FILE *open_file ( char *file, char *mode )
    {
    FILE *fp = fopen ( file, mode );

    /* "r" open for reading */
    /* "w" create for writing, discard previous contents */
    /* "a" append, open or create for writing */
    /* "r+" open for update */
    /* "w+" create for update, discard previous contents */
    /* "a+" append, open or create for update */

    if ( fp == NULL ) {
    printf("Unable to open %s for %s\n", file, mode);
    perror ( "Unable to open file" );
    exit ( EXIT_FAILURE );
    }

    return fp;
    }

  6. #6
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    What's the point? And do you really want to exit your program just because you can't open a file? I suppose it's ok for logging purposes, but as a general use tool, why not just use fopen as is?

    Quzah.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Pointer confusion
    By Blackroot in forum C++ Programming
    Replies: 11
    Last Post: 09-12-2007, 12:44 AM
  2. Replies: 2
    Last Post: 10-24-2005, 01:32 PM
  3. Replies: 1
    Last Post: 06-29-2004, 05:23 PM
  4. Multiple Command Line Args
    By mart_man00 in forum C Programming
    Replies: 10
    Last Post: 08-16-2002, 02:15 PM
  5. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 01:27 PM