Thread: Persistent Segmentation Fault

  1. #1
    Registered User
    Join Date
    May 2010
    Posts
    178

    Persistent Segmentation Fault

    I am not certain why the following code I posted causes a persistent segmentation fault when run with one command like argument that is an invalid file name. Please take a look:

    Code:
    else if (argc > 1)	// Command-line arguments greater than one
    	{
    		FILE *inputFile = NULL;
    
    		do
    		{
    
    			while ((inputFile = fopen (*++argv, "r")) == NULL && argc > 1)	// Read arguments until file exists
    			{
    				printf("\n%s\n", *argv);	// Print file name
    				puts("\nFile not found error.\n");
    				argc--;	// Reduce arguments by 1
    			}
    
    
    			if (fgetc(inputFile) == EOF && argc > 1)	// Empty file
    			{
    				printf("\n%s\n", *argv);	// Print the file name
    				printf("\nFile empty.\n");
    			}
    
    			if (argc > 1)
    			{
    
    				fseek (inputFile , 0 , SEEK_SET );	// Position at the next line of text
    
    				src = fgets(buffer, 100, inputFile);	// Read the first line of the file
    
    				printf("\n%s\n", *argv);	// Print the file name
    
    				printf("\nLine: %s\n", src);
    			}
    
    			while(src != NULL && argc > 1)
    			{
    
    				if (*src != '\n')	// Not blank line
    				{
    					rValue = makeargv(src, delmtrs, &argvp);	    // Call makeargv to tokenize the string
    
    					for (i = 0; i < rValue; ++i)	// Print the tokens
    					{
    						printf("\t%s\n", argvp[i]);
    					}
    					printf("\nThe number of tokens in the string is: %d\n\n", rValue);
    				}
    
    				fseek (inputFile , 0 , SEEK_CUR );	// Position at the next line of text
    
    				src = fgets(buffer, 100, inputFile);	// Read the next line
    
    				if (src != NULL)
    				{
    					printf("\nLine: %s\n", src);
    				}
    				else
    					printf("\nEnd of file.\n");
    
    			}
    			fclose(inputFile);	// Close the file
    			argc--;	// Decrement the number of remaining arguments
    			}while (argc > 1);
    	}
    	free(argvp);
    	return 0;

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    This is pretty terrible:
    Code:
    while ((inputFile = fopen (*++argv, "r")) == NULL && argc > 1)
    How about this instead?
    Code:
    for( x = 1; x < argc; x++ )
    {
        inputFile = fopen( argv[ x ], "r" );
        ...
    }

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

  3. #3
    Registered User
    Join Date
    May 2010
    Posts
    178
    Is the modification to the while loop suspect by increment argv beyond [0]?

    I am not sure what you mean by changing the while loop testing for valid files to a for loop. Is that the implied meaning?

  4. #4
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,059
    Quote Originally Posted by Imanuel View Post
    Is the modification to the while loop suspect by increment argv beyond [0]?
    Yep! because when argv[] is incremented it'll be a null pointer causing fopen() to segfault; so swap the while expressions
    Code:
    while (argc > 1 && (inputFile = fopen (*++argv, "r")) == NULL)
    That's what quzah meant by switching from a while to a for loop - afaict.

  5. #5
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You will still have a problem, since you aren't ever checking to see if you try to access argv[ NOVALIDINDEX ]. You can't try to access beyond the bounds of an array, which is what he's trying to do.


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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Segmentation fault
    By bennyandthejets in forum C++ Programming
    Replies: 7
    Last Post: 09-07-2005, 05:04 PM
  2. Segmentation fault
    By NoUse in forum C Programming
    Replies: 4
    Last Post: 03-26-2005, 03:29 PM
  3. Locating A Segmentation Fault
    By Stack Overflow in forum C Programming
    Replies: 12
    Last Post: 12-14-2004, 01:33 PM
  4. Annoying Segmentation Fault
    By Zildjian in forum C++ Programming
    Replies: 7
    Last Post: 10-08-2004, 02:07 PM
  5. Segmentation fault...
    By alvifarooq in forum C++ Programming
    Replies: 14
    Last Post: 09-26-2004, 12:53 PM