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;